搜索

此内容没有您所选择的语言版本。

4.9. Global Transactions

download PDF
Global or client XA transactions allow the JBoss Data Virtualization JDBC API to participate in transactions that are beyond the scope of a single client resource. For this, use the org.teiid.jdbc.TeiidDataSource class for establishing connections.
When the data source class is used in the context of a user transaction in an application server, such as JBoss, WebSphere, or Weblogic, the resulting connection will already be associated with the current XA transaction. No additional client JDBC code is necessary to interact with the XA transaction.
The following code demonstrates usage of UserTransactions.
UserTransaction ut = context.getUserTransaction();
try {
	ut.begin();
	Datasource oracle = lookup(...)
	Datasource teiid = lookup(...)
	Connection c1 = oracle.getConnection();
	Connection c2 = teiid.getConnection();
	// do something with Oracle connection
	// do something with Teiid connection
	c1.close();
	c2.close();
	ut.commit();
} catch (Exception ex) {
	ut.rollback();
}
The following code demonstrates manual usage of XA transactions.
XAConnection xaConn = null;
XAResource xaRes = null;
Connection conn = null;
Statement stmt = null;

try 
{
   xaConn = <XADataSource instance>.getXAConnection();
   xaRes = xaConn.getXAResource();
   Xid xid = <new Xid instance>;
   conn = xaConn.getConnection();
   stmt = conn.createStatement();

   xaRes.start(xid, XAResource.TMNOFLAGS);
   stmt.executeUpdate("insert into …");
   // other statements on this connection or other resources enlisted in this transaction
   // ...
   xaRes.end(xid, XAResource.TMSUCCESS);
        
   if (xaRes.prepare(xid) == XAResource.XA_OK) 
   {
      xaRes.commit(xid, false);
   }
}
catch (XAException e) 
{
   xaRes.rollback(xid);
} 
finally 
{
  // clean up code
  // ...
}
With the use of global transactions, multiple XAConnections may participate in the same transaction. It is important to note that the JDBC XAResource isSameRM() method only returns true if connections are made to the same server instance in a cluster. If the JBoss Data Virtualization connections are to different server instances then transactional behavior may not be the same as if they were to the same cluster member. For example, if the client transaction manager uses the same XID for each connection, duplicate XID exceptions may arise from the same physical source accessed through different cluster members. If the client transaction manager uses a different branch identifier for each connection, issues may arise with sources that lock or isolate changes based upon branch identifiers.
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.