运用jotm实现跨数据库事务控制
使用jotm实现跨数据库事务控制
测试环境:mysql-5.5.14,Oracle 10g Express,ow2-jotm-dist-2.1.9,JDK7,Win7
1,背景说明:两个数据库中分别有一张用户表,表结构设计类似,如下:
MySQL中:
CREATE TABLE TUSER ( id INT, name VARCHAR(10) NOT NULL, PRIMARY KEY (id) )
Oracle中:
create table TUSER ( id int primary key, name VARCHAR2(10 CHAR) not null )系统要求同时保存用户信息到这两个数据库中的用户表中。
2,新建Java Project,将两个数据库的驱动以及jotm中lib目录下的相关jar文件加入buildpath中
3,编写实体类
package com.tanlan.jta.entity; public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
4,编写DAO
package com.tanlan.jta.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import com.tanlan.jta.entity.User; public class UserDAO { /** * 增加用户信息到MySQL数据库中 * * @param user * @param connection * @throws SQLException */ public void addUserToMySQL(User user, Connection connection) throws SQLException { String sql = "insert into TUser values(?,?)"; PreparedStatement pstmt = connection.prepareStatement(sql); pstmt.setInt(1, user.getId()); pstmt.setString(2, user.getName()); pstmt.execute(); } /** * 增加用户信息到Oracle数据库中 * * @param user * @param connection * @throws SQLException */ public void addUserToOracle(User user, Connection connection) throws SQLException { String sql = "insert into TUser values(?,?)"; PreparedStatement pstmt = connection.prepareStatement(sql); pstmt.setInt(1, user.getId()); pstmt.setString(2, user.getName()); pstmt.execute(); } }
5,编写帮助类,实现启动与停止事务管理服务,以及取得数据库连接:
package com.tanlan.jta.dao; import java.sql.Connection; import javax.naming.NamingException; import javax.sql.XAConnection; import javax.transaction.UserTransaction; import org.enhydra.jdbc.standard.StandardXADataSource; import org.objectweb.jotm.Jotm; import org.objectweb.transaction.jta.TMService; public class JotmHelper { private TMService jotm; private UserTransaction userTransaction; /** * 启动事务管理服务 */ public void startTMService() { try { jotm = new Jotm(true, false); userTransaction = jotm.getUserTransaction(); } catch (NamingException e1) { e1.printStackTrace(); } } /** * 取得数据库连接 * * @param db * @return * @throws Exception */ public Connection getConnection(String db) throws Exception { StandardXADataSource xads = new StandardXADataSource(); XAConnection xaconn = null; if ("mysql".equals(db)) { xads.setDriverName("com.mysql.jdbc.Driver"); xads.setUrl("jdbc:mysql://localhost/test"); xads.setTransactionManager(jotm.getTransactionManager()); xaconn = xads.getXAConnection("root", "root"); } else if ("oracle".equals(db)) { xads.setDriverName("oracle.jdbc.driver.OracleDriver"); xads.setUrl("jdbc:oracle:thin:@localhost:1521:XE"); xads.setTransactionManager(jotm.getTransactionManager()); xaconn = xads.getXAConnection("tanlan", "tanlan"); } else { } return xaconn.getConnection(); } public void begin() { try { userTransaction.begin(); } catch (Exception e) { e.printStackTrace(); } } public void commit() { try { userTransaction.commit(); } catch (Exception e) { e.printStackTrace(); } } public void rollback() { try { userTransaction.rollback(); } catch (Exception e) { e.printStackTrace(); } } /** * 停止事务管理服务 */ public void stopTMService() { jotm.stop(); jotm = null; } }
6,测试代码
package com.tanlan.jta.test; import java.sql.Connection; import com.tanlan.jta.dao.JotmHelper; import com.tanlan.jta.dao.UserDAO; import com.tanlan.jta.entity.User; public class TestUSer { public static void main(String[] args) { User user=new User(); user.setId(300); user.setName("a122456"); UserDAO dao = new UserDAO(); JotmHelper helper = new JotmHelper(); try { helper.startTMService(); Connection mysqlConn = helper.getConnection("mysql"); Connection oracleConn = helper.getConnection("oracle"); helper.begin(); dao.addUserToMySQL(user, mysqlConn); dao.addUserToOracle(user, oracleConn); helper.commit(); } catch (Exception e) { helper.rollback(); e.printStackTrace(); } finally { helper.stopTMService(); } } }经过测试,这种方法能够较好的控制对两个数据库操作数据的事务。