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

SpringHibernateJpaJpa 2.0

Spring Problem Overview


This post is in continuation of https://stackoverflow.com/questions/17856917/jpa-how-to-get-the-value-from-database-after-persist

When I execute the following I am getting following exception, how can I resolve this?

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

DAOImpl code

public void create(Project project) {
        entityManager.persist(project);
        entityManager.getTransaction().commit();
        project = entityManager.find(Project.class, project.getProjectId());
        entityManager.refresh(project);
        System.out.println("Id    -- " + project.getProjectId());
            System.out.println("no -- " + project.getProjectNo());
    }

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<bean id="DataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="username" value="scott" />
		<property name="password" value="tiger" />
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
		<property name="url" value="jdbc:oracle:thin:@myserver:1521:ORCL" />
	</bean>

	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="DataSource" />
		<property name="packagesToScan" value="test.entity" />
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="showSql" value="true" />
				<property name="generateDdl" value="false" />
				<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
			</bean>
		</property>
	</bean>

	<context:component-scan base-package="test.net" />
               
    <tx:annotation-driven transaction-manager="transactionManager"/> 
    
     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>         
                           
     <context:annotation-config/>

</beans>

Spring Solutions


Solution 1 - Spring

I guess the problem here is that although you have defined the bean for the transaction manager, you haven't annotated the create() method with @Transactional which enables spring transactions.

Also remove the entityManager.getTransaction().commit(); statement as now all the transaction management will be handled by spring, if you leave the statement as it is then you will get the same error again.

Solution 2 - Spring

Injecting EntityManagerFactory instead of EntityManager and javax.transaction.Transactional annotation on method solved my issue as shown below.

//Autowire EntityManagerFactory
@PersistenceUnit(unitName = "readwrite.config")
private EntityManagerFactory entityManagerFactory;


//Use below code on create/update
EntityManager entityManager = entityManagerFactory.createEntityManager();

entityManager.getTransaction().begin();
if (!ObjectUtils.isEmpty(entity) && !entityManager.contains(entity)) {
   entityManager.persist(entity);
   entityManager.flush();
}
entityManager.getTransaction().commit();

Solution 3 - Spring

you need to remove entityManager.getTransaction().begin() statement and annotate method using @Transactional which enables spring transactions

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJacobView Question on Stackoverflow
Solution 1 - SpringSiddhartha NegiView Answer on Stackoverflow
Solution 2 - SpringAnil KonduruView Answer on Stackoverflow
Solution 3 - SpringSyam KumarView Answer on Stackoverflow