在 Jboss的docs\examples\jca中有各种数据库的数据源配置模版。数据源配置文件总是以*-ds.xml文件命名,部署器是jboss- jca.sar中的XSLSubDeployer。这其中的数据源都使用的是local-tx-datasource定义,平常用的时候也没注意它到底是 什么意思,只是简单的修改其中的参数。
*-ds.xml文件中的XML结构如下:
mbean
定义数据源用到的任何MBean服务,并且这些服务要在jboss-service.xml文件中定义。
local-tx-datasource
这是最常用的(至少我是这样),配置LocalTxConnectionManager服务,该ConnectionManager只支持“本地事 务”,确切的说,只支持一个数据源的事务,不支持分布事务,本地不是说数据源指定的数据库在本地,可以在任何地方,但local-tx- datasource不能在一个事务中访问两个数据源,即使这两个数据源配置是一样的。也就是一个事务中不能有两个Connection,如果有两个数据 源,后面打开的数据源无法打开链接,并且出现异常:
2009-07-02 12:08:30,892 WARN [com.arjuna.ats.jta.logging.loggerI18N] [com.arjuna.ats.internal.jta.transaction.arjunacore.lastResource.disallow]
[com.arjuna.ats.internal.jta.transaction.arjunacore.lastResource.disallow] Adding multiple last resources is disallowed. Current resource is org.jboss.resource.connectionmanager.TxConnectionManager$LocalXAResource@7d3b91b
2009-07-02 12:08:30,902 DEBUG
[org.hibernate.util.JDBCExceptionReporter] Cannot open connection [???] org.jboss.util.NestedSQLException: Could not enlist in transaction on entering meta-aware object!; – nested throwable:
(javax.transaction.SystemException: java.lang.Throwable: Unabled to enlist resource, see the previous warnings. tx=TransactionImple < ac, BasicAction: 7f000001:dcdc:4a4c3005:35 status: ActionStatus.ABORT_ONLY >); – nested throwable:
(org.jboss.resource.JBossResourceException: Could not enlist in transaction on entering meta-aware object!; – nested throwable: (javax.transaction.SystemException: java.lang.Throwable: Unabled to enlist resource, see the previous warnings. tx=TransactionImple < ac, BasicAction: 7f000001:dcdc:4a4c3005:35 status: ActionStatus.ABORT_ONLY >)) at
org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource
.java:94)
at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider. getConnection(InjectedDataSourceConnectionProvider.java:47) at
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:423) at
org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
LocalTxConnectionManager会确保所有的链接都是同一个ManagedConnection(所以两个数据源就是两个 Connection就不行),当有一个链接请示时会检查该链接是否存在,存在则用之。
包含的元素
xa-datasource
该数据源配置XATxConnectionManager服务。XA事务也就是通常所说的分布式事务。在一个事务中可以有多个资源(如数据源),但 这些资源必须能够支持XA事务,支持XA事务的数据库在docs\examples\jca目录中都有一个xa-ds.xml文件,但mysql没 有,mysql是支持XA事务的(我的是mysql 5, jdbc驱动是5.1.7,可以用 xa start ‘test’,’test’; xa end ‘test’,’test’; 来测试一下是否支持)
XA与JTA
XA是Open Group提出的分布式事务模型。JTA是sun提出的java事务API(Java Transaction API),也支持XA,或者说实现了XA,使用JTA可以与任何其它支持XA事务的应用集成。
这里有份openlink的文档:http://docs.openlinksw.com/mt/xamt.html
no-tx-datasource
该数据源的链接管理噐是NoTxConnectionManager,不支持事务
ha-local-tx-datasource
同local-tx-datasource,区别是提供了一些容错能力允许Jboss从数据库错误中恢复。不是很理解
ha-xa-datasource
同ha-tx-datasource,区别是提供了一些容错能力允许Jboss从数据库错误中恢复。不是很理解