Spring分布式事务实现
分布式事务是指操作多个数据库之间的事务,spring的org.springframework.transaction.jta.JtaTransactionManager,提供了分布式事务支持。如果使用WAS的JTA支持,把它的属性改为WebSphere对应的TransactionManager。
在tomcat下,是没有分布式事务的,不过可以借助于第三方软件jotm(Java Open Transaction Manager )和AtomikosTransactionsEssentials实现,在spring中分布式事务是通过jta(jotm,atomikos)来进行实现。
1、http://jotm.objectweb.org/
2、http://www.atomikos.com/Main/TransactionsEssentials
一、使用JOTM例子
(1)、Dao及实现
public interface GenericDao { public int save(String ds, String sql, Object[] obj) throws Exception; public int findRowCount(String ds, String sql); }
public class GenericDaoImpl implements GenericDao{ private JdbcTemplate jdbcTemplateA; private JdbcTemplate jdbcTemplateB; public void setJdbcTemplateA(JdbcTemplate jdbcTemplate) { this.jdbcTemplateA = jdbcTemplate; } public void setJdbcTemplateB(JdbcTemplate jdbcTemplate) { this.jdbcTemplateB = jdbcTemplate; } public int save(String ds, String sql, Object[] obj) throws Exception{ if(null == ds || "".equals(ds)) return -1; try{ if(ds.equals("A")){ return this.jdbcTemplateA.update(sql, obj); }else{ return this.jdbcTemplateB.update(sql, obj); } }catch(Exception e){ e.printStackTrace(); throw new Exception("执行" + ds + "数据库时失败!"); } } public int findRowCount(String ds, String sql) { if(null == ds || "".equals(ds)) return -1; if(ds.equals("A")){ return this.jdbcTemplateA.queryForInt(sql); }else{ return this.jdbcTemplateB.queryForInt(sql); } } }
(2)、Service及实现
public interface UserService { public void saveUser() throws Exception; }
public class UserServiceImpl implements UserService{ private GenericDao genericDao; public void setGenericDao(GenericDao genericDao) { this.genericDao = genericDao; } public void saveUser() throws Exception { String userName = "user_" + Math.round(Math.random()*10000); System.out.println(userName); StringBuilder sql = new StringBuilder(); sql.append(" insert into t_user(username, gender) values(?,?); "); Object[] objs = new Object[]{userName,"1"}; genericDao.save("A", sql.toString(), objs); sql.delete(0, sql.length()); sql.append(" insert into t_user(name, sex) values(?,?); "); objs = new Object[]{userName,"男的"};//值超出范围 genericDao.save("B", sql.toString(), objs); } }
(3)、applicationContext-jotm.xml
public class TestUserService{ private static UserService userService; @BeforeClass public static void init(){ ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext-jotm.xml"); userService = (UserService)app.getBean("userService"); } @Test public void save(){ System.out.println("begin..."); try{ userService.saveUser(); }catch(Exception e){ System.out.println(e.getMessage()); } System.out.println("finish..."); } }
二、关于使用atomikos实现
(1)、数据源配置
- <bean id="dataSourceA" class="com.atomikos.jdbc.SimpleDataSourceBean" init-method="init" destroy-method="close">
- <property name="uniqueResourceName">
- <value>${datasource.uniqueResourceName}</value>
- </property>
- <property name="xaDataSourceClassName">
- <value>${database.driver_class}</value>
- </property>
- <property name="xaDataSourceProperties">
- <value>URL=${database.url};user=${database.username};password=${database.password}</value>
- </property>
- <property name="exclusiveConnectionMode">
- <value>${connection.exclusive.mode}</value>
- </property>
- <property name="connectionPoolSize">
- <value>${connection.pool.size}</value>
- </property>
- <property name="connectionTimeout">
- <value>${connection.timeout}</value>
- </property>
- <property name="validatingQuery">
- <value>SELECT 1</value>
- </property>
- </bean>
(2)、事务配置
- <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
- init-method="init" destroy-method="close">
- <property name="forceShutdown" value="true"/>
- </bean>
- <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
- <property name="transactionTimeout" value="${transaction.timeout}"/>
- </bean>
- <!-- JTA事务管理器 -->
- <bean id="springTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
- <property name="transactionManager" ref="atomikosTransactionManager"/>
- <property name="userTransaction" ref="atomikosUserTransaction"/>
- </bean>
- <!-- 事务切面配置 -->
- <aop:config>
- <aop:pointcut id="serviceOperation" expression="execution(* *..service*..*(..))"/>
- <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>
- </aop:config>
- <!-- 通知配置 -->
- <tx:advice id="txAdvice" transaction-manager="springTransactionManager">
- <tx:attributes>
- <tx:method name="*" rollback-for="Exception"/>
- </tx:attributes>
- </tx:advice>
相关推荐
LeeLuffy 2020-10-16
zjuwangleicn 2020-09-04
loviezhang 2020-08-08
打不死的小强 2020-07-03
夙梦流尘 2020-06-28
粗茶淡饭 2020-06-25
花落花开春去秋来 2020-06-20
loviezhang 2020-06-16
wenjieyatou 2020-06-09
middleware0 2020-06-09
韩学敏 2020-06-08
CharlesYooSky 2020-06-06
isHooky 2020-05-30
wqbala 2020-06-04
zhangll00 2020-05-11
wqbala 2020-05-05
langyue 2020-05-03