When I call the below method twice it results in "javax.persistence.TransactionRequiredException: Executing an update/delete query
" error. I confirm that the first-time error is not there only when the method call is complete i'm calling this method.
the import used is "org.springframework.transaction.annotation.Transactional;" For the first time this method "TransactionSynchronizationManager.isActualTransactionActive()" return true and the second time it returns false.
I have followed all the approaches present in "https://stackoverflow.com/questions/25821579/transactionrequiredexception-executing-an-update-delete-query" but none helped.
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void upgradeToSuperUser(UserBO userToBeMadeSuperUser, UserBO loggedInUser) {
System.out.println("Transactional Message "+TransactionSynchronizationManager.isActualTransactionActive());
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();
}
edit 1:- From where its called this method is written in action class
public String upgradeToSuperUser() {
UserBO userToBeMadeSuperUser = userManager.getUserByRefId(userRefID);
UserBO currentSuperUser = userManager
.findSuperUserForLoggedInUser(userToBeMadeSuperUser
.getSchoolBO().getId());
if (Role.getValue(currentSuperUser.getRole()).equals(
Role.getValue(Role.SUPERUSER))
&& Role.getValue(userToBeMadeSuperUser.getRole()).equals(
Role.getValue(Role.TEACHER))) {
userManager.upgradeToSuperUser(userToBeMadeSuperUser,
currentSuperUser);
EDIT 2:- I'm upgrading my spring 3 to 5, hibernate 3 to 5 with aspectj version v1.8.10.
<aspectj.version>1.8.10</aspectj.version>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<outxml>true</outxml>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>1.8</source>
<target>1.8</target>
<complianceLevel>1.8</complianceLevel>
</configuration>
</plugin>
and my applicationContext.xml for transactions looks like
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
</bean>
<tx:annotation-driven mode="aspectj"
transaction-manager="transactionManager" proxy-target-class="true" />
EDIT 3:- entity manager in applicationContext.xml
<bean id="entityManager"
class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
</bean>