使用JOTM实现分布式事务的例子


使用JOTM实现分布式事务的例子
发布时间:2009/4/18 11:55:07 | 32 人感兴趣 | 0 人参与 | 评分:3
import java.sql.Connection;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import org.enhydra.jdbc.standard.StandardXADataSource;
import org.hsqldb.Server;
import org.objectweb.jotm.Jotm;
import org.springframework.jdbc.core.JdbcTemplate;

public class XADataSourceTest {

    public static void main(String[] args) throws Exception {
        startDataBase();
      
        //创建JTA的UserTransaction和TransactionManager。
        //下面会使用UserTransaction进行事务的提交和回滚。
        //TransactionManager用来管理事务源。
        Jotm jotm = new Jotm(true, false);
        TransactionManager transactionManager = jotm.getTransactionManager();
        UserTransaction utx = jotm.getUserTransaction();
      
        //创建一个分布式数据源 XADataSource
        StandardXADataSource dataSource1 = new StandardXADataSource();
        dataSource1.setDriverName("org.hsqldb.jdbcDriver");
        dataSource1.setUser("sa");
        dataSource1.setUrl("jdbc:hsqldb:testdb1");
        //将该数据源加入到TransactionManager管理范围内
        dataSource1.setTransactionManager(transactionManager);
              
        StandardXADataSource dataSource2 = new StandardXADataSource();
        dataSource2.setDriverName("org.hsqldb.jdbcDriver");
        dataSource2.setUser("sa");
        dataSource2.setUrl("jdbc:hsqldb:testdb2");
        dataSource2.setTransactionManager(transactionManager);
      
        //得到两个分布式Connection
        Connection cn1 = dataSource1.getXAConnection().getConnection();
        Connection cn2 = dataSource2.getXAConnection().getConnection();
      
        try{
            cn1.createStatement().execute("DROP TABLE table1");
            cn2.createStatement().execute("DROP TABLE table2");
        }catch(Exception e){}
        cn1.createStatement().execute("CREATE TABLE table1(id int primary key, money int)");
        cn2.createStatement().execute("CREATE TABLE table2(id int primary key, money int)");
      
        //插入初始数据。
        cn1.createStatement().execute("INSERT INTO table1 VALUES(1, 50)");
        cn2.createStatement().execute("INSERT INTO table2 VALUES(1, 50)");
      
        utx.begin();
        try{
            cn1.createStatement().execute("UPDATE table1 SET money=money-10 WHERE id=1");
            cn2.createStatement().execute("UPDATE table2 SET money=money+10 WHERE id=1");
            //模拟抛出一个业务异常
            int a = 1 / 0;
            utx.commit();
        }catch(Exception e) {
            utx.rollback();
        }
      
        System.out.println(new JdbcTemplate(dataSource1).queryForInt("SELECT money FROM table1 WHERE id=1"));
        System.out.println(new JdbcTemplate(dataSource2).queryForInt("SELECT money FROM table2 WHERE id=1"));
    }

    //启动HSQL,其中有两个数据库testdb1和testdb2。
    private static void startDataBase() {
        Server.main(new String[]{"-database.0", "hsqldb/testdb1", "-dbname.0", "testdb1",  "-database.1", "hsqldb/testdb2", "-dbname.1", "testdb2"});
    }

}

运行结果为:
50
50

说明事务回滚了。

你可能感兴趣的:(分布式事务)