不允许在共享 EntityManager 上创建事务 - 使用 Spring 事务或 EJB CMT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17860696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT
提问by J?cob
This post is in continuation of 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
DAOImplcode
DAOImpl代码
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
应用上下文.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>
回答by Siddhartha Negi
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 @Transactionalwhich enables spring transactions.
我猜这里的问题是,虽然您已经为事务管理器定义了 bean,但您还没有注释 create() 方法,@Transactional用于启用 spring 事务。
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.
同时删除该entityManager.getTransaction().commit();语句,因为现在所有的事务管理都将由 spring 处理,如果您保留该语句,那么您将再次收到相同的错误。
回答by Anil Konduru
Injecting EntityManagerFactory instead of EntityManager and javax.transaction.Transactional annotation on method solved my issue as shown below.
在方法上注入 EntityManagerFactory 而不是 EntityManager 和 javax.transaction.Transactional 注释解决了我的问题,如下所示。
//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();
回答by Syam Kumar
you need to remove entityManager.getTransaction().begin() statement and annotate method using @Transactional which enables spring transactions
您需要使用启用 spring 事务的 @Transactional 删除 entityManager.getTransaction().begin() 语句和注释方法

