Durid连接池工具类的封装使用

V 1.0(封装创建连接、回收连接)

public abstract class DruidToolsVersion1 {
//使用静态代码块和静态类,调用的时候可以直接类名.方法调用而不用类实例化
    private static DataSource ds;
    static {
        try {
            Properties properties = new Properties();//创建properties对象
            File file = new File("cfg/driudsoft.properties");//配置文件
            FileInputStream fileInputStream = new FileInputStream(file);//读取配置文件
            properties.load(fileInputStream);//加载配置文件
            ds= DruidDataSourceFactory.createDataSource(properties);//通过工厂方法创建对象
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static Connection getConnnextion() throws SQLException {
        Connection connection = ds.getConnection();//获取连接
        return connection;
    }
    public static void collectConnection(Connection connection) throws SQLException {
        connection.close();
    }
}
//调用的时候
Connection connnection = DruidToolsVersion1.getConnnection();//建立连接
// curd
DruidToolsVersion1.collectConnection(connnextion);//回收连接

V 2.0(同一个线程的不同方法获取同一个连接)

  • 在转账事务的实现中,流程是:在srevice层统一创建驱动和连接,并把连接对象传递给加钱和减钱的方法,所以Dao层中的加减钱方法都传入了连接connection。因为在service、Dao层使用的连接都是同一个线程在调用,所以考虑能不能让二者都调用同一个线程,因此引入线程本地变量,在封装的工具类中创建一个ThreadLocal线程本地变量,在service层和Dao层调用封装的工具类创建连接,这样就可以保证service和Dao层是同样的连接。
  • ThreadLocal用于保存某个线程共享变量,不同线程只能从中get,set,remove自己的变量,而不会影响其他线程的变量。get、set、remove用于获取、设置、移除ThreadLocal中当前线程共享变量的值。
public class DruidToolsVersion2 {
    private static ThreadLocal<Connection> tl=new ThreadLocal<>();//创建线程本地变量
    private static DataSource ds;
    static {
        try {
            Properties properties = new Properties();
            File file = new File("cfg/druidsoft.properties");
            FileInputStream fileInputStream = new FileInputStream(file);
            properties.load(fileInputStream);
            ds=DruidDataSourceFactory.createDataSource(properties);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    public static Connection getConnection() throws SQLException {
        Connection connection = tl.get();
        //先看是不是第一次建立连接,如果是那就先获取连接并复制给本地变量,
        //如果不是说明之前已经获取过了,直接从线程本地变量返回connection即可
        if(connection==null){
            connection = ds.getConnection();
            tl.set(connection);
        }
        return connection;
    }
    public static void collectConnection() throws SQLException {
        Connection connection = tl.get();
        if(connection!=null){
           tl.remove();
           connection.setAutoCommit(true);
           connection.close();
        }
    }
}
在转账事务中的调用:
service层不用在创建驱动建立连接了,直接调用DruidToolsVersion2.getconnection()DruidToolsVersion2.collectconnection()来建立连接和释放连接
Dao层也不用再传入connection变量了,直接调用DruidToolsVersion2.getconnection()获取连接
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值