atomikos跨库事务配置

以下配置作为参考笔记,个人比较喜欢注解方式的事务,所以没有切片事务的配置。

环境:Spring 3.2.5 + hibernate4.2.7sp1

单库事务:

<?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:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

 

 

 

 

  

   

">

<bean

destroy-method="close">

<property>

<value>com.mysql.jdbc.Driver</value>

</property>

<property>

<value>jdbc:mysql://www.englishfree.com.cn:3308/knowledge?characterEncoding=UTF8</value>

</property>

<property>

<value>icod</value>

</property>

<property>

<value>123.com</value>

</property>


<!-- 解决死锁问题 -->

<!-- 当连接池连接耗尽时,客户端getConnection(),所等待的时间 -->

<property>

<value>30000</value>

</property>


<!--连接池中保留的最小连接数。 -->

<property>

<value>3</value>

</property>


<!--连接池中保留的最大连接数。Default: 15 -->

<property>

<value>30</value>

</property>


<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->

<property>

<value>3</value>

</property>


<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->

<property>

<value>30</value>

</property>


<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->

<property>

<value>3</value>

</property>


<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 

如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->

<property>

<value>0</value>

</property>


<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->

<property>

<value>60</value>

</property>


<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->

<property>

<value>30</value>

</property>


<!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 

获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->

<property>

<value>true</value>

</property>


<!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 

等方法来提升连接测试的性能。Default: false -->

<property>

<value>false</value>

</property>

</bean>


<bean

>

<property>

<ref bean="dataSource" />

</property>

<property>

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</prop>

</props>

</property>

<property>

<list>

<value>

com/store/hibernate/Goods.hbm.xml

</value>

<value>

com/store/hibernate/Purchase.hbm.xml

</value>

<value>

com/store/hibernate/Role.hbm.xml

</value>

<value>

com/store/hibernate/Sale.hbm.xml

</value>

<value>

com/store/hibernate/Stock.hbm.xml

</value>

<value>

com/store/hibernate/User.hbm.xml

</value>

<value>

com/store/hibernate/GoodsStock.hbm.xml

</value>

<value>

com/store/hibernate/Locktest.hbm.xml

</value>

</list>

</property>

</bean>

<bean

>

<property ref="sessionFactory" />

</bean>


<tx:annotation-driven transaction-manager="transactionManager"

proxy-target-class="true" />


<bean>

<property>

<ref bean="sessionFactory" />

</property>

</bean>



<bean>

<property>

<ref bean="storeDAO" />

</property>

<property>

<ref bean="storeDAO" />

</property>

</bean>


<!-- action beans -->

<bean scope="prototype">

<property ref="storeDAO" />

</bean>



</beans>


atomikos-nonxa方式:

<?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:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

 

 

 

 

  

   

">



<bean

init-method="init" destroy-method="close">

<property>

<value>true</value>

</property>

</bean>

<bean>

<property value="240" />

</bean>


<bean

>

<property>

<ref bean="atomikosTransactionManager" />

</property>

<property>

<ref bean="atomikosUserTransaction" />

</property>

<property value="true" />

</bean>


<tx:annotation-driven transaction-manager="transactionManager"

proxy-target-class="true" />




<bean

init-method="init" destroy-method="close">

<property>

<value>store3308</value>

</property>

<property>

<value>com.mysql.jdbc.Driver</value>

</property>

<property>

<value>jdbc:mysql://www.englishfree.com.cn:3308/knowledge?characterEncoding=UTF8</value>

</property>

<property>

<value>icod</value>

</property>

<property>

<value>123.com</value>

</property>

<property>

<value>5</value>

</property>

<property>

<value>60</value>

</property>

</bean>

<bean

init-method="init" destroy-method="close">

<property>

<value>store3333</value>

</property>

<property>

<value>com.mysql.jdbc.Driver</value>

</property>

<property>

<value>jdbc:mysql://www.englishfree.com.cn:3333/knowledge?characterEncoding=UTF8</value>

</property>

<property>

<value>icod</value>

</property>

<property>

<value>123.com</value>

</property>

<property>

<value>5</value>

</property>

<property>

<value>60</value>

</property>

</bean>


<bean

>

<property>

<ref bean="dataSource1" />

</property>

<property>

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</prop>

<prop key="hibernate.current_session_context_class">jta</prop>

<prop key="hibernate.transaction.factory_class">

org.hibernate.transaction.JTATransactionFactory

</prop>

<prop key="hibernate.transaction.manager_lookup_class">

com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup

</prop>



</props>

</property>

<property>

<list>

<value>

com/store/hibernate/Goods.hbm.xml

</value>

<value>

com/store/hibernate/Purchase.hbm.xml

</value>

<value>

com/store/hibernate/Role.hbm.xml

</value>

<value>

com/store/hibernate/Sale.hbm.xml

</value>

<value>

com/store/hibernate/Stock.hbm.xml

</value>

<value>

com/store/hibernate/User.hbm.xml

</value>

<value>

com/store/hibernate/GoodsStock.hbm.xml

</value>

<value>

com/store/hibernate/Locktest.hbm.xml

</value>

</list>

</property>

</bean>


<bean

>

<property>

<ref bean="dataSource2" />

</property>

<property>

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</prop>

<prop key="hibernate.current_session_context_class">jta</prop>

<prop key="hibernate.transaction.factory_class">

org.hibernate.transaction.JTATransactionFactory

</prop>

<prop key="hibernate.transaction.manager_lookup_class">

com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup

</prop>

</props>

</property>

<property>

<list>

<value>

com/store/hibernate/Goods.hbm.xml

</value>

<value>

com/store/hibernate/Purchase.hbm.xml

</value>

<value>

