Spring支持JTA事务之JOTM

随着网站(www.jzease.com)的扩大,在三个月的时间里Myslq的表格从四十几张,扩到现在的一百多张,以后肯定还会继续扩大为了更好的管理和运行,着手开始分数据库,这就涉及到跨数据库事务,我们使用的是Tomcat,它本身不支持XA,供我选择的就只有spring+JTA,JTA的实现就只有两种,一种是JOTM,另一种是AtomikosTransactionsEssentials.下面是JOTM的实现:
DataSource:
<bean id="jtaTxManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotmJta"/>
</bean>
<!--第一个数据源,采用XAPool链接池-->
<bean id="familyDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
<property name="dataSource">
<bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
<property name="transactionManager" ref="jotmJta" />
<property name="driverName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/family?useUnicode=true&characterEncoding=GBK"/>
</bean>
</property>
<property name="user" value="root"/>
<property name="password" value="1234"/>
</bean>
<!--第二个数据源,采用XAPool链接池-->
<bean id="localDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
<property name="dataSource">
<bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
<property name="transactionManager" ref="jotmJta" />
<property name="driverName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/local?useUnicode=true&characterEncoding=GBK"/>
</bean>
</property>
<property name="user" value="root"/>
<property name="password" value="1234"/>
</bean>
<!--tx配置aop,spring2.0的用法-->
<tx:annotation-driven transaction-manager="jtaTxManager"/>
DAO:
<beans>
<bean id="familyJdbcDAO" class="com.xband.admin.spring.LocalJdbcDAO">
<property name="dataSource" ref="familyDataSource" />
</bean>

<bean id="localJdbcDAO" class="com.xband.admin.spring.LocalJdbcDAO">
<property name="dataSource" ref="localDataSource" />
</bean>
</beans>


Service:
<!--在程序里使用元数据@Transactional声明事务-->
<bean id="springService" class=" com.xband.admin.spring.SpringService">
<property name="familyJdbcDAO" ref="familyHibernatedao"/>
<property name="localJdbcDAO" ref="localJdbcDAO"/>
</bean>

好!如果你只用JDBC写DAO,上面的配置完全没问题,两个数据源能同时rollback(),接着我们配置Hibernater,配置如下:
<beans>
<bean id="familySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="familyDataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.family.xml"/>
<property name="jtaTransactionManager"> <ref bean="jotmJta" /> </property>
</bean>
<bean id="localSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="localDataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.local.xml"/>
<property name="jtaTransactionManager"> <ref bean="jotmJta" /> </property>
</bean>
</beans> 


http://www.iteye.com/topic/145404

你可能感兴趣的:(DAO,spring,AOP,tomcat,配置管理)