26

When I call:

entityManager.flush() 

I get the exception mentioned in the title.

I am using Hibernate JPA.

1
  • show your web.xml , and your servlets files Commented Apr 22, 2011 at 4:13

13 Answers 13

17

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Well saying this is a bug is not totally correct, the link you have given already has given good explanation why this is happening - the Service class getting instantiated as a bean in both appContext.xml and spring-servlet.xml. It should only get 'bean'ed in appContext.xml otherwise it won't have transaction support.
@HarshalWaghmare You saved my bottom twice. I encountered the same error twice and was eagerly trying to find your comment again :D. Finally, hurray. Told servlet-context just to scan controllers.. everything works like charm.
5

Ensure that you have an active transaction when this statement executes. If you are using JPA use EntityManager.getTransaction().begin(). This is assuming that you are using JPA outside a JTA transaction scope.

If you are running the application inside a container with JTA support you can also use JTA UserTransaction to manage transactions.

1 Comment

"Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead"
4

My Problem was to do with the way that I setup the <tx:annotation-driven/> Element in my context definition -

Originally I had load time weaving enabled (not knownley) that read <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/> and by simply removing the 2nd attribute - everything worked (took 2 hours of head banging though). I believe the 2nd element relates to the @Configurable sterotype but can let other (smarter) people explain the difference & why one would work & the other does does not.. Hope this helps...

working definition= <tx:annotation-driven transaction-manager="transactionManager"/>

Comments

3

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 { } 

2 Comments

The @Transactional annotation to the class solved my problem.
How exactly does it solve the problem? Why @Transactional on the method is not enough?
3

Spring 4.3.1 / Hibernate 4.2.21

My configuration was 100% Java code with no hibernate or spring xml documents (eg context.xml, persistence.xml etc). The issue was the EntityManagerFactory I was passing to the TransactionManager, see the below configuration in the transactionManager method.

@Configuration @EnableTransactionManagement public class HibernateConfiguration2 { @Bean public DataSource dataSource() { return ...; // Build a basic datasource } @Bean @Autowired public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) { LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean(); entityManagerFactory.setDataSource(dataSource); entityManagerFactory.setPackagesToScan("nz.co.mark"); entityManagerFactory.setPersistenceProviderClass(org.hibernate.ejb.HibernatePersistence.class); return entityManagerFactory; } @Bean @Autowired public EntityManager entityManager(LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean) { EntityManager em = localContainerEntityManagerFactoryBean.getNativeEntityManagerFactory().createEntityManager(); em.setFlushMode(FlushModeType.AUTO); return em; } @Bean @Autowired public JpaTransactionManager transactionManager(LocalContainerEntityManagerFactoryBean emf) throws Exception { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(emf.getObject()); // The below line would generate javax.persistence.TransactionRequiredException: no transaction is in progress // transactionManager.setEntityManagerFactory(emf.getNativeEntityManagerFactory()); return transactionManager; } 

Comments

2

Ensure that you have an active transaction when this statement executes. If you are using JPA use EntityManager.getTransaction().begin(). This is assuming that you are using JPA outside a JTA transaction scope.

Comments

2

I had the same problem... spent some hours until I found the reason finally. It was just one line of code that caused the exception in my case...

In my mvc-core-config.xml the following line was the reason:

<context:component-scan base-package="com.my.package.application" /> 

After I changed it as follows, everything worked again:

<context:component-scan base-package="com.my.package.application.controller" /> 

So I guess the scanning of all my application packages instead of just my @Controller classes lead to the problem like @harshal-waghmare mentioned in his post to another answer.

Comments

1

Please make sure that your handler method is declared as public

@Transactional @RequestMapping('/test') public String doTest() { // do your stuff here return 'testview'; } 

Comments

0

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.

Comments

0

Same was happening to me using spring 3.0.0 / 3.0.3. Data was persisted in MySQL from JUnit but not from the tomcat server. After so much work I gave up on RESOURCE_LOCAL for JTA.

This worked for me http://erich.soomsam.net/2007/04/24/spring-jpa-and-jta-with-hibernate-and-jotm/ It uses JTA and depends on JOTM.

Comments

0

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.

Comments

0

For JBoss 4.0 and Hibernate, I fixed this problem by adding some transaction manager properties to my EntityManagerFactoryBean definition:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="xaDs" /> <property name="jpaProperties"> <props> <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory </prop> <prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup </prop> </props> </property> 

I found the soluton on this message board thread.

Comments

0

I have finally fixed this error by adding

<tx:annotation-driven mode="aspectj" transaction-manager="yourTransactionManager" /> 

into my application-context.xml

1 Comment

Your answer contradicts Ian's answer. He removed the mode="aspectj" parameter.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.