ThreadLocal的原理是使用一个hashMap保存connection,而键为当前线程即Thread.currentThread()
下面先简单模拟一个ThreadLocal
public class MyThreadLocal <T> {
private Map<Thread, T> map = new HashMap<Thread, T>();
public void set(T data) {
// 使用当前线程做key
map.put(Thread.currentThread(), data);
}
public T get() {
return map.get(Thread.currentThread());
}
public void remove() {
map.remove(Thread.currentThread());
}
}
写一个支持事务的dao,需要提供getconnection(),beginTransaction(),commitTransaction(),rollBackTransaction()的方法
使用该getTXConnection()调包之前DAO的CRUD 的getConnection()方法,并把connection 存放到ThreadLocal中
public static Connection getTXConnection(){
Connection connection = tl.get();
if (connection==null){
try {
connection = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
tl.set(connection);
}
return connection;
}
beginTransaction开启事务
public static void beginTransaction() {
Connection con = tl.get();
if (con==null){
try {
con = dataSource.getConnection();
con.setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
tl.set(con);
}else{
throw new RuntimeException("你已经开启事务了");
}
}
commitTransaction提交事务,关闭连接,移除ThreadLocal中的connection
public static void commitTransaction(){
Connection con = tl.get();
if (con==null){
throw new RuntimeException("你还没有开启事务");
}else {
try {
con.commit();
con.close();
tl.remove();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
rollBackTransaction回滚事务,关闭连接,移除ThreadLocal中的connection
public static void rollBackTransaction() {
Connection con = tl.get();
if (con==null){
throw new RuntimeException("你还没有开启事务");
}else {
try {
con.rollback();
con.close();
tl.remove();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
对于close方法,对于事务而言,是不需要关闭的,关闭在commitTransaction和rollBackTransaction中,但为了兼容没有开启事务的CRUD,这里需要判断
public static void close(Connection con, PreparedStatement ps) {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
Connection connection = tl.get();
//不是事务connection
if (connection==null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}else {
//也不是事务connection
if (con!=connection){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
接下来是加强之前写的DAO了,这里只展示update方法
public class JDBCUtils {
private static BasicDataSource dataSource;
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
static {
dataSource = new BasicDataSource();
Properties properties = new Properties();
InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
try {
properties.load(in);
dataSource.setDriverClassName(properties.getProperty("driver"));
dataSource.setUrl(properties.getProperty("jdbcUrl"));
dataSource.setUsername(properties.getProperty("user"));
dataSource.setPassword(properties.getProperty("password"));
} catch (IOException e) {
e.printStackTrace();
}
}
//insert update delete
public static void update(String sql,Object... args){
Connection connection = getTXConnection();
PreparedStatement ps=null;
try {
ps = connection.prepareStatement(sql,Statement);
for (int i=0;i<args.length;i++){
ps.setObject(i+1,args[i]);
}
ps.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}
JDBCUtils.close(connection,ps);
}
}