当我进入/ confirmation-account链接时,在tomcat控制台中我可以看到if和else块也被执行了。我可以看到:
从ColorConsoleHelper.getGreenLog(" loginView")和ColorConsoleHelper.getGreenLog(" confirmationAccountView")
打印这真是一种奇怪的行为。为什么呢?
@RequestMapping(value = "/confirmation-account", method = RequestMethod.GET)
@Transactional
public ModelAndView displayConfirmationAccountPage(ModelAndView modelAndView, @RequestParam Map<String, String> requestParams) {
final int ACTIVE_USER = 1;
// find the user associated with the confirmation token
UserEntity userEntity = userService.findUserByConfirmationToken(requestParams.get("token"));
// this should always be non-null but we check just in case
if (userEntity!=null) {
// set the confirmation token to null so it cannot be used again
userEntity.setConfirmationToken(null);
// set enabled user
userEntity.setEnabled(ACTIVE_USER);
// save data: (token to null and active user)
saveAll(userEntity.getTrainings());
/*
RedirectAttributes won't work with ModelAndView but returning a string from the redirecting handler method works.
*/
modelAndView.addObject("successMessage", "Konto zostało pomyślnie aktywowane!");
modelAndView.setViewName("loginView");
ColorConsoleHelper.getGreenLog("loginView");
} else {
ColorConsoleHelper.getGreenLog("confirmationAccountView");
modelAndView.addObject("errorMessage", "Link jest nieprawidłowy...");
modelAndView.setViewName("confirmationAccountView");
}
return modelAndView;
}
public void saveAll(List<TrainingUserEntity> trainingUserEntityList) {
for ( TrainingUserEntity trainingUserEntity : trainingUserEntityList) {
entityManagerService.mergeUsingPersistenceUnitB(trainingUserEntity);
}
}
public void mergeUsingPersistenceUnitB(Object object) {
EntityManager entityManager = getEntityManagerPersistenceUnitB();
EntityTransaction tx = null;
try {
tx = entityManager.getTransaction();
tx.begin();
entityManager.merge(object);
tx.commit();
}
catch (RuntimeException e) {
if ( tx != null && tx.isActive() ) tx.rollback();
throw e; // or display error message
}
finally {
entityManager.close();
}
}
答案 0 :(得分:0)
以下解决方案&amp;说明:
由于/ confirmation-account链接被调用两次,因此在控制器中注释的动态代理和@Transactional方法导致的内容必须检查调用了多少displayConfirmationAccountPage方法。这是解决方法。
您认为对@Transactional控制器方法进行注释有什么好处?