spring+hibernate配置多数据源并统一管理事物,首先确定的是必须使用JTA管理事物,在spring的HibernateTransactionManager类API中已经详细说明了两种transactionManager的不同,quote:JTA (usually through JtaTransactionManager) is necessary for accessing multiple transactional resources within the same transaction. The DataSource that Hibernate uses needs to be JTA-enabled in such a scenario (see container setup)。必须引用JTA,而且数据源必须是支持JTA的XAdatasource。quote百度百科:如果计划使用JTA来划分事务,你将需要一个实现了javax.sql.XADataSource,javax.sql.XAConnection和javax.sql.XAResource接口JDBC的驱动。好了,如果你还是坚持用HibernateTransactionManager的话,可以确定的是,事务不一定会回滚。
现在在tomcat下用Atomikos实现JTA分布式事务管理,但是数据源只能用免费版的com.atomikos.jdbc.SimpleDataSourceBean,不能用收费版的com.atomikos.jdbc.AtomikosDataSourceBean。但是这个数据源和tomcat使用的时候关闭服务器时有可能引发内存溢出。tomcat6以上会强制解除那个实例,但是会提示你,因为tomcat已经强制移除了,所以除了日志也没啥关系,可以手动去掉那段监听
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
attempts to log objects that have failed to be unregistered by webapps it hosts when they are stopped, and were forcibly unregistered by Tomcat.
我想说这个数据源貌似使用起来问题蛮多的,日志文件有时候不自动删,内存老溢出,杂七杂八的,反正我现在还在改。程序也有问题哈,内存占用很变态,AIX服务器也很变态,总之都很变态,哥哥我也快变态了。总之想找一个好的实现了XADataSource的数据源,换个JBOSS去试试。
然后复制一下spring的配置文件,如下
<bean id="pdmDataSource" class="com.atomikos.jdbc.SimpleDataSourceBean"> <property name="uniqueResourceName"> <value>pdmDataSource</value> </property> <property name="xaDataSourceClassName"> <value>${pdm.jdbc.xaDataSourceClassName}</value> </property> <property name="xaDataSourceProperties"> <value>user=${pdm.jdbc.username};password=${pdm.jdbc.password};URL=${pdm.jdbc.url}</value> </property> <property name="exclusiveConnectionMode"> <value>true</value> </property> <property name="connectionPoolSize"> <value>15</value> </property> <property name="validatingQuery"> <value>SELECT 1</value> </property> </bean> <bean id="pdmSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="pdmDataSource"/> </property> <property name="mappingDirectoryLocations"> <list> </list> </property> <property name="mappingResources"> <list> <value>com/ccac/mes/materialmanagement/entity/TSfcMove.hbm.xml</value> <value>com/ccac/mes/materialmanagement/entity/PubProduct.hbm.xml</value> <value>com/ccac/mes/materialmanagement/entity/InvdepotDetail.hbm.xml</value> <value>com/ccac/mes/materialmanagement/entity/Invindepot.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${pdm.dialect}</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.hbm2ddl.auto">none</prop> <prop key="hibernate.format_sql">false</prop> </props> </property> </bean> <bean id="dataSource" class="com.atomikos.jdbc.SimpleDataSourceBean"> <property name="uniqueResourceName"> <value>dataSource</value> </property> <property name="xaDataSourceClassName"> <value>${jdbc.xaDataSourceClassName}</value> </property> <property name="xaDataSourceProperties"> <value>user=${jdbc.username};password=${jdbc.password};URL=${jdbc.url}</value> </property> <property name="exclusiveConnectionMode"> <value>true</value> </property> <property name="connectionPoolSize"> <value>15</value> </property> <property name="validatingQuery"> <value>SELECT 1</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource"/> </property> <property name="mappingDirectoryLocations"> <list> <!-- 项目计划模块 --> <value>classpath:com/ccac/mes/projectplan/entity</value> <!-- 工程数据模块 --> <value>classpath:com/ccac/mes/projectdata/entity</value> <!-- 生产资源模块 --> <value>classpath:com/ccac/mes/productionresource/entity</value> <!-- 自定义用户权限模块 --> <value>classpath:com/ccac/mes/factorymodel/entity</value> <!-- 物料管理模块 <value>classpath:com/ccac/mes/materialmanagement/entity</value> --> <!-- 生产监控模块 --> <value>classpath:com/ccac/mes/productmonitor/entity</value> <!-- 生产计划模块 --> <value>classpath:com/ccac/mes/productionplan/entity</value> <!-- 工作流模块 --> <value>classpath:com/ccac/mes/process/entity</value> <!-- OA管理模块--> <value>classpath:com/ccac/mes/OAmanagement/entity</value> </list> </property> <property name="mappingResources"> <list> <!-- 工作流自带表模块 --> <value>jbpm.repository.hbm.xml</value> <value>jbpm.execution.hbm.xml</value> <value>jbpm.history.hbm.xml</value> <value>jbpm.task.hbm.xml</value> <!-- 物料管理模块 --> <value>com/ccac/mes/materialmanagement/entity/InputRecordEffectivity.hbm.xml</value> <value>com/ccac/mes/materialmanagement/entity/InventoryInputRecord.hbm.xml</value> <value>com/ccac/mes/materialmanagement/entity/InventoryInputSheet.hbm.xml</value> <value>com/ccac/mes/materialmanagement/entity/InventoryOutputRecord.hbm.xml</value> <value>com/ccac/mes/materialmanagement/entity/InventoryOutputSheet.hbm.xml</value> <value>com/ccac/mes/materialmanagement/entity/Warehouse.hbm.xml</value> <value>com/ccac/mes/materialmanagement/entity/WarehousePosition.hbm.xml</value> </list> </property> <!-- herbernate properties --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${dialect}</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.format_sql">false</prop> </props> </property> </bean> <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <property name="forceShutdown"> <value>true</value> </property> </bean> <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"> <property name="transactionTimeout"> <value>300</value> </property> </bean> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager"> <ref local="atomikosTransactionManager"/> </property> <property name="userTransaction"> <ref local="atomikosUserTransaction"/> </property> </bean> [b]<!--这个不能用了啊[/b] <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> --> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transactionManager"/> <property name="transactionAttributes"> <props> <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="del*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="revise*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="submit*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="start*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="end*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="change*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="re*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="edit*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="copy*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="input*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="output*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="instance*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="import*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="close*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="operate*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="move*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="workaround*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="rollback*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="cancel*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="is*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="check*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="authenticate*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> <!-- 定义BeanNameAutoProxyCreator--> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*Service</value> </list> </property> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean>
忘了properties的东西
jdbc.xaDataSourceClassName=oracle.jdbc.xa.client.OracleXADataSource jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl jdbc.username=ccacmes jdbc.password=ccacmes dialect=org.hibernate.dialect.Oracle10gDialect