我正在开发一个使用 JPA(Hibernate) 作为后端的应用程序。我对每个实体都有一个 DAO 和 JPADAO,并且我的流程将多个实体更新为一个工作单元,因此我决定使用 JTA,因为它允许我控制事务,所以这就是我的代码的外观 persistence.xml
<persistence-unit name="Pub" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:comp/env/jdbc/dcapps</jta-data-source>
<property name="hibernate.dialect" value="org.hibernate.dialect.InformixDialect"/>
<property name="transactionSynchronizationRegistryName" value="java:comp/env/TransactionSynchronizationRegistry"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JOTMJtaPlatform"/>
web.xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/dcapps</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<resource-env-ref>
<description>JTA transaction manager</description>
<resource-env-ref-name>jta/UserTransaction</resource-env-ref-name>
<resource-env-ref-type>javax.transaction.UserTransaction</resource-env-ref-type>
</resource-env-ref>
<resource-env-ref>
<description>JTA Transaction Synchronization Registry</description>
<resource-env-ref-name>TransactionSynchronizationRegistry</resource-env-ref-name>
<resource-env-ref-type>javax.transaction.TransactionSynchronizationRegistry
</resource-env-ref-type>
</resource-env-ref>
content.xml(Tomcat 7)
<Resource name="jdbc/dcapps" auth="Container" type="javax.sql.DataSource"
maxActive="20" maxIdle="10" maxWait="-1"
username="dcapps" password="dcapps" driverClassName="com.informix.jdbc.IfxDriver"
url="jdbc:informix-sqli://fddb.fd.gtwy.dcn:7101/fd_test:INFORMIXSERVER=fd_ecf"/>
<Resource
name="TransactionSynchronizationRegistry"
auth="Container"
type="javax.transaction.TransactionSynchronizationRegistry"
factory="org.objectweb.jotm.TransactionSynchronizationRegistryFactory"/>
<Transaction
factory="org.objectweb.jotm.UserTransactionFactory"
jotm.timeout="60"/>
Process.java
Context initContext = new InitialContext();
utx = (UserTransaction)initContext.lookup("java:comp/UserTransaction");
dktPartDAO.getEntityManager().joinTransaction();
dkttextDAO.getEntityManager().joinTransaction();
reqHDAO.getEntityManager().joinTransaction();
reqDDAO.getEntityManager().joinTransaction();
costDAO.getEntityManager().joinTransaction();
reportDDAO.getEntityManager().joinTransaction();
caseflagDAO.getEntityManager().joinTransaction();
redStatusDAO.getEntityManager().joinTransaction();
pcdktPartDAO.getEntityManager().joinTransaction();
pcDkttextDAO.getEntityManager().joinTransaction();
utx.commit();
现在,当我尝试开始事务并提交对实体(多个)的更改时,它会抛出异常并调用 utx.rollback 但它不会回滚,它仅部分提交了事务,并非所有实体都已保存
Exception
java.lang.IllegalStateException: Cannot get Transaction for rollback
at org.objectweb.jotm.Current.rollback(Current.java:486)
at pub.jpa.dto.support.PubDProcess.processRecords(PubDProcess.java:397)
org.omg.CORBA.BAD_INV_ORDER: The Servant has not been associated with an ORB instance vmcid: 0x0 minor code: 0 completed: No
at org.omg.PortableServer.Servant._get_delegate(Unknown Source)
at org.omg.PortableServer.Servant._poa(Unknown Source)
at org.objectweb.jotm._SubCoordinator_Tie.deactivate(Unknown Source)
at com.sun.corba.se.impl.javax.rmi.CORBA.Util.cleanUpTie(Unknown Source)
at com.sun.corba.se.impl.javax.rmi.CORBA.Util.unexportObject(Unknown Source)
at javax.rmi.CORBA.Util.unexportObject(Unknown Source)
at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.unexportObject(Unknown Source)
at javax.rmi.PortableRemoteObject.unexportObject(Unknown Source)