解决druid的线程同步机制,想同步curd的时候不用再传入connection连接,让它们得到相同的连接
package com.shayiheng.api.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.shayiheng.api.druid.DruidUsePart;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcUtilsV2 {
private static DataSource dataSource=null;
private static ThreadLocal<Connection> t1=new ThreadLocal<>();
static {
Properties properties=new Properties();
InputStream ips = DruidUsePart.class.getClassLoader().getResourceAsStream("druid.properties");
try {
properties.load(ips);
}catch (IOException e){
throw new RuntimeException(e);
}
try {
dataSource= DruidDataSourceFactory.createDataSource(properties);
}catch (Exception e){
throw new RuntimeException(e);
}
}
public static Connection getConnection() throws SQLException {
Connection connection=t1.get();
if (connection==null){
connection=dataSource.getConnection();
t1.set(connection);
}
return connection;
}
public static void freeConnection() throws SQLException {
Connection connection=t1.get();
if (connection!=null) {
t1.remove();
connection.setAutoCommit(true);
connection.close();
}
}
}
package transactionnew;
import com.shayiheng.api.utils.JdbcUtilsV2;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class BankDao {
public void add(String account,int money) throws ClassNotFoundException, SQLException {
Connection connection = JdbcUtilsV2.getConnection();
String sql= "update t_bank set money = money + ? where account= ? ;";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setObject(1,money);
statement.setObject(2,account);
statement.executeUpdate();
statement.close();
System.out.println("加钱成功");
}
public void sub(String account,int money) throws ClassNotFoundException, SQLException {
Connection connection = JdbcUtilsV2.getConnection();
String sql= "update t_bank set money = money - ? where account= ? ;";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setObject(1,money);
statement.setObject(2,account);
statement.executeUpdate();
statement.close();
System.out.println("减钱成功");
}
}
package transactionnew;
import com.shayiheng.api.utils.JdbcUtilsV2;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class BankServer {
@Test
public void start() throws SQLException, ClassNotFoundException {
transfer("ergouzi","e",500);
}
public void transfer(String addAccount,String subAccount,int money) throws SQLException, ClassNotFoundException {
BankDao bankDao = new BankDao();
Connection connection = JdbcUtilsV2.getConnection();
try{
connection.setAutoCommit(false);
bankDao.add(addAccount,money);
System.out.println("----------");
bankDao.sub(subAccount,money);
connection.commit();
}catch (Exception e){
connection.rollback();
throw e;
}finally {
JdbcUtilsV2.freeConnection();
}
}
}