Apache-dbutils 簡介及事務處理
阿新 • • 發佈:2019-01-02
/**
* @ClassName: JdbcUtils2
* @Description: 資料庫連線工具類
* @author:
* @date:
*
*/
public class JdbcUtils2 {
private static ComboPooledDataSource ds = null;
//使用ThreadLocal儲存當前執行緒中的Connection物件
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
//在靜態程式碼塊中建立資料庫連線池
static{
try{
//通過程式碼建立C3P0資料庫連線池
/*ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/jdbcstudy");
ds.setUser("root");
ds.setPassword("XDP");
ds.setInitialPoolSize(10);
ds.setMinPoolSize(5);
ds.setMaxPoolSize(20);*/
//通過讀取C3P0的xml配置檔案建立資料來源,C3P0的xml配置檔案c3p0-config.xml必須放在src目錄下
//ds = new ComboPooledDataSource();//使用C3P0的預設配置來建立資料來源
ds = new ComboPooledDataSource("MySQL");//使用C3P0的命名配置來建立資料來源
}catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
/**
* @Method: getConnection
* @Description: 從資料來源中獲取資料庫連線
* @Anthor:
* @return Connection
* @throws SQLException
*/
public static Connection getConnection() throws SQLException{
//從當前執行緒中獲取Connection
Connection conn = threadLocal.get();
if(conn==null){
//從資料來源中獲取資料庫連線
conn = getDataSource().getConnection();
//將conn繫結到當前執行緒
threadLocal.set(conn);
}
return conn;
}
/**
* @Method: startTransaction
* @Description: 開啟事務
* @Anthor:
*
*/
public static void startTransaction(){
try{
Connection conn = threadLocal.get();
if(conn==null){
conn = getConnection();
//把 conn繫結到當前執行緒上
threadLocal.set(conn);
}
//開啟事務
conn.setAutoCommit(false);
}catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Method: rollback
* @Description:回滾事務
* @Anthor:
*/
public static void rollback(){
try{
//從當前執行緒中獲取Connection
Connection conn = threadLocal.get();
if(conn!=null){
//回滾事務
conn.rollback();
}
}catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Method: commit
* @Description:提交事務
* @Anthor:
*/
public static void commit(){
try{
//從當前執行緒中獲取Connection
Connection conn = threadLocal.get();
if(conn!=null){
//提交事務
conn.commit();
}
}catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Method: close
* @Description:關閉資料庫連線(注意,並不是真的關閉,而是把連線還給資料庫連線池)
* @Anthor:
*
*/
public static void close(){
try{
//從當前執行緒中獲取Connection
Connection conn = threadLocal.get();
if(conn!=null){
conn.close();
//解除當前執行緒上繫結conn
threadLocal.remove();
}
}catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Method: getDataSource
* @Description: 獲取資料來源
* @Anthor:
* @return DataSource
*/
public static DataSource getDataSource(){
//從資料來源中獲取資料庫連線
return ds;
}
}
* @ClassName: JdbcUtils2
* @Description: 資料庫連線工具類
* @author:
* @date:
*
*/
public class JdbcUtils2 {
private static ComboPooledDataSource ds = null;
//使用ThreadLocal儲存當前執行緒中的Connection物件
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
//在靜態程式碼塊中建立資料庫連線池
static{
try{
//通過程式碼建立C3P0資料庫連線池
/*ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/jdbcstudy");
ds.setUser("root");
ds.setPassword("XDP");
ds.setInitialPoolSize(10);
ds.setMinPoolSize(5);
ds.setMaxPoolSize(20);*/
//通過讀取C3P0的xml配置檔案建立資料來源,C3P0的xml配置檔案c3p0-config.xml必須放在src目錄下
//ds = new ComboPooledDataSource();//使用C3P0的預設配置來建立資料來源
ds = new ComboPooledDataSource("MySQL");//使用C3P0的命名配置來建立資料來源
}catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
/**
* @Method: getConnection
* @Description: 從資料來源中獲取資料庫連線
* @Anthor:
* @return Connection
* @throws SQLException
*/
public static Connection getConnection() throws SQLException{
//從當前執行緒中獲取Connection
Connection conn = threadLocal.get();
if(conn==null){
//從資料來源中獲取資料庫連線
conn = getDataSource().getConnection();
//將conn繫結到當前執行緒
threadLocal.set(conn);
}
return conn;
}
/**
* @Method: startTransaction
* @Description: 開啟事務
* @Anthor:
*
*/
public static void startTransaction(){
try{
Connection conn = threadLocal.get();
if(conn==null){
conn = getConnection();
//把 conn繫結到當前執行緒上
threadLocal.set(conn);
}
//開啟事務
conn.setAutoCommit(false);
}catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Method: rollback
* @Description:回滾事務
* @Anthor:
*/
public static void rollback(){
try{
//從當前執行緒中獲取Connection
Connection conn = threadLocal.get();
if(conn!=null){
//回滾事務
conn.rollback();
}
}catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Method: commit
* @Description:提交事務
* @Anthor:
*/
public static void commit(){
try{
//從當前執行緒中獲取Connection
Connection conn = threadLocal.get();
if(conn!=null){
//提交事務
conn.commit();
}
}catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Method: close
* @Description:關閉資料庫連線(注意,並不是真的關閉,而是把連線還給資料庫連線池)
* @Anthor:
*
*/
public static void close(){
try{
//從當前執行緒中獲取Connection
Connection conn = threadLocal.get();
if(conn!=null){
conn.close();
//解除當前執行緒上繫結conn
threadLocal.remove();
}
}catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Method: getDataSource
* @Description: 獲取資料來源
* @Anthor:
* @return DataSource
*/
public static DataSource getDataSource(){
//從資料來源中獲取資料庫連線
return ds;
}
}