网上图书商城项目学习笔记-035工具类之JdbcUtils及TxQueryRunner及C3P0配置
生活随笔
收集整理的這篇文章主要介紹了
网上图书商城项目学习笔记-035工具类之JdbcUtils及TxQueryRunner及C3P0配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
事務就是保證多個操作在同一個connection,TxQueryRunner通過JdbcUtils獲取連接,而JdbcUtils通過ThreadLocal<Connection>確保了不同線程設置的con不會混淆(tl.set(con)),而同一線程的connecion可以共用,從而具有事務的功能
1.JdbcUtils.java
1 package cn.itcast.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.SQLException; 5 6 import javax.sql.DataSource; 7 8 import com.mchange.v2.c3p0.ComboPooledDataSource; 9 10 /** 11 * 使用本類的方法,必須提供c3p0-copnfig.xml文件 12 * @author qdmmy6 13 */ 14 public class JdbcUtils { 15 // 餓漢式 16 private static DataSource ds = new ComboPooledDataSource(); 17 18 /** 19 * 它為null表示沒有事務 20 * 它不為null表示有事務 21 * 當開啟事務時,需要給它賦值 22 * 當結束事務時,需要給它賦值為null 23 * 并且在開啟事務時,讓dao的多個方法共享這個Connection 24 */ 25 private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); 26 27 public static DataSource getDataSource() { 28 return ds; 29 } 30 31 /** 32 * dao使用本方法來獲取連接 33 * @return 34 * @throws SQLException 35 */ 36 public static Connection getConnection() throws SQLException { 37 /* 38 * 如果有事務,返回當前事務的con 39 * 如果沒有事務,通過連接池返回新的con 40 */ 41 Connection con = tl.get();//獲取當前線程的事務連接 42 if(con != null) return con; 43 return ds.getConnection(); 44 } 45 46 /** 47 * 開啟事務 48 * @throws SQLException 49 */ 50 public static void beginTransaction() throws SQLException { 51 Connection con = tl.get();//獲取當前線程的事務連接 52 if(con != null) throw new SQLException("已經開啟了事務,不能重復開啟!"); 53 con = ds.getConnection();//給con賦值,表示開啟了事務 54 con.setAutoCommit(false);//設置為手動提交 55 tl.set(con);//把當前事務連接放到tl中 56 } 57 58 /** 59 * 提交事務 60 * @throws SQLException 61 */ 62 public static void commitTransaction() throws SQLException { 63 Connection con = tl.get();//獲取當前線程的事務連接 64 if(con == null) throw new SQLException("沒有事務不能提交!"); 65 con.commit();//提交事務 66 con.close();//關閉連接 67 con = null;//表示事務結束! 68 tl.remove(); 69 } 70 71 /** 72 * 回滾事務 73 * @throws SQLException 74 */ 75 public static void rollbackTransaction() throws SQLException { 76 Connection con = tl.get();//獲取當前線程的事務連接 77 if(con == null) throw new SQLException("沒有事務不能回滾!"); 78 con.rollback(); 79 con.close(); 80 con = null; 81 tl.remove(); 82 } 83 84 /** 85 * 釋放Connection 86 * @param con 87 * @throws SQLException 88 */ 89 public static void releaseConnection(Connection connection) throws SQLException { 90 Connection con = tl.get();//獲取當前線程的事務連接 91 if(connection != con) {//如果參數連接,與當前事務連接不同,說明這個連接不是當前事務,可以關閉! 92 if(connection != null &&!connection.isClosed()) {//如果參數連接沒有關閉,關閉之! 93 connection.close(); 94 } 95 } 96 } 97 }2.TxQueryRunner
1 package cn.itcast.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.SQLException; 5 6 import org.apache.commons.dbutils.QueryRunner; 7 import org.apache.commons.dbutils.ResultSetHandler; 8 9 public class TxQueryRunner extends QueryRunner { 10 11 @Override 12 public int[] batch(String sql, Object[][] params) throws SQLException { 13 Connection con = JdbcUtils.getConnection(); 14 int[] result = super.batch(con, sql, params); 15 JdbcUtils.releaseConnection(con); 16 return result; 17 } 18 19 @Override 20 public <T> T query(String sql, ResultSetHandler<T> rsh, Object... params) 21 throws SQLException { 22 Connection con = JdbcUtils.getConnection(); 23 T result = super.query(con, sql, rsh, params); 24 JdbcUtils.releaseConnection(con); 25 return result; 26 } 27 28 @Override 29 public <T> T query(String sql, ResultSetHandler<T> rsh) throws SQLException { 30 Connection con = JdbcUtils.getConnection(); 31 T result = super.query(con, sql, rsh); 32 JdbcUtils.releaseConnection(con); 33 return result; 34 } 35 36 @Override 37 public int update(String sql) throws SQLException { 38 Connection con = JdbcUtils.getConnection(); 39 int result = super.update(con, sql); 40 JdbcUtils.releaseConnection(con); 41 return result; 42 } 43 44 @Override 45 public int update(String sql, Object param) throws SQLException { 46 Connection con = JdbcUtils.getConnection(); 47 int result = super.update(con, sql, param); 48 JdbcUtils.releaseConnection(con); 49 return result; 50 } 51 52 @Override 53 public int update(String sql, Object... params) throws SQLException { 54 Connection con = JdbcUtils.getConnection(); 55 int result = super.update(con, sql, params); 56 JdbcUtils.releaseConnection(con); 57 return result; 58 } 59 }3.c3p0-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <c3p0-config><default-config> <property name="jdbcUrl"><![CDATA[jdbc:mysql://localhost:3306/goods?useUnicode=true&characterEncoding=UTF8&useServerPrepStmts=true&prepStmtCacheSqlLimit=256&cachePrepStmts=true&prepStmtCacheSize=256&rewriteBatchedStatements=true]]></property><property name="driverClass">com.mysql.jdbc.Driver</property><property name="user">root</property><property name="password">1234</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>?
轉載于:https://www.cnblogs.com/shamgod/p/5181997.html
總結
以上是生活随笔為你收集整理的网上图书商城项目学习笔记-035工具类之JdbcUtils及TxQueryRunner及C3P0配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (王道408考研数据结构)第三章栈和队列
- 下一篇: 字符串经典题之扑克牌的大小