[tomcat] spring2.5.6 + hiberante3.1.3 + atomikos3.8.0 多数据源事务配置

1.将 atomikos3.8.0 提供的相关jar包( AtomikosTransactionsEssentials-3.8.0-bin.zip),添加到项目lib目录中
    atomikos-util.jar
    transactions.jar
    transactions-jta.jar
    transactions-api.jar
    transactions-hibernate3.jar
    transactions-jdbc.jar

    transactions-jdbc-deprecated.jar


2.修改applicationContext-Hibernate.xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<!-- 数据库信息文件 -->
		<property name="location">
			<value>classpath:hibernate.properties</value>
		</property>
	</bean>


	<bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close" lazy-init="true">
		<property name="uniqueResourceName"><value>OracleXADataSource</value></property>
		<property name="xaDataSourceClassName"><value>oracle.jdbc.xa.client.OracleXADataSource</value></property>
		<property name="xaProperties">
			<props>
				<prop key="URL">${url}</prop>
				<prop key="user">${username}</prop>
				<prop key="password">${password}</prop>
			</props>
		</property>
		<!-- set properties for datasource connection pool -->  
		<property name="minPoolSize"><value>2</value></property>  
		<property name="maxPoolSize"><value>20</value></property>
		<property name="loginTimeout"><value>30</value></property>  
		<property name="reapTimeout"><value>600</value></property>  
		<!-- set a SQL for testing connection -->  
		<property name="testQuery"><value>select 1 from dual</value></property>
	</bean>     


	<bean id="dataSourceUUS" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close" lazy-init="true">
		<property name="uniqueResourceName"><value>OracleXADataSourceUUS</value></property>
		<property name="xaDataSourceClassName"><value>oracle.jdbc.xa.client.OracleXADataSource</value></property>
		<property name="xaProperties">
			<props>
				<prop key="URL">${uus_url}</prop>
				<prop key="user">${uus_username}</prop>
				<prop key="password">${uus_password}</prop>
			</props>
		</property>
		<!-- set properties for datasource connection pool -->  
		<property name="minPoolSize"><value>2</value></property>  
		<property name="maxPoolSize"><value>20</value></property>
		<property name="loginTimeout"><value>30</value></property>  
		<property name="reapTimeout"><value>600</value></property>  
		<!-- set a SQL for testing connection -->  
		<property name="testQuery"><value>select 1 from dual</value></property>
	</bean>     

	<!-- sessionFactory1-->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref local="dataSourceDMS"/>
		</property>
		<!-- 处理CLOB对象 -->
		<property name="lobHandler" ref="lobHandler" />
		<property name="mappingDirectoryLocations">
			<list>
				<value>classpath:org/hdht/</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
				<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
				<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
				<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
				<prop key="Hibernate.cache.use_query_cache">${Hibernate.cache.use_query_cache}</prop>
				<prop key="Hibernate.cache.query_cache_factory">${Hibernate.cache.query_cache_factory}</prop>
			</props>
		</property>
	</bean>
	
	<!-- sessionFactory2 -->
	<bean id="uusSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref local="dataSourceUUS" />
		</property>
		<!-- 处理CLOB对象 -->
		<property name="lobHandler" ref="lobHandler" />
		<property name="mappingDirectoryLocations">
			<list>
				<value>classpath:org/hdht/</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
				<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
				<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
				<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
				<prop key="Hibernate.cache.use_query_cache">${Hibernate.cache.use_query_cache}</prop>
				<prop key="Hibernate.cache.query_cache_factory">${Hibernate.cache.query_cache_factory}</prop>
			</props>
		</property>
	</bean>
	
	
	<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">  
	    <property name="transactionManager">  
	        <bean class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">  
	            <property name="forceShutdown" value="true"/>  
	        </bean>  
	    </property>  
	    <property name="userTransaction">  
	        <bean class="com.atomikos.icatch.jta.UserTransactionImp"/>  
	    </property>  
	</bean>

	
	<!-- ========================= ASPECT CONFIGURATION ======================== -->
	<aop:config proxy-target-class="true">
		<!--
			This definition creates auto-proxy infrastructure based on the given pointcut,
			expressed in AspectJ pointcut language. Here: applying the advice named
			"txAdvice" to all methods on classes named PetStoreImpl.
		-->
		<aop:advisor pointcut="execution(* org.hdht..*Logic.*(..))" advice-ref="txAdvice"/>
	</aop:config>
	
	<!--
		Transaction advice definition, based on method name patterns.
		Defaults to PROPAGATION_REQUIRED for all methods whose name starts with
		"insert" or "update", and to PROPAGATION_REQUIRED with read-only hint
		for all other methods.
	-->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="insert*"/>
		    <tx:method name="save*"/>
			<tx:method name="add*"/>
            <tx:method name="create*"/>	

			<tx:method name="update*"/>
			<tx:method name="modify*"/>
	        <tx:method name="change*"/>	

			<tx:method name="remove*"/>
			<tx:method name="delete*"/>

			<tx:method name="reset*"/>
			<tx:method name="recycle*"/>
			<tx:method name="backup*"/>	
			
			<tx:method name="*" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- 处理CLOB对象 -->
	<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor" lazy-init="true" />
	<bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
		<property name="nativeJdbcExtractor">
			<ref local="nativeJdbcExtractor" />
		</property>
	</bean>
    
    <!-- 
        Use default sessionFactory
        MyBusinessDao1 need extends HibernateDaoSupport
    -->    
    <bean id="myBusinessDao1" class="org.mycorp.business.dao.MyBusinessDao1" autowire="byName"/>

    <!-- 
        Use uusSessionFactory
        MyBusinessDao2 need extends HibernateDaoSupport
    -->   
    <bean id="myBusinessDao2" class="org.mycorp.business.dao.MyBusinessDao2" autowire="byName" >
		<property name="sessionFactory" ref="uusSessionFactory"/>
    </bean>

