先骂一下狗日的微软。让我费了一翻周折。
1.mssql的jdbc驱动:之前一直使用三个jar的那个驱动。但在使用seam-gen生成项目时要求指定jar时只让指定一个文件。所以无奈上网查找,发现已经有新版的JDBC驱动是单个文件的,但是又分为jdk1.6的和1.6的,使用之后又发现驱动的包名被换了。由“com.microsoft.jdbc.sqlserver”改为“com.microsoft.sqlserver.jdbc”,我这才明白为什么好多java的工具提供的默认驱动名为什么会是反的,
2.由于要使用多个数据库所以要作用XA数据源,用来支持JTA的分布式事务,我用的是jboss as 5.1 所以找到mssql-xa-ds.xml来修改,改好后如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE datasources
PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
"http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd">
<datasources>
<xa-datasource>
<jndi-name>house_mssql_xa_datasource</jndi-name>
<isSameRM-override-value>false</isSameRM-override-value>
<xa-datasource-class>com.microsoft.sqlserver.jdbc.SQLServerXADataSource</xa-datasource-class>
<xa-datasource-property name="ServerName">192.168.1.200</xa-datasource-property>
<xa-datasource-property name="DatabaseName">DGHouseInfo</xa-datasource-property>
<xa-datasource-property name="SelectMethod">cursor</xa-datasource-property>
<xa-datasource-property name="URL">jdbc:sqlserver://192.168.1.200:1433</xa-datasource-property>
<user-name>sa</user-name>
<password>×××</password>
<!-- The minimum connections in a pool/sub-pool. Pools are lazily
constructed on first use -->
<min-pool-size>5</min-pool-size>
<!-- The maximum connections in a pool/sub-pool -->
<max-pool-size>20</max-pool-size>
<metadata>
<type-mapping>MS SQLSERVER2000</type-mapping>
</metadata>
</xa-datasource>
<xa-datasource>
<jndi-name>ensure_mssql_xa_datasource</jndi-name>
<isSameRM-override-value>false</isSameRM-override-value>
<xa-datasource-class>com.microsoft.sqlserver.jdbc.SQLServerXADataSource</xa-datasource-class>
<xa-datasource-property name="ServerName">192.168.1.200</xa-datasource-property>
<xa-datasource-property name="DatabaseName">EnsureHouseRecord</xa-datasource-property>
<xa-datasource-property name="SelectMethod">cursor</xa-datasource-property>
<xa-datasource-property name="URL">jdbc:sqlserver://192.168.1.200:1433</xa-datasource-property>
<user-name>sa</user-name>
<password>×××</password>
<!-- The minimum connections in a pool/sub-pool. Pools are lazily
constructed on first use -->
<min-pool-size>5</min-pool-size>
<!-- The maximum connections in a pool/sub-pool -->
<max-pool-size>20</max-pool-size>
<metadata>
<type-mapping>MS SQLSERVER2000</type-mapping>
</metadata>
</xa-datasource>
</datasources>
对印的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Persistence deployment descriptor for dev profile -->
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="house-mssql-xa-ds" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:house_mssql_xa_datasource</jta-data-source>
<jar-file>WEB-INF/lib/house-mssql-po-1.0-SNAPSHOT.jar</jar-file>
<exclude-unlisted-classes/>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.hbm2ddl.auto" value="none"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.default_schema" value="dbo"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:/house-mssql-xaEntityManagerFactory"/>
</properties>
</persistence-unit>
<persistence-unit name="ensure-mssql-xa-ds" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:ensure_mssql_xa_datasource</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.hbm2ddl.auto" value="none"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.default_schema" value="dbo"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:/ensure-mssql-xaEntityManagerFactory"/>
</properties>
</persistence-unit>
</persistence>
但是配好之后程序 异常:
Caused by: org.hibernate.exception.GenericJDBCException: Cannot open connection
(javax.transaction.SystemException: java.lang.Throwable: Unabled to enlist resource, see the previous warnings. tx=TransactionImple < ac, BasicAction .... Could not enlist in transaction on entering meta-aware object!;
网上查了一下,说是如果jboss中一次事务如果包含两个数据源 就会出现 上述第二个异常,解决方式是修改下jboss的配置:
conf/jbossjta-properties.xml (JBoss-4.x) or conf/jbossts-properies.xml (JBoss 5)
在<properties depends="arjuna" name="jta">
节点下增加:
<!-- 支持一个事务中包括多个数据源 -->
<property name="com.arjuna.ats.jta.allowMultipleLastResources" value="true"/>
参考:
http://community.jboss.org/wiki/Multiple1PC
http://community.jboss.org/thread/158421
http://topic.csdn.net/u/20090331/22/c7752efc-de60-470a-930c-6a739bf8c56d.html
http://ruowu.iteye.com/blog/725591
但是我修改了之后无效,没有效果,异常依旧.
后来我就又上网查,mssql2000的xa事务配置.但是网上都是说mssql 2005的,我依样画瓢,终于好使了。原来mssql要支持xa事务,还要给mssql打上补丁。补丁就在JDBC驱动包中。还要开启DTC服务,并进行COM设置(mssql的分布式事务是通过com+来实现的,汉),具体参见:
http://tantan.iteye.com/blog/617442
这大概就是出现第一个异常 Cannot open connection的原因了。另外区别这两个异常的关键在于,jboss有没有这个警告:
WARN Adding multiple last resources is disallowed. Current resource is <resource>