我使用comporsys JCA连接器从Java连接到大型机环境。 java应用程序部署在JBOSS中。以这种方式配置的连接器:
<connection-factories>
<no-tx-connection-factory>
<jndi-name>ComporsysIMSConnector</jndi-name>
<rar-name>ims_connector_port4027_CreditAuth.rar</rar-name>
<connection-definition>de.comporsys.connector.ims.ConnectionFactory</connection-definition>
<min-pool-size>1</min-pool-size>
<max-pool-size>15</max-pool-size>
<shrinking-enabled>true</shrinking-enabled>
<shrink-period-minutes>2</shrink-period-minutes>
<logging-enabled>true</logging-enabled>
<!-- <log-filename>/opt/jboss/xxxx/logs/ims_connector.log</log-filename> -->
<log-filename>D:\ims_connector.log</log-filename>
</no-tx-connection-factory>
</connection-factories>
通信必须不是100%正常,因为我在日志中看到很多错误,如下所示:
[2010-12-16 05:21:10,061] WARN (org.jboss.resource.connectionmanager.NoTxConnectionManager:738) - Pearl 1,DE82000,CGN,D5 ,10450565,768209411 - Connection error occured: org.jboss.resource.connectionmanager.NoTxConnectionManager$NoTxConnectionEventListener@3dcbef2a[state=NORMAL mc=de.comporsys.connector.ims.ManagedConnectionImpl@4743f21b handles=1 lastUse=1292++++++++++++ permit=true trackByTx=false mcp=org.jboss.resource.connectionmanager.JBossManagedConnectionPool$OnePool@7afdd15b context=org.jboss.resource.connectionmanager.InternalManagedConnectionPool@2dbbec72]
de.comporsys.connector.ims.imstoc.ImstocException: IMS Connect-Error: Return=40, Reason=59
at de.comporsys.connector.ims.imstoc.Connection.execute(Connection.java:261)
at de.comporsys.connector.ims.imstoc.IMSTOCWorkunit.execute(IMSTOCWorkunit.java:261)
at de.comporsys.connector.ims.imstoc.IMSTOCWorkunit.resumeTPipe(IMSTOCWorkunit.java:167)
at de.comporsys.connector.ims.imstoc.IMSTOCWorkunit.execute(IMSTOCWorkunit.java:98)
at de.comporsys.connector.AbstractStatement.executeRequest(AbstractStatement.java:45)
at de.comporsys.connector.ims.Statement.execute(Statement.java:101)
at com.xxx.ims.access.GenericImsTransaction.executeUCIAysnc(GenericImsTransaction.java:340)
at com.xxx.ims.access.GenericImsTransaction.executeUCI(GenericImsTransaction.java:127)
at com.xxx.ims.access.GenericImsTransaction.execute(GenericImsTransaction.java:60)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:50)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:28)
at com.xxx.ims.GenericCheckinOperation.executeTransaction(GenericCheckinOperation.java:229)
at com.xxx.ims.UpdateCheckinIMF.handleRepromptWithEnter(UpdateCheckinIMF.java:280)
at com.xxx.ims.UpdateCheckinIMF.parseOutput(UpdateCheckinIMF.java:208)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:57)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:28)
at com.xxx.ims.GenericCheckinOperation.executeTransaction(GenericCheckinOperation.java:229)
at com.xxx.ims.UpdateCheckinIMF.handleOverPaymentResponse(UpdateCheckinIMF.java:367)
at com.xxx.ims.UpdateCheckinIMF.parseOutput(UpdateCheckinIMF.java:219)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:57)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:28)
at com.xxx.ims.GenericCheckinOperation.executeTransaction(GenericCheckinOperation.java:229)
at com.xxx.ims.UpdateCheckinIMF.handleRepromptWithEnter(UpdateCheckinIMF.java:280)
at com.xxx.ims.UpdateCheckinIMF.parseOutput(UpdateCheckinIMF.java:208)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:57)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:28)
at com.xxx.ims.GenericCheckinOperation.executeTransaction(GenericCheckinOperation.java:121)
at com.xxx.ims.GenericCheckinOperation.executeTransaction(GenericCheckinOperation.java:22)
at com.xxx.core.rental.VehicleCheckinBC.executeUpdateCheckInWizardTransaction(VehicleCheckinBC.java:541)
at com.xxx.core.rental.VehicleCheckinBC.executeVehicleCheckin(VehicleCheckinBC.java:315)
at com.xxx.merlin.vehiclecheckin.VehicleCheckinMH.processMessage(VehicleCheckinMH.java:112)
at com.xxx.merlin.servlet.util.HttpRequestHandler.handleMessage(HttpRequestHandler.java:150)
at com.xxx.merlin.servlet.util.HttpRequestHandler.processRequest(HttpRequestHandler.java:88)
at com.xxx.merlin.servlet.MerlinServlet.doPost(MerlinServlet.java:22)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.xxx.scope.ServletRequestScopeFilter.doFilter(ServletRequestScopeFilter.java:37)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)
[2010-12-16 05:21:10,155] WARN (com.xxx.ims.access.GenericImsTransaction:82) - Pearl 1,DE82000,CGN,D5 ,10450565,768209411 - pac71068: ResourceException caught upon IMF execute. IMF body was:/MSKY\/RAN768209411\/MVA10450565\/DOM23021\/DOGY/G8\/COLN\/AGT82000\/OWM\/FLO\/MOPCV\/DLAY\/DOLCGN\/DOD16Dec10/0600\/TAM39058\/ACTA\/RPIY\/ENT\/AUT\/PRTN\@
javax.resource.spi.ResourceAdapterInternalException: de.comporsys.connector.NoActiveTransactionException: connection is not in transaction: de.comporsys.connector.ims.ManagedConnectionImpl@4743f21b
at de.comporsys.connector.AbstractManagedConnection.getActiveTransaction(AbstractManagedConnection.java:216)
at de.comporsys.connector.AbstractStatement.executeRequest(AbstractStatement.java:53)
at de.comporsys.connector.ims.Statement.execute(Statement.java:101)
at com.xxx.ims.access.GenericImsTransaction.executeUCIAysnc(GenericImsTransaction.java:340)
at com.xxx.ims.access.GenericImsTransaction.executeUCI(GenericImsTransaction.java:127)
at com.xxx.ims.access.GenericImsTransaction.execute(GenericImsTransaction.java:60)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:50)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:28)
at com.xxx.ims.GenericCheckinOperation.executeTransaction(GenericCheckinOperation.java:229)
at com.xxx.ims.UpdateCheckinIMF.handleRepromptWithEnter(UpdateCheckinIMF.java:280)
at com.xxx.ims.UpdateCheckinIMF.parseOutput(UpdateCheckinIMF.java:208)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:57)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:28)
at com.xxx.ims.GenericCheckinOperation.executeTransaction(GenericCheckinOperation.java:229)
at com.xxx.ims.UpdateCheckinIMF.handleOverPaymentResponse(UpdateCheckinIMF.java:367)
at com.xxx.ims.UpdateCheckinIMF.parseOutput(UpdateCheckinIMF.java:219)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:57)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:28)
at com.xxx.ims.GenericCheckinOperation.executeTransaction(GenericCheckinOperation.java:229)
at com.xxx.ims.UpdateCheckinIMF.handleRepromptWithEnter(UpdateCheckinIMF.java:280)
at com.xxx.ims.UpdateCheckinIMF.parseOutput(UpdateCheckinIMF.java:208)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:57)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:28)
at com.xxx.ims.GenericCheckinOperation.executeTransaction(GenericCheckinOperation.java:121)
at com.xxx.ims.GenericCheckinOperation.executeTransaction(GenericCheckinOperation.java:22)
at com.xxx.core.rental.VehicleCheckinBC.executeUpdateCheckInWizardTransaction(VehicleCheckinBC.java:541)
at com.xxx.core.rental.VehicleCheckinBC.executeVehicleCheckin(VehicleCheckinBC.java:315)
at com.xxx.merlin.vehiclecheckin.VehicleCheckinMH.processMessage(VehicleCheckinMH.java:112)
at com.xxx.merlin.servlet.util.HttpRequestHandler.handleMessage(HttpRequestHandler.java:150)
at com.xxx.merlin.servlet.util.HttpRequestHandler.processRequest(HttpRequestHandler.java:88)
at com.xxx.merlin.servlet.MerlinServlet.doPost(MerlinServlet.java:22)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.xxx.scope.ServletRequestScopeFilter.doFilter(ServletRequestScopeFilter.java:37)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)
at de.comporsys.connector.ExceptionHandler.handle(ExceptionHandler.java:46)
at de.comporsys.connector.AbstractStatement.executeRequest(AbstractStatement.java:55)
at de.comporsys.connector.ims.Statement.execute(Statement.java:101)
at com.xxx.ims.access.GenericImsTransaction.executeUCIAysnc(GenericImsTransaction.java:340)
at com.xxx.ims.access.GenericImsTransaction.executeUCI(GenericImsTransaction.java:127)
at com.xxx.ims.access.GenericImsTransaction.execute(GenericImsTransaction.java:60)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:50)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:28)
at com.xxx.ims.GenericCheckinOperation.executeTransaction(GenericCheckinOperation.java:229)
at com.xxx.ims.UpdateCheckinIMF.handleRepromptWithEnter(UpdateCheckinIMF.java:280)
at com.xxx.ims.UpdateCheckinIMF.parseOutput(UpdateCheckinIMF.java:208)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:57)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:28)
at com.xxx.ims.GenericCheckinOperation.executeTransaction(GenericCheckinOperation.java:229)
at com.xxx.ims.UpdateCheckinIMF.handleOverPaymentResponse(UpdateCheckinIMF.java:367)
at com.xxx.ims.UpdateCheckinIMF.parseOutput(UpdateCheckinIMF.java:219)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:57)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:28)
at com.xxx.ims.GenericCheckinOperation.executeTransaction(GenericCheckinOperation.java:229)
at com.xxx.ims.UpdateCheckinIMF.handleRepromptWithEnter(UpdateCheckinIMF.java:280)
at com.xxx.ims.UpdateCheckinIMF.parseOutput(UpdateCheckinIMF.java:208)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:57)
at com.xxx.ims.UpdateCheckinIMF.executeTransactionInternally(UpdateCheckinIMF.java:28)
at com.xxx.ims.GenericCheckinOperation.executeTransaction(GenericCheckinOperation.java:121)
at com.xxx.ims.GenericCheckinOperation.executeTransaction(GenericCheckinOperation.java:22)
at com.xxx.core.rental.VehicleCheckinBC.executeUpdateCheckInWizardTransaction(VehicleCheckinBC.java:541)
at com.xxx.core.rental.VehicleCheckinBC.executeVehicleCheckin(VehicleCheckinBC.java:315)
at com.xxx.merlin.vehiclecheckin.VehicleCheckinMH.processMessage(VehicleCheckinMH.java:112)
at com.xxx.merlin.servlet.util.HttpRequestHandler.handleMessage(HttpRequestHandler.java:150)
at com.xxx.merlin.servlet.util.HttpRequestHandler.processRequest(HttpRequestHandler.java:88)
at com.xxx.merlin.servlet.MerlinServlet.doPost(MerlinServlet.java:22)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.xxx.scope.ServletRequestScopeFilter.doFilter(ServletRequestScopeFilter.java:37)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)
Caused by: de.comporsys.connector.NoActiveTransactionException: connection is not in transaction: de.comporsys.connector.ims.ManagedConnectionImpl@4743f21b
at de.comporsys.connector.AbstractManagedConnection.getActiveTransaction(AbstractManagedConnection.java:216)
at de.comporsys.connector.AbstractStatement.executeRequest(AbstractStatement.java:53)
... 52 more
不幸的是我没有任何信息是什么意思,因为composys不再支持它。 你知道为什么会这样吗?
有一个IMSConnectionFactory初始化comporsys的东西
public static ConnectionFactory getIMSConnectionFactory() {
InitialContext context;
ConnectionFactory imsConnectionFactory = null;
try {
// Create a Properties Object and set the JNDI properties
Properties jndiProperties = new Properties();
jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, WizardContext.getInstance().getWizardProperties().getJndiClass());
// Try obtaining a JNDI context so that the application server can be queried
context = new InitialContext(jndiProperties);
// Request the Connection Factory from the application server using a JNDI context lookup
imsConnectionFactory = (ConnectionFactory) context.lookup(WizardContext.getInstance().getWizardProperties().getConnectionJndi());
Ther事务处理可能在m_IMSStatementSync.execute中,但它是一个黑盒子,因为它是第三方的东西。 IMSConnector在JBOSS中注册,我们在servlet中访问它。事务处理程序的详细信息:
for(int attempts = 1 +顽强;尝试&gt; 0;){ 试试{
// Slightly nervous re. the location of this block.
// It looks like it should be above, where it is now commented out,
// but moving it there coincided with failures.
// This may well have just been a coincidence - but it all happened at cutover time,
// so for the sake of safety I've put it back here for now.
m_IMSStatementSync = m_IMSConnection.createStatement(); /* This created by the connection factory */
m_IMSRequest = m_IMSStatementSync.createRequest();
m_IMSRequest.setTimeout(m_WizardConfiguration.getWizardTimeOut());
m_IMSRequest.setTransaction(p_Input.get_TransactionCode());
m_IMSRequest.setCommArea(p_Input);
m_IMSRequest.setLterm(p_Input.get_TSR().get_ID());
m_IMSRequest.setTimer((byte) WizardOperationConstants.IMS_CONNECT_SYNC_TIMEOUT);
// end of block
// Execute the IMS transaction
m_IMSReply = m_IMSStatementSync.execute(m_IMSRequest); /*pac7102*/
attempts = 0;
}
catch (NoActiveTransactionException e) {
if (--attempts < 1) {
tenaciousness >>= 1;
throw (e);
}
try {
wait(rnd());
}
catch (InterruptedException f) { ... }
}
答案 0 :(得分:2)
警告的原因在堆栈跟踪中非常明确:
de.comporsys.connector.NoActiveTransactionException: connection is not in transaction
....但你肯定想澄清界面是否真的有效。无论如何,连接不在事务中的警告是因为您将JCA池定义为非事务池。为了切换到事务池,它看起来应该是这样的:
<connection-factories>
<tx-connection-factory>
<jndi-name>ComporsysIMSConnector</jndi-name>
<rar-name>ims_connector_port4027_CreditAuth.rar</rar-name>
<connection-definition>de.comporsys.connector.ims.ConnectionFactory</connection-definition>
<min-pool-size>1</min-pool-size>
<max-pool-size>15</max-pool-size>
<shrinking-enabled>true</shrinking-enabled>
<shrink-period-minutes>2</shrink-period-minutes>
<logging-enabled>true</logging-enabled>
<!-- <log-filename>/opt/jboss/xxxx/logs/ims_connector.log</log-filename> -->
<log-filename>D:\ims_connector.log</log-filename>
</tx-connection-factory>
</connection-factories>
我假设这只是支持,因为警告消息表明它希望连接处于 tx-connection-factory 将要执行的事务中。
我会给它一个旋转,看看会发生什么。
答案 1 :(得分:1)
交易连接工厂申报是必要的,但还不够。
您还需要向与数据库交互的方法添加声明性事务。我建议“PROPAGATION = REQUIRED_NEW”。
答案 2 :(得分:1)
您从哪个位置发起对JCA资源的调用?
换句话说,是什么
at com.xxx.ims.access.GenericImsTransaction.executeUCIAysnc(GenericImsTransaction.java:340)
从堆栈跟踪中可以看出,这里没有涉及EJB。调用来自Servlet,然后传播到您自己的代码,直到使用JCA连接。
GenericImsTransaction
听起来像做了一些交易,但到底是什么?您应该验证是否在某处启动了JTA
事务。在您的Servlet中,您可以获得UserTransaction,这是一个启动和提交JTA事务的简单界面:
@Resource
private UserTransaction tx;
如果您使用的是JBoss AS之前的Java EE 5版本,则可以使用以下代码获取UserTransaction:
private UserTransaction getUserTransaction() {
UserTransaction tx = null;
try {
tx = (UserTransaction)new InitialContext().lookup("java:comp/UserTransaction");
} catch (NamingException e) {
throw new RuntimeException("No UserTransaction available at default location, is a transaction manager installed?", e);
}
return tx;
}
在您的callstack的某个地方,您需要致电tx.begin();
,然后再致电tx.commit()
或tx.rollback()
。
如果你能够使用EJB3,那么让你的Servlet调用无状态会话bean(@Stateless)然后让这个bean调用你的其他代码会更容易。然后,您将自动拥有一个事务,容器将负责提交或回滚。如果您还没有使用EJB,可能很难将现有代码改进为使用它们,并且手动JTA代码可能更容易插入。