自己封裝的DatasourceUtils類
阿新 • • 發佈:2019-03-26
.com source get() vat hang color set stat close
本工具類用於獲取連接池和數據庫連接
package com.itheima.utils; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DataSourceUtils { private staticDataSource dataSource = new ComboPooledDataSource(); private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); public static DataSource getDataSource() { return dataSource; } public static Connection getConnection_V1() throwsSQLException { return dataSource.getConnection(); } public static Connection getConnection() throws SQLException { Connection con = tl.get(); if (con == null) { con = dataSource.getConnection(); tl.set(con); }return con; } public static void startTransaction() throws SQLException { Connection con = getConnection(); if (con != null) { con.setAutoCommit(false); } } public static void rollback() throws SQLException { Connection con = getConnection(); if (con != null) { con.rollback(); } } public static void commitAndRelease() throws SQLException { Connection con = getConnection(); if (con != null) { con.commit(); // ?????? con.close();// ?????? tl.remove();// ??????????? } } public static void closeConnection() throws SQLException { Connection con = getConnection(); if (con != null) { con.close(); } } public static void closeStatement(Statement st) throws SQLException { if (st != null) { st.close(); } } public static void closeResultSet(ResultSet rs) throws SQLException { if (rs != null) { rs.close(); } } }
自己封裝的DatasourceUtils類