From 965fc47173f22e9220fb7acfed50e808208d52ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BA=9C=E5=BC=BA?= <8569561+fuqiangma@user.noreply.gitee.com> Date: Tue, 10 Jan 2023 02:15:24 +0000 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20threadlo?= =?UTF-8?q?caldemo/ThreadLocal=5FDemo/demo02/src/com/woniu/transfer/utils/?= =?UTF-8?q?JdbcUtils.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/woniu/transfer/utils/JdbcUtils.java | 72 ------------------- 1 file changed, 72 deletions(-) delete mode 100644 threadlocaldemo/ThreadLocal_Demo/demo02/src/com/woniu/transfer/utils/JdbcUtils.java diff --git a/threadlocaldemo/ThreadLocal_Demo/demo02/src/com/woniu/transfer/utils/JdbcUtils.java b/threadlocaldemo/ThreadLocal_Demo/demo02/src/com/woniu/transfer/utils/JdbcUtils.java deleted file mode 100644 index 82b857e..0000000 --- a/threadlocaldemo/ThreadLocal_Demo/demo02/src/com/woniu/transfer/utils/JdbcUtils.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.itheima.transfer.utils; - -import com.mchange.v2.c3p0.ComboPooledDataSource; -import java.sql.Connection; -import java.sql.SQLException; - -public class JdbcUtils { - static ThreadLocal tl = new ThreadLocal<>(); - // c3p0 数据库连接池对象属性 - private static final ComboPooledDataSource ds = new ComboPooledDataSource(); - // 获取连接 - /* - * 原本: 直接从连接池中获取连接 - * 现在: - * 1. 直接获取当前线程绑定的连接对象 - * 2. 如果连接对象是空的 - * 2.1 再去连接池中获取连接 - * 2.2 将此连接对象跟当前线程进行绑定 - * */ - public static Connection getConnection() throws SQLException { - Connection conn = tl.get(); - if(conn == null){ - conn = ds.getConnection(); - tl.set(conn); - } - return conn; -// return ds.getConnection(); - } - //释放资源 - public static void release(AutoCloseable... ios){ - for (AutoCloseable io : ios) { - if(io != null){ - try { - io.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - } - - public static void commitAndClose(Connection conn) { - try { - if(conn != null){ - //提交事务 - conn.commit(); - //解绑当前线程绑定的连接对象 - tl.remove(); - //释放连接 - conn.close(); - } - } catch (SQLException e) { - e.printStackTrace(); - } - } - - public static void rollbackAndClose(Connection conn) { - try { - if(conn != null){ - //回滚事务 - conn.rollback(); - //解绑当前线程绑定的连接对象 - tl.remove(); - //释放连接 - conn.close(); - } - } catch (SQLException e) { - e.printStackTrace(); - } - } - -}