[Java] 纯文本查看 复制代码
import java.sql.Connection;
import java.sql.SQLException;
import com.wuming.c3p0utils.C3P0Utils;
public class ConnectionMangager {
private static ThreadLocal<Connection> local=new ThreadLocal<>();
public static Connection getConnection() throws SQLException{
Connection con = local.get();
if(con==null){
con=C3P0Utils.getConnection();
local.set(con);
}
return con;
}
public static void setAutoCommit() throws SQLException{
getConnection().setAutoCommit(false);
}
public static void commit() throws SQLException{
getConnection().commit();
}
public static void rollback() throws SQLException{
getConnection().rollback();
}
public static void close(){
try {
getConnection().close();
local.remove();
} catch (SQLException e) {
e.printStackTrace();
}
}
}