JdbcUtils工具类3.0最终版,添加了事务相关功能和释放链接。最终版本可以直接打成jar包,在后面的基本项目都会使用该工具类

1. JdbcUtils代码

 1 /**
 2  * 最终版
 3  * @author hui.zhang
 4  *
 5  */
 6 public class JdbcUtils {
 7         // 配置文件的默认配置,必须给出c3p0-config.xml
 8         private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
 9         
10         //事务专用连接
11         private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
12         /**
13          * 使用连接池返回一个连接对象
14          * @return
15          * @throws SQLException
16          */
17         public static Connection getConnection() throws SQLException {
18             Connection con = tl.get();
19             //当con比等于null,说明已经开启了事务
20             if(con != null) return con;
21             return dataSource.getConnection();
22         }
23 
24         /**
25          * 返回连接池对象
26          * @return
27          */
28         public static DataSource getDataSource() {
29             return dataSource;
30         }
31         
32         /**
33          * 开启事务
34          * 1. 获取一个Connection,设置它的setAutoCommit(false)
35          * 2. 要保证dao中使用的连接是我们刚刚创建的
36          * @throws SQLException 
37          */
38         public static void beginTransaction() throws SQLException{
39             Connection con = tl.get();
40             if(con != null) throw new SQLException("已经开启了事务,请不要重复开启!");
41             con = getConnection();
42             con.setAutoCommit(false);
43             tl.set(con);
44         }
45         
46         /**
47          * 提交事务
48          * 1. 获取beginTransaction提供的Connection,然后调用commit方法
49          * @throws SQLException 
50          */
51         public static void commitTransaction() throws SQLException{
52             Connection con = tl.get();
53             if(con == null) throw new SQLException("还没有开启事务,不能提交!");
54             con.commit();
55             con.close();
56             tl.remove();
57         }
58         
59         /**
60          * 回滚事务
61          * 1. 获取beginTransaction提供的Connection,然后调用rollback方法
62          * @throws SQLException 
63          */
64         public static void rollbackTransaction() throws SQLException{
65             Connection con = tl.get();
66             if(con == null) throw new SQLException("还没有开启事务,不能回滚!");
67             con.rollback();
68             con.close();
69             tl.remove();
70         }
71         
72         /**
73          * 释放连接
74          * @param connection
75          * @throws SQLException 
76          */
77         public static void releaseConnection(Connection connection) throws SQLException{
78             Connection con = tl.get();
79             //判断是不是事务专用连接,如果是不用关
80             if(con == null)
81                 connection.close();
82             //如果con != null,说明有事务,需要判断参数连接是否与con相同
83             //不同 说明不是事务专用链接
84             if(con != connection)
85                 connection.close();
86         }
87 }

2. 在src下给出c3p0-config.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <!-- 默认配置信息 -->
    <default-config>
    <!-- 连接四大参数 -->
        <property name="user">root</property>
        <property name="password">123</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql:///mydb</property>
    
    <!-- 池参数配置 -->
    <property name="acquireIncrement">3</property> 
    <property name="initialPoolSize">10</property> 
    <property name="minPoolSize">2</property> 
    <property name="maxPoolSize">10</property>
    </default-config>
</c3p0-config> 

3. 总结

  从第一个基本版本1.0到加入连接池2.0再到现在的事务,一步一个脚印。每个版本都应该留下。。。温故而知新!!!

 posted on 2017-09-22 18:17  这名字还可以改吗  阅读(455)  评论(0编辑  收藏  举报