OpenJPA - 交易管理不可用......(Fuse ESB)
我在使用Fuse ESB中的JPA的 RESOURCE_LOCAL
交易类型时遇到问题。
I'm having trouble with RESOURCE_LOCAL
transaction type for JPA in Fuse ESB.
我也不喜欢完全理解 JTA
或 RESOURCE_LOCAL
对我来说是否更好。
I also don't have a complete understanding of whether JTA
or RESOURCE_LOCAL
is better for me.
我的persistence.xml:
My persistence.xml :
<persistence-unit name="invoicePersistence" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/invDataSource)</jta-data-source>
<non-jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/invDataSource)</non-jta-data-source>
<class>com.company.service.Invoice</class>
<!-- etc... -->
</persistence-unit>
我在blueprint.xml中的bean:
My beans in blueprint.xml :
<reference id="invDataSource" interface="javax.sql.DataSource" filter="(datasource.name=invDataSource)"/>
<bean id="invoiceDao" class="com.company.project.InvoiceDao">
<jpa:context unitname="invoicePersistence" property="entityManager"/>
<tx:transaction method="*" value="Required" />
</bean>
我的代码:
entityManager.getTransaction().begin();
entityManager.persist(a);
entityManager.persist(b);
entityManager.getTransaction().commit();
例外,使用事务类型 RESOURCE_LOCAL
在我的persistence.xml中:
And the exception, using transaction-type RESOURCE_LOCAL
in my persistence.xml:
java.lang.IllegalStateException: Transaction management is not available for container managed EntityManagers.
我也尝试使用交易类型 JTA
在我的persistence.xml中然后我收到 OptimisticLockException
。
I also tried using transaction-type JTA
in my persistence.xml but then I received OptimisticLockException
.
我不确定哪种方法更好(RESOURCE_LOCAL或JTA)但主要的是在我的代码对象 a
和 b
需要在事务中持久化(全部或全部)。
I'm not sure which approach is better (RESOURCE_LOCAL or JTA) but the main thing is that in my code object a
and b
need to be persisted in a transaction (all or nothing).
我在Fuse ESB(驼峰,cxf等)中运行。
I'm running in Fuse ESB (camel, cxf, etc).
感谢您的任何提示或帮助。
Thanks for any tips or help.
好的,这是答案。
首先, JPA Concepts 和 Aries JPA容器
RESOURCE_LOCAL
transaction-type =RESOURCE_LOCAL
确实自我管理持久性,代码应该是这样的:
transaction-type="RESOURCE_LOCAL"
is indeed self-managed persistence, and the code should be like this:
EntityManager entityManager = entityManagerFactory.createEntityManager();
...
entityManager.getTransaction().begin();
entityManager.persist(a);
entityManager.persist(b);
entityManager.getTransaction().commit();
使用 entityManager.getTransaction()
和 entityManager.flush()
两者都导致异常,因为我指定了< jpa:context>
。
Using entityManager.getTransaction()
and entityManager.flush()
both caused exceptions, because I had specified <jpa:context>
.
正确的方法是使用< jpa:unit>
和 EntityManagerFactory
。
<bean id="invoiceDao" class="com.company.project.InvoiceDao">
<jpa:unit unitname="invoicePersistence" property="entityManagerFactory"/>
</bean>
JTA
另一方面 transaction-type =JTA
是'容器管理'持久性:
On the other hand transaction-type="JTA"
is 'container-managed' persistence:
entityManager.persist(a);
entityManager.persist(b);
它应该用蓝图配置< jpa:context>
和 EntityManager
。