Spring分布式事务实现

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

       分布式事务是指操作多个数据库之间的事务,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及实现 

GenericDao接口:

public interface GenericDao public int save(String ds, String sql, Object[] obj) throws Exception;  public int findRowCount(String ds, String sql); }

GenericDaoImpl 实现:

public class GenericDaoImpl implements GenericDaoprivate  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及实现 

UserService 接口:

public interface UserService public void saveUser() throws Exception;}

UserServiceImpl 实现:

public class UserServiceImpl implements UserServiceprivate 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 
<?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:context="http://www.springframework.org/schema/context"  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.5.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <description>springJTA</description> <!--指定Spring配置中用到的属性文件-->  <bean id="propertyConfig"    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   <property name="locations">    <list>     <value>classpath:jdbc.properties</value>    </list>   </property>  </bean>   <!-- JOTM实例 --> <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean">       <property name="defaultTimeout" value="500000"/> </bean> <!-- JTA事务管理器 --> <bean id="jtaTransactionManager" class="org.springframewor
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值