-1

When I go to /confirmation-account link, in tomcat console I can see that if and else block is also executed. I can see:

print from ColorConsoleHelper.getGreenLog("loginView") and from ColorConsoleHelper.getGreenLog("confirmationAccountView")

This is really strange behavior. Why?

 @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();
     }
 }

1 Answers1

0

Below solution & explanation:

Because of /confirmation-account link is invoke twice, what is caused by dynamic proxy and @Transactional method annotated in controller It is mandatory to check how many displayConfirmationAccountPage method is invoked. It is workaround.

What do you think it is good or not to annotated @Transactional controller method?