com/store/hibernate/Role.hbm.xml

</value>

<value>

com/store/hibernate/Sale.hbm.xml

</value>

<value>

com/store/hibernate/Stock.hbm.xml

</value>

<value>

com/store/hibernate/User.hbm.xml

</value>

<value>

com/store/hibernate/GoodsStock.hbm.xml

</value>

<value>

com/store/hibernate/Locktest.hbm.xml

</value>

</list>

</property>

</bean>


<bean>

<property>

<ref bean="sessionFactory1" />

</property>

</bean>


<bean>

<property>

<ref bean="sessionFactory1" />

</property>

</bean>


<bean>

<property>

<ref bean="sessionFactory2" />

</property>

</bean>


<bean>

<property>

<ref bean="storeDAO1" />

</property>

<property>

<ref bean="storeDAO2" />

</property>

</bean>


<!-- action beans -->

<bean scope="prototype">

<property ref="storeDAO1" />

</bean>



</beans>

atomikos-xa方式:

<?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:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

 

 

 

 

  

   

">



<bean

init-method="init" destroy-method="close">

<property>

<value>true</value>

</property>

</bean>

<bean>

<property value="240" />

</bean>


<bean

>

<property>

<ref bean="atomikosTransactionManager" />

</property>

<property>

<ref bean="atomikosUserTransaction" />

</property>

<property value="true" />

</bean>


<tx:annotation-driven transaction-manager="transactionManager"

proxy-target-class="true" />




<bean

init-method="init" destroy-method="close">

<description>dataSource 3308</description>

<property>

<value>store3308</value>

</property>

<property>

<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>

</property>

<property>

<props>

<prop key="user">icod</prop>

<prop key="password">123.com</prop>

<prop key="URL">jdbc:mysql://www.englishfree.com.cn:3308/knowledge?characterEncoding=UTF8</prop>

<prop key="pinGlobalTxToPhysicalConnection">true</prop>

<prop key="autoReconnect">true</prop>

</props>

</property>

<!-- set properties for datasource connection pool -->

<property>

<value>2</value>

</property>

<property>

<value>20</value>

</property>

<property>

<value>30</value>

</property>

<property>

<value>600</value>

</property>

<!-- set a SQL for testing connection -->

<property>

<value>select 1 from dual</value>

</property>

</bean>


<bean

init-method="init" destroy-method="close">

<description>dataSource 3333</description>

<property>

<value>store3333</value>

</property>

<property>

<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>

</property>

<property>

<props>

<prop key="user">icod</prop>

<prop key="password">123.com</prop>

<prop key="URL">jdbc:mysql://www.englishfree.com.cn:3333/knowledge?characterEncoding=UTF8</prop>

<prop key="pinGlobalTxToPhysicalConnection">true</prop>

<prop key="autoReconnect">true</prop>

</props>

</property>

<!-- set properties for datasource connection pool -->

<property>

<value>2</value>

</property>

<property>

<value>20</value>

</property>

<property>

<value>30</value>

</property>

<property>

<value>600</value>

</property>

<!-- set a SQL for testing connection -->

<property>

<value>select 1 from dual</value>

</property>

</bean>


<bean

>

<property>

<ref bean="dataSource1" />

</property>

<property>

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</prop>

<prop key="hibernate.current_session_context_class">jta</prop>

<prop key="hibernate.transaction.factory_class">

org.hibernate.transaction.JTATransactionFactory

</prop>

<prop key="hibernate.transaction.manager_lookup_class">

com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup

</prop>



</props>

</property>

<property>

<list>

<value>

com/store/hibernate/Goods.hbm.xml

</value>

<value>

com/store/hibernate/Purchase.hbm.xml

</value>

<value>

com/store/hibernate/Role.hbm.xml

</value>

<value>

com/store/hibernate/Sale.hbm.xml

</value>

<value>

com/store/hibernate/Stock.hbm.xml

</value>

<value>

com/store/hibernate/User.hbm.xml

</value>

<value>

com/store/hibernate/GoodsStock.hbm.xml

</value>

<value>

com/store/hibernate/Locktest.hbm.xml

</value>

</list>

</property>

</bean>


<bean

>

<property>

<ref bean="dataSource2" />

</property>

<property>

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</prop>

<prop key="hibernate.current_session_context_class">jta</prop>

<prop key="hibernate.transaction.factory_class">

org.hibernate.transaction.JTATransactionFactory

</prop>

<prop key="hibernate.transaction.manager_lookup_class">

com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup

</prop>

</props>

</property>

<property>

<list>

<value>

com/store/hibernate/Goods.hbm.xml

</value>

<value>

com/store/hibernate/Purchase.hbm.xml

</value>

<value>

com/store/hibernate/Role.hbm.xml

</value>

<value>

com/store/hibernate/Sale.hbm.xml

</value>

<value>

com/store/hibernate/Stock.hbm.xml

</value>

<value>

com/store/hibernate/User.hbm.xml

</value>

<value>

com/store/hibernate/GoodsStock.hbm.xml

</value>

<value>

com/store/hibernate/Locktest.hbm.xml

</value>

</list>

</property>

</bean>


<bean>

<property>

<ref bean="sessionFactory1" />

</property>

</bean>


<bean>

<property>

<ref bean="sessionFactory1" />

</property>

</bean>


<bean>

<property>

<ref bean="sessionFactory2" />

</property>

</bean>


<bean>

<property>

<ref bean="storeDAO1" />

</property>

<property>

<ref bean="storeDAO2" />

</property>

</bean>


<!-- action beans -->

<bean scope="prototype">

<property ref="storeDAO1" />

</bean>



</beans>

anomikos引入,会让web app启动慢,没必要的情况下,还是不用。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/11286.html