1
我正在開發一個應用程序與JPA(休眠)作爲後端。 我有一個DAO和JPADAO爲每個實體和我的過程更新多個實體作爲一個工作單元,所以我決定使用JTA,因爲它允許我控制交易,所以這裏是我的代碼看起來如何 persistence.xmlJPA(休眠),JTA(JOTM)和Tomcat
<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但它不會回滾它已經COMMITED交易僅部分不是全部實體都被保存
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)