When I call:
entityManager.flush()
I get the exception mentioned in the title.
I am using Hibernate JPA.
After encountering this problem myself and spending a few hours trying to get it resolved I finally found a reason for it: Spring has a bug and can't maintain transactions with @Transactional
annotation if the same class has @Service
annotation for the means of autowiring.
Once the @Service
annotation was removed from the service class in question, and an appropriate bean was declared in the XML config:
<bean id="myService" class="com.example.myapp.service.MyServiceImpl" />
the problem is gone.
Check this JIRA bug for more details.
I did all the thing as a following. My problems was with "import" tag, there are several context root like servlet-context and root-context which are not dependent on each other. It becomes clear with Spring Explorer view in STS. No JTA for Tomcat.
My advice would be universal: run Pet Clinic on your environment , How to run Spring 3.0 PetClinic in tomcat with Hibernate backed JPA or generate with Roo stub of application and try to compare your configs with referenced.
Make sure that your spring configuration includes the following line:
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />
mode
can be either proxy or aspectj and transaction-manager
has to point to your transaction manager been.
Please make sure that your handler method is declared as public
@Transactional
@RequestMapping('/test')
public String doTest() {
// do your stuff here
return 'testview';
}
I had this problem, just add @Transacctional annotation not only on the method, also in the class together with your @Service annotation.
for example:
@Service
@Transactional
public class MyService {
}
I have finally fixed this error by adding
<tx:annotation-driven mode="aspectj" transaction-manager="yourTransactionManager" />
into my application-context.xml