"https://stackoverflow.com/questions/25821579/transactionrequiredexception-executing-an-update-delete-query" This doesn't help my answer as there is no condition in which one method is working and other not also tried all the method present in that.
first, i'm calling the below method which runs fine with no errors. The error replicates only after calling this method.
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void upgradeToSuperUser(UserBO userToBeMadeSuperUser, UserBO loggedInUser) {
javax.persistence.Query query = entityManager
.createNativeQuery("Update M_USERS SET ROLE='SUPERUSER' where ID=:newSuperUserId");
query.setParameter("newSuperUserId", userToBeMadeSuperUser.getId());
query.executeUpdate();
javax.persistence.Query query0 = entityManager
.createNativeQuery("Update M_USERS SET ROLE='TEACHER' where ID=:oldSuperUserId");
query0.setParameter("oldSuperUserId", loggedInUser.getId());
query0.executeUpdate();
javax.persistence.Query query1 = entityManager
.createNativeQuery("Update M_SCH_USERS SET USER_ID=:oldSuperUserId where USER_ID=:newSuperUserId");
query1.setParameter("oldSuperUserId", loggedInUser.getId());
query1.setParameter("newSuperUserId", userToBeMadeSuperUser.getId());
query1.executeUpdate();
javax.persistence.Query query2 = entityManager.createNativeQuery(
"Update M_SCH_USERS SET SUPERUSER_ID=:newSuperUserId where SUPERUSER_ID=:oldSuperUserId");
query2.setParameter("oldSuperUserId", loggedInUser.getId());
query2.setParameter("newSuperUserId", userToBeMadeSuperUser.getId());
query2.executeUpdate();
javax.persistence.Query query3 = entityManager
.createNativeQuery("Update M_TEACHERS set ID = :oldSuperUserId where ID=:newSuperUserId");
query3.setParameter("oldSuperUserId", loggedInUser.getId());
query3.setParameter("newSuperUserId", userToBeMadeSuperUser.getId());
query3.executeUpdate();
javax.persistence.Query query4 = entityManager
.createNativeQuery("Update M_TEACHERS set IS_MANAGE_USERS = 0 where ID= :oldSuperUserId");
query4.setParameter("oldSuperUserId", loggedInUser.getId());
query4.executeUpdate();
}
After this when i try to call the below method it throws "javax.persistence.TransactionRequiredException: Executing an update/delete query" error i was upgrading my application and recently found this error earlier it was working fine
@Override
// @Modifying
@Transactional //(propagation = Propagation.REQUIRED)
public void updateLastlogin(Long id, Date lastLogin, String signedInWith) {
long startTime = System.currentTimeMillis();
// entityManager.getTransaction().begin();
javax.persistence.Query query = entityManager.createNativeQuery(
"Update M_USERS SET LAST_LOGIN_ON = :lastLogin, SIGNED_IN_WITH = :signedInWith" + " where ID=:id");
query.setParameter("lastLogin", lastLogin);
query.setParameter("signedInWith", signedInWith);
query.setParameter("id", id);
//entityManager.joinTransaction();
try{
query.executeUpdate();
// entityManager.getTransaction().commit();
}
catch(Exception e){
System.out.println("In Exception");
System.out.println("--------");
System.out.println(e.getMessage());
System.out.println("!!!!!!!!!!!");
System.out.println(e.getCause());
System.out.println("$$$$$");
System.out.println(e.toString());
System.out.println("printing stacktrace");
e.printStackTrace();
}
logger.info("Time to execute query updateLastLogin:: " + ((System.currentTimeMillis() - startTime)));
}
I have tried all the answers(as you can see in my comments) that are present online none of them are working for me. The imports are correct and refer to only "import org.springframework.transaction.annotation.Transactional;"
Both methods are present in the same service class