您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 能源与动力工程 > Spring分布式事务实现
Spring分布式事务实现分布式事务是指操作多个数据库之间的事务,spring的org.springframework.transaction.jta.JtaTransactionManager,提供了分布式事务支持。如果使用WAS的JTA支持,把它的属性改为WebSphere对应的TransactionManager。在tomcat下,是没有分布式事务的,不过可以借助于第三方软件jotm(JavaOpenTransactionManager)和AtomikosTransactionsEssentials实现,在spring中分布式事务是通过jta(jotm,atomikos)来进行实现。1、、一、使用JOTM例子(1)Dao及实现GenericDao接口:?1234publicinterfaceGenericDao{publicintsave(Stringds,Stringsql,Object[]obj)throwsException;publicintfindRowCount(Stringds,Stringsql);}GenericDaoImpl实现:?12345678910111213141516171819202122232425262728publicclassGenericDaoImplimplementsGenericDao{privateJdbcTemplatejdbcTemplateA;privateJdbcTemplatejdbcTemplateB;publicvoidsetJdbcTemplateA(JdbcTemplatejdbcTemplate){this.jdbcTemplateA=jdbcTemplate;}publicvoidsetJdbcTemplateB(JdbcTemplatejdbcTemplate){this.jdbcTemplateB=jdbcTemplate;}publicintsave(Stringds,Stringsql,Object[]obj)throwsException{if(null==ds||.equals(ds))return-1;try{if(ds.equals(A)){returnthis.jdbcTemplateA.update(sql,obj);}else{returnthis.jdbcTemplateB.update(sql,obj);}}catch(Exceptione){e.printStackTrace();thrownewException(执行+ds+数据库时失败!);}}publicintfindRowCount(Stringds,Stringsql){293031323334353637if(null==ds||.equals(ds))return-1;if(ds.equals(A)){returnthis.jdbcTemplateA.queryForInt(sql);}else{returnthis.jdbcTemplateB.queryForInt(sql);}}}(2)Service及实现UserService接口:?123publicinterfaceUserService{publicvoidsaveUser()throwsException;}UserServiceImpl实现:?123456789101112131415161718192021222324publicclassUserServiceImplimplementsUserService{privateGenericDaogenericDao;publicvoidsetGenericDao(GenericDaogenericDao){this.genericDao=genericDao;}publicvoidsaveUser()throwsException{StringuserName=user_+Math.round(Math.random()*10000);System.out.println(userName);StringBuildersql=newStringBuilder();sql.append(insertintot_user(username,gender)values(?,?););Object[]objs=newObject[]{userName,1};genericDao.save(A,sql.toString(),objs);sql.delete(0,sql.length());sql.append(insertintot_user(name,sex)values(?,?););objs=newObject[]{userName,男的};//值超出范围genericDao.save(B,sql.toString(),objs);}}(3)applicationContext-jotm.xml?123?xmlversion=1.0encoding=UTF-8?beansxmlns=:xsi=:context=:aop=:tx=:schemaLocation=://://://://://://://!--指定Spring配置中用到的属性文件--beanid=propertyConfigclass=org.springframework.beans.factory.config.PropertyPlaceholderConfigurerpropertyname=locationslistvalueclasspath:jdbc.properties/value/list/property/bean!--JOTM实例--beanid=jotmclass=org.springframework.transaction.jta.JotmFactoryBeanpropertyname=defaultTimeoutvalue=500000//bean!--JTA事务管理器--beanid=jtaTransactionManagerclass=org.springframework.transaction.jta.JtaTransactionManagerpropertyname=userTransactionref=jotm//bean!--数据源A--beanid=dataSourceAclass=org.enhydra.jdbc.pool.StandardXAPoolDataSourcedestroy-method=shutdownpropertyname=dataSourcebeanclass=org.enhydra.jdbc.standard.StandardXADataSourcedestroy-method=shutdownpropertyname=transactionManagerref=jotm/propertyname=driverNamevalue=${jdbc.driver}/propertyname=urlvalue=${jdbc.url}//bean/propertypropertyname=uservalue=${jdbc.username}/propertyname=passwordvalue=${jdbc.password}//bean!--数据源B--beanid=dataSourceBclass=org.enhydra.jdbc.pool.StandardXAPoolDataSourcedestroy-method=shutdownpropertyname=dataSourcebeanclass=org.enhydra.jdbc.standard.StandardXADataSourcedestroy-method=shutdownpropertyname=transactionManagerref=jotm/propertyname=driverNamevalue=${jdbc2.driver}/535455565758596061626364656667686970717273747576777879808182838485868788899091propertyname=urlvalue=${jdbc2.url}//bean/propertypropertyname=uservalue=${jdbc2.username}/propertyname=passwordvalue=${jdbc2.password}//beanbeanid=jdbcTemplateAclass=org.springframework.jdbc.core.JdbcTemplatepropertyname=dataSourceref=dataSourceA//beanbeanid=jdbcTemplateBclass=org.springframework.jdbc.core.JdbcTemplatepropertyname=dataSourceref=dataSourceB//bean!--事务切面配置--aop:configaop:pointcutid=pointCutexpression=execution(*com.logcd.service..*.*(..))/!--包及其子包下的所有方法--aop:advisorpointcut-ref=pointCutadvice-ref=txAdvice/aop:advisorpointcut=execution(**..common.service..*.*(..))advice-ref=txAdvice//ao
本文标题:Spring分布式事务实现
链接地址:https://www.777doc.com/doc-6224856 .html