JDBCUtils工具類(轉)

JdbcUtils.javajava

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
 * 依賴:
 *        + c3p0-config.xml
 *        + c3p0-0.9.2-pre1.jar
 *        + mchange-commons-0.2.jar
 * 版本1.3 
 * 更新日期:2018/07/03
 * @author CeoBai
 *
 */
public class JdbcUtils {
    //ComboPooledDataSource(String configName)的參數configName指的是配置文件c3p0-config.xml中的 <named-config name="mysql">...</named-config>
    //若是沒有輸入configName參數,那麼就採用默認的<default-config>...</defalut-config>,那麼就能夠爲空不傳參數
    private static ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql");
    private static Connection con = null;
    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
    /**
     * 獲取鏈接對象
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException{
        con = tl.get();
        if(con != null){return con;}
        return dataSource.getConnection();
    }
    /**
     * 獲取鏈接池對象
     * @return
     */
    public static DataSource getDataSource(){
        return dataSource;
    }
    /**
     * 開啓事務
     * @throws SQLException
     */
    public static void beginTransaction() throws SQLException{
        con = tl.get();
        if(con != null){throw new RuntimeException("事務已經開啓!不能重複開啓!");}
        con = getConnection();
        con.setAutoCommit(false);
        tl.set(con);
    }
    /**
     * 提交事務
     * @throws SQLException
     */
    public static void commitTransaction() throws SQLException{
        con = tl.get();
        if(con == null){throw new RuntimeException("事務還沒開啓!不能提交!");}
        con.commit();
        con.close();//歸還鏈接對象到鏈接池
        tl.remove();//移除鏈接對象con。那麼tl.get() == null
    }
    /**
     * 回滾事務
     * @throws SQLException
     */
    public static void rollbackTransaction() throws SQLException{
        con = tl.get();
        if(con == null){throw new RuntimeException("事務還沒開啓!不能回滾!");}
        con.rollback();
        con.close();
        tl.remove();
    }
    /**
     * 關閉 非事務專用的鏈接
     * @param connection
     * @throws SQLException
     */
    public static void releaseConnection(Connection connection) throws SQLException{
        con = tl.get();//獲取事務專用鏈接
        if(con == null){connection.close();}
        if(con != connection){connection.close();}
    } 
}

c30p-config.xmlmysql

<?xml version="1.0" encoding="UTF-8" ?>
<c3p0-config>
    <!-- c3p0有兩種配置方式,一種是默認的default-config,一種是按名字區分:named-config須要添加一個屬性值name -->
    <default-config> 
        <property name="jdbcUrl">jdbc:oracle:thin:username/password@amrood:1521:EMP</property>
        <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
        <property name="user">root</property>
        <property name="password"></property>
        
        <property name="acquireIncrement">3</property>
        <property name="initialPoolSize">10</property>
        <property name="minPoolSize">2</property>
        <property name="maxPoolSize">10</property>
    </default-config>
    <named-config name="mysql"> 
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/db_user</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="user">root</property>
        <property name="password"></property>
        
        <property name="acquireIncrement">3</property>
        <property name="initialPoolSize">10</property>
        <property name="minPoolSize">2</property>
        <property name="maxPoolSize">10</property>
    </named-config>
</c3p0-config>
相關文章
相關標籤/搜索