利用ThreadLocal控制多个dao调用事务

/* 获取connection 对象* /
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import oracle.jdbc.driver.OracleDriver;

/**
* @author sfluo
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class DBUtil {

public static Connection conn;

public static Connection getConnection() {

try {

DriverManager.registerDriver(new OracleDriver());
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@10.100.143.161:1521:gb02", "gib", "gib");
} catch (SQLException e) {
e.printStackTrace();
}

return conn;
}

}

利用ThreadLocal保存connection对象
package conn;

import java.sql.Connection;
import java.sql.SQLException;

/**
* @author sfluo
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class ConnectionManage {
private static ThreadLocal currentConn = new ThreadLocal();

public static Connection currentConnection() {

Connection conn = (Connection) currentConn.get();
if (conn == null) {
conn = DBUtil.getConnection();
currentConn.set(conn);
openTransaction();
}
return conn;
}

public static void closeConnection() {
try {
Connection conn = (Connection) currentConn.get();
currentConn.set(null);
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void openTransaction() {
try {
Connection conn = currentConnection();
conn.setAutoCommit(false);
conn.setTransactionIsolation(2);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void commit() {
try {
Connection conn = currentConnection();
if (conn != null)
conn.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void roolback() {
try {
Connection conn = currentConnection();
if (conn != null)
conn.rollback();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

被简化的dao实现
package conn;


import java.sql.PreparedStatement;

/**
* @author sfluo
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class DaoA {


public static final String sql = "insert into t_user_bnas (id_globalsession,id_dispseq) values('11200409220000001279','1100')";

void createData() throws Exception {
PreparedStatement pstmt = ConnectionManage.currentConnection().prepareStatement(sql);

pstmt.execute();

pstmt.close();

}

}

package conn;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
* @author sfluo
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class DaoB {
Connection conn;

DaoB() {
this.conn = ConnectionManage.currentConnection();
}

public static final String sql = "update t_user_bnas set id_1dispseq='1111' where id_globalsession='11200409220000001279'";

void createData() throws Exception {
PreparedStatement pstmt = conn.prepareStatement(sql);

pstmt.execute();

pstmt.close();


}
}
测试例子
package conn;

/**
* @author sfluo
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class Test {

public static void main(String args[]) {
Test test = new Test();
test.testMethod();
}

DaoA daoA = new DaoA();

DaoB daoB = new DaoB();

void testMethod() {

try {

daoA.createData();
daoB.createData();
} catch (Exception e) {

ConnectionManage.roolback();

} finally {
ConnectionManage.closeConnection();

}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值