I. Preface:
Before writing this blog, we need to figure out two concepts: local and distributed transactions.
Local transactions: Handle only a single data source, such as a single database.
Distributed transactions: Handles a variety of heterogeneous data sources, such as JDBC and JMS in a business operation, or an operation that requires access to several different databases.
Java through JTA to complete the distributed transaction, JTA itself is only a specification, this blog will use JOTM as the implementation, follow-up will also use Atomikos implementation.
Two. Business background:
Suppose we have a requirement: when we create a new user, we need to insert a user record into a db, and we need to log in another db
Because it is a different db operation, this involves the processing of distributed transactions.
Three. Code implementation:
1. Code structure diagram:
2. Build the Table statement:
Create DATABASE Log;drop TABLE IF EXISTS ' log '; CREATE TABLE ' log ' ( ' id ' varchar) NOT NULL, ' content ' varchar (+) default NULL, PRIMARY KEY (' id ')) Create DATABASE User;drop TABLE IF EXISTS ' user '; CREATE TABLE ' user ' ( ' id ' varchar () not NULL, ' name ' varchar (+) default NULL, PRIMARY KEY (' id '));
3. configuration file Applicationcontext.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: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.xsd Http://www.springframework.org/sche Ma/tx http://www.springframework.org/schema/tx/spring-tx.xsd HTTP://WWW.SPRINGFR AMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd "><!--quotes within spring The JOTM supported factory class--><bean id= "Jotm" class= "Org.springframework.transaction.jta.JotmFactoryBean"/><!-- Configure the JTA transaction manager and use the JOTM--><bean id= "Txmanager" class= "in the manager that is configured above Org.springframework.transaction.jta.JtaTransactionManager "><property name=" usertransaction "ref=" JoTM "/></bean><!--Configuring multiple data sources--><bean id=" DB1 "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=" Jotm "/><property name=" drivername "value=" Com.mysql.jdbc.Driver "/><property Name= "url" value= "Jdbc:mysql://localhost:3306/user"/></bean></property><property name= "user" Value= "root"/><property name= "password" value= "root"/></bean><bean id= "DB2" 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=" Jotm "/><property name=" drivername "value=" Com.mysql.jdbc.Driver "/><property Name= "url" value= "Jdbc:mysql://localhost: 3306/log "/></bean></property><property name=" user "value=" root "/><property name=" Password "value=" root "/></bean><!--Configure two jdbctemplate--><bean id=" jdbcTemplate1 "class= according to different data sources Org.springframework.jdbc.core.JdbcTemplate "><property name=" DataSource "ref=" DB1 "/></bean>< Bean id= "JdbcTemplate2" class= "org.springframework.jdbc.core.JdbcTemplate" ><property name= "DataSource" ref= " DB2 "/></bean><bean id=" Userdao "class=" Com.zdp.dao.UserDao "><property name=" JdbcTemplate "ref=" JdbcTemplate1 "/></bean><bean id=" Logdao "class=" Com.zdp.dao.LogDao "><property name=" JdbcTemplate "ref=" JdbcTemplate2 "/></bean><bean id=" UserService "class=" Com.zdp.service.UserService " ><property name= "Userdao" ref= "Userdao"/><property name= "Logdao" ref= "Logdao"/></bean><!-- JTA Transaction propagation Characteristics--<tx:advice id= "Txadvicejta" transaction-manager= "Txmanager" > <tx:attributes> <tx:method name= "save*" isolation= "DEFAULT" propagation= "REQUIRED" rollback-for= "Exception"/> <tx:method name= "add*" isolation= "DEFAULT" propagation= "REQUIRED" rollback-for= "Exception"/> <tx:met Hod name= "create*" isolation= "DEFAULT" propagation= "REQUIRED" rollback-for= "Exception"/> <tx:method name= "insert*" isolation= "DEFAULT" propagation= "REQUIRED" rollback-for= "Exception"/> <tx:method name= "del*" is olation= "DEFAULT" propagation= "REQUIRED" rollback-for= "Exception"/> <tx:method name= "update*" isolation= " DEFAULT "propagation=" REQUIRED "rollback-for=" Exception "/> <tx:method name=" * "read-only=" true "/> </tx:attributes> </tx:advice> <aop:config> <aop:advisor pointcut= "Execution (* COM.ZD P.service. *(..))" advice-ref= "Txadvicejta"/> </aop:config></beans>
4. Service Business class:
public class UserService {private Userdao userdao;private Logdao logdao;public void Saveuser (string id, string name) {User Dao.insertuser (ID, name);//int i = 1/0; Manufacturing exception Logdao.insertlog (ID, id + "_" + Name);} Public Userdao Getuserdao () {return userdao;} public void Setuserdao (Userdao userdao) {This.userdao = Userdao;} Public Logdao Getlogdao () {return logdao;} public void Setlogdao (Logdao logdao) {This.logdao = Logdao;}}
5. DAO Class:
public class Userdao extends Jdbcdaosupport {public void Insertuser (string id, string name) {JdbcTemplate template = Getjd Bctemplate (); Template.execute ("INSERT into user values ('" + ID + "', '" + name + ') ");}}
public class Logdao extends Jdbcdaosupport {public void Insertlog (string id, string content) {JdbcTemplate template = Getj Dbctemplate (); Template.execute ("INSERT into log values ('" + ID + "', '" + content + ') ");}}
6. Test class:
public class Usertest {@Testpublic void Testsave () {ApplicationContext cxt = new Classpathxmlapplicationcontext (" Applicationcontext.xml "); UserService US = (userservice) cxt.getbean ("UserService"); Us.saveuser ("1", "Zhangsan");}}
SOURCE Download: http://download.csdn.net/detail/zdp072/7950383
Java Transactions (vii)-Distributed transactions-Spring + JTA + JOTM