zoukankan      html  css  js  c++  java
  • JavaWeb基础之JdbcUtils工具类final

    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再到现在的事务,一步一个脚印。每个版本都应该留下。。。温故而知新!!!

  • 相关阅读:
    杨玲 201771010133《面向对象程序设计(java)》第六周学习总结
    杨玲 201771010133《面向对象程序设计(java)》第五周学习总结
    【Alpha】Scrum meeting 3
    【Alpha】Scrum meeting 2
    【Alpha】Scrum meeting 1
    实验八 团队作业四:团队项目需求分析建模与系统设计
    超越队 实验七 团队作业3 :团队项目需求分析与原型设计
    超越队 实验六 团队作业2 :校园失物招领系统
    超越队 实验五 团队作业1:软件研发团队组建与软件案例分析
    【Alpha】Scrum meeting 3
  • 原文地址:https://www.cnblogs.com/stefan95/p/7576366.html
Copyright © 2011-2022 走看看