package utils;

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

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JdbcUtils {
    private static final ComboPooledDataSource DATASOURCE = new ComboPooledDataSource();
    private static final ThreadLocal<Connection> TL = new ThreadLocal<Connection>();
    
    public static DataSource getDataSource() {
        return DATASOURCE;
    }
    
    public static Connection getConn() throws SQLException {
        if (TL.get() == null) {
            TL.set(DATASOURCE.getConnection());
        }
        return TL.get();
    }
    
    public static void beginTranscation() throws SQLException {
        if (TL.get() == null) {
            TL.set(DATASOURCE.getConnection());
        }
        TL.get().setAutoCommit(false);
    }
    
    public static void  committranscation() throws SQLException {
        if (TL.get() == null) {
            TL.set(DATASOURCE.getConnection());
        }
        TL.get().commit();
    }
    
    public static void roolbackTranscation() throws SQLException {
        if (TL.get() == null) {
            TL.set(DATASOURCE.getConnection());
        }
        TL.get().rollback();
    }
}

 

posted on 2018-08-20 11:23  zeronexyz  阅读(153)  评论(0编辑  收藏  举报