OpenJPA – 交易管理不可用……(Fuse ESB)
我在Fuse ESB中使用JPA的RESOURCE_LOCAL
事务类型时遇到问题。
我也不完全了解JTA
或RESOURCE_LOCAL
对我来说是否更好。
我的persistence.xml:
org.apache.openjpa.persistence.PersistenceProviderImpl osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/invDataSource) osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/invDataSource) com.company.service.Invoice
我在blueprint.xml中的bean:
我的代码:
entityManager.getTransaction().begin(); entityManager.persist(a); entityManager.persist(b); entityManager.getTransaction().commit();
exception,在我的persistence.xml中使用事务类型RESOURCE_LOCAL
:
java.lang.IllegalStateException: Transaction management is not available for container managed EntityManagers.
我也尝试在我的persistence.xml中使用事务类型JTA
,但后来我收到了OptimisticLockException
。
我不确定哪种方法更好(RESOURCE_LOCAL或JTA),但主要的是在我的代码对象中a
和b
需要在事务中持久化(全部或全部)。
我正在使用Fuse ESB(camel,cxf等)。
感谢您的任何提示或帮助。
好的,这是答案。
首先,关于JPA Concepts和Aries JPA Container的 2个不错的链接
RESOURCE_LOCAL
transaction-type="RESOURCE_LOCAL"
确实是自我管理的持久性,代码应该是这样的:
EntityManager entityManager = entityManagerFactory.createEntityManager(); ... entityManager.getTransaction().begin(); entityManager.persist(a); entityManager.persist(b); entityManager.getTransaction().commit();
使用entityManager.getTransaction()
和entityManager.flush()
都会导致exception,因为我已经指定了
。
正确的方法是使用
和EntityManagerFactory
。
JTA
另一方面, transaction-type="JTA"
是“容器管理”持久性:
entityManager.persist(a); entityManager.persist(b);
它应该使用
和EntityManager
在蓝图中配置。