私は非常に特殊な状況に直面しています。DB操作にSpring 3.0.5で休止状態テンプレートを使用しています。初めて User モデルを挿入しようとすると、DataAccessException がスローされ、これをキャッチします。ここで、同じ DB 操作を 3 回再試行したいと考えています。2 回目は、例外はスローされません。
コードは次のとおりです。
package com.user.profile.dao;
@Repository("userProfileDAOImpl")
public class UserProfileDAOImpl implements IUserProfileDAO {
@Autowired
private HibernateTemplate hibernateTemplate;
public Long insertUserProfileData(User user) throws AppNonFatalException {
Long id = null;
int retryCount = 0;
while (retryCount < 3) {
try {
id = (Long)hibernateTemplate.save(user);
}
catch (DataAccessException e) {
e.printStackTrace();
retryCount++;
System.out.println("Retry Count = " + retryCount);
if (retryCount > 3) {
throw new AppNonFatalException(e.getLocalizedMessage(), "10000", e.getMessage(), e);
}
}
catch (Exception e) {
/* not coming inside this block too second time onwards */
System.out.println("Pure Exception");
}
}
return id;
}
}
RuntimeExceptions をキャッチすべきではないことを読みました。次に、操作を再試行するにはどうすればよいですか。サービス層で再試行する必要がありますか? 何か不足していますか?どんな助けでも大歓迎です。