本文主要是介绍jotm oracle10g oracle 9i,施用jotm实现跨数据库事务控制,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Java代码
packagecom.tanlan.jta.dao;
importjava.sql.Connection;
importjavax.naming.NamingException;
importjavax.sql.XAConnection;
importjavax.transaction.UserTransaction;
importorg.enhydra.jdbc.standard.StandardXADataSource;
importorg.objectweb.jotm.Jotm;
importorg.objectweb.transaction.jta.TMService;
publicclassJotmHelper {
privateTMService jotm;
privateUserTransaction userTransaction;
/**
* 启动事务管理服务
*/
publicvoidstartTMService() {
try{
jotm =newJotm(true,false);
userTransaction = jotm.getUserTransaction();
}catch(NamingException e1) {
e1.printStackTrace();
}
}
/**
* 取得数据库连接
*
* @param db
* @return
* @throws Exception
*/
publicConnection getConnection(String db)throwsException {
StandardXADataSource xads =newStandardXADataSource();
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");
}elseif("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{
}
returnxaconn.getConnection();
}
publicvoidbegin() {
try{
userTransaction.begin();
}catch(Exception e) {
e.printStackTrace();
}
}
publicvoidcommit() {
try{
userTransaction.commit();
}catch(Exception e) {
e.printStackTrace();
}
}
publicvoidrollback() {
try{
userTransaction.rollback();
}catch(Exception e) {
e.printStackTrace();
}
}
/**
* 停止事务管理服务
*/
publicvoidstopTMService() {
jotm.stop();
jotm =null;
}
}
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;
}
}
这篇关于jotm oracle10g oracle 9i,施用jotm实现跨数据库事务控制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!