</beans>



附1:
hibernate.properties
--------------------------------

dms_url=jdbc:oracle:thin:@192.168.1.89:1521:mas
username=MAS_DMS
password=MAS_DMS

uus_url=jdbc:oracle:thin:@192.168.1.89:1521:mas
uus_username=MAS_UUS
uus_password=MAS_UUS

hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
hibernate.show_sql=true
hibernate.format_sql=false
hibernate.jdbc.batch_size=50
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
Hibernate.cache.use_query_cache=false
Hibernate.cache.query_cache_factory=org.hibernate.cache.StandardQueryCacheFactory

附2:

tx:method 属性说明
---------------------------------
name  
参数说明:与事务属性关联的方法名。通配符(*)可以用来指定一批关联到相同的事务属性的方法。如:'get*'、'handle*'、'on*Event'等等。
是否必须:必须   默认值:无

propagation
参数说明:事务传播行为
是否必须:不必须   默认值:REQUIRED

isolation
参数说明:事务隔离级别
是否必须:不必须   默认值:DEFAULT

timeout
参数说明:事务超时的时间(以秒为单位)
是否必须:不必须   默认值:-1

read-only
参数说明:事务是否只读
是否必须:不必须   默认值:false

rollback-for
参数说明:将被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException,ServletException'
是否必须:不必须   默认值:无

no-rollback-for
参数说明:不被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException,ServletException'
是否必须:不必须   默认值:无



事务的集中传播忒性

-------------------------------
1、PROPAGATION_REQUIRED:如果存在一个事务,则支持当前事务。如果没有事务则开启。
2、PROPAGATION_SUPPORTS:如果存在一个事务,支持当前事务。如果没有事务,则非事务的执行。
3、PROPAGATION_MANDATORY:如果已经存在一个事务,支持当前事务。如果没有一个活动的事务,则抛出异常。
4、PROPAGATION_REQUIRES_NEW:总是开启一个新的事务。如果一个事务存在,则将这个存在的事务挂起。
5、PROPAGATION_NOT_SUPPORTED:总是非事务地执行,并挂起任何存在的事务。
6、PROPAGATION_NEVER:总是非事务地执行,如果存在一个活动事务,则抛出异常。
7、PROPAGATION_NESTED:如果一个活动的事务存在,则运行在一个嵌套的事务中,如果没有活动事务,则按TransactionDefinition.PROPAGATION_REQUIRED属性执行


Spring事务的隔离级别

-------------------------------
1、ISOLATION_DEFAULT: 这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别。
2、ISOLATION_READ_UNCOMMITTED:这是事务最低的隔离级别,它允许另外一个事务可以看到这个事务未提交的数据。
3、ISOLATION_READ_COMMITTED:保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据。
4、ISOLATION_REPEATALBE_READ: 这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻想读。它除了保证一个事务不能读取另外一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)。
5、ISOLATION_SERIALIZABLE 这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。除了防止脏读,不课重复读外,还避免了幻想读。



有关编译器优化的更完整信息,请参阅优化通知