XA Datasource 与 Non-XA Datasource 区别

An XA transaction, in the most general terms, is a "global transaction"that may span multiple resources. A non-XA transaction always involvesjust one resource.

An XA transaction involves a coordinating transaction manager, withone or more databases (or other resources, like JMS) all involved in asingle global transaction. Non-XA transactions have no transactioncoordinator, and a single resource is doing all its transaction workitself (this is sometimes called local transactions).

XA transactions come from the X/Open group specification ondistributed, global transactions. JTA includes the X/Open XA spec, inmodified form.

Most stuff in the world is non-XA - a Servlet or EJB or plain oldJDBC in a Java application talking to a single database. XA getsinvolved when you want to work with multiple resources - 2 or moredatabases, a database and a JMS connection, all of those plus maybe aJCA resource - all in a single transaction. In this scenario, you'llhave an app server like Websphere or Weblogic or JBoss acting as theTransaction Manager, and your various resources (Oracle, Sybase, IBM MQJMS, SAP, whatever) acting as transaction resources. Your code can thenupdate/delete/publish/whatever across the many resources. When you say"commit", the results are commited across all of the resources. Whenyou say "rollback", _everything_ is rolled back across all resources.

The Transaction Manager coordinates all of this through a protocolcalled Two Phase Commit (2PC). This protocol also has to be supportedby the individual resources.

In terms of datasources, an XA datasource is a data source that canparticipate in an XA global transaction. A non-XA datasource generallycan't participate in a global transaction (sort of - some peopleimplement what's called a "last participant" optimization that can letyou do this for exactly one non-XA item).

For more details - see the JTA pages on java.sun.com. Look at theXAResource and Xid interfaces in JTA. See the X/Open XA DistributedTransaction specification. Do a google source on "Java JTA XAtransaction".


XA 连接与非 XA 连接不同。一定要记住 XA 连接参与了 JTA 事务。这意味着 XA 连接不支持 JDBC 的自动提交功能。同时,应用程序一定不要对 XA 连接调用 java.sql.Connection.commit() 或者 java.sql.Connection.rollback() .相反,应用程序应该使用 UserTransaction.begin()、 UserTransaction.commit() 和 serTransaction.rollback() .

`AtomikosNonXADataSourceBean` 是 Atomikos 提供的一个用于 Spring 框架的 XA 数据源,它主要用于那些不需要支持 XA(两阶段提交)分布式事务的场景。如果需要额外的事务管理,特别是涉及到分布式事务,你需要做以下几个步骤: 1. **添加Atomikos依赖**:首先,在你的项目中引入Atomikos的Spring整合包,比如 `atomikos-spring` 或者 `atomikos-jta-api`。 2. **配置AtomikosXADatasource**:在Spring的XML配置文件(`.xml` 或者YAML)中,创建一个 AtomikosNonXADataSourceBean 对象,如下所示: ```xml <bean id="atomikosDataSource" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean"> <!-- 连接参数 --> <property name="driverClassName" value="your_driver_class"/> <property name="url" value="your_jdbc_url"/> <property name="username" value="your_username"/> <property name="password" value="your_password"/> <!-- Atomikos配置 --> <property name="xaDataSourceClassName" value="com.atomikos.jdbc.xa.XAPoolDataSource"/> <property name="xaResourceName" value="your_resource_name"/> <!-- 更多配置项,如xaRecoveryMechanismClass属性,根据实际情况设置--> </bean> ``` 3. **配置事务管理器**:如果你还需要一个全局的事务管理器,可以在Spring配置中添加`AtomikosTransactionManager`,并将其关联到你的数据源: ```xml <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager" ref="atomikosDataSource" /> </bean> ``` 4. **启用事务管理**:在需要事务控制的地方使用`@Transactional`注解,或者手动开启事务。 5. **处理分布式事务**:如果需要在分布式环境中处理事务,可能需要用到`PlatformTransactionManager`接口的回调函数,比如`beforeCompletion()``afterCompletion()`。 **相关问题--:** 1. 什么是XAPoolDataSource? 2. 如何在Spring中启用声明式事务管理? 3. 如果业务中不存在分布式事务,AtomikosXADatasourceBean是否必需?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值