package com.tfy.itheima.jdbc.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
public class DbcpUtil {
private static ThreadLocal<Connection> t1=new ThreadLocal<Connection>();
private static DataSource ds=null;
static{
Properties props=new Properties();
InputStream in=DbcpUtil.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
try {
props.load(in);
ds=BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
public static DataSource getDataSource(){
return ds;
}
public static Connection getConnection(){
try {
Connection conn=t1.get();
if(conn==null){
conn=ds.getConnection();
t1.set(conn);
}
return conn;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public void release(ResultSet rs,Statement stmt,Connection conn){
if(rs!=null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(stmt!=null){
try{
stmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void commit() {
Connection conn=t1.get();
if(conn!=null){
try{
conn.commit();
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void release() {
Connection conn=t1.get();
if(conn!=null){
try{
conn.close();
t1.remove();
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void startTransaction() {
Connection conn=t1.get();
if(conn==null){
// conn=t1.get();这里取的是t1中的Connection
conn=getConnection();
t1.set(conn);
}
try{
conn.setAutoCommit(false);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void rollback() {
Connection conn=t1.get();
if(conn!=null){
try{
conn.rollback();
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
public class DbcpUtil {
private static ThreadLocal<Connection> t1=new ThreadLocal<Connection>();
private static DataSource ds=null;
static{
Properties props=new Properties();
InputStream in=DbcpUtil.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
try {
props.load(in);
ds=BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
public static DataSource getDataSource(){
return ds;
}
public static Connection getConnection(){
try {
Connection conn=t1.get();
if(conn==null){
conn=ds.getConnection();
t1.set(conn);
}
return conn;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public void release(ResultSet rs,Statement stmt,Connection conn){
if(rs!=null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(stmt!=null){
try{
stmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void commit() {
Connection conn=t1.get();
if(conn!=null){
try{
conn.commit();
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void release() {
Connection conn=t1.get();
if(conn!=null){
try{
conn.close();
t1.remove();
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void startTransaction() {
Connection conn=t1.get();
if(conn==null){
// conn=t1.get();这里取的是t1中的Connection
conn=getConnection();
t1.set(conn);
}
try{
conn.setAutoCommit(false);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void rollback() {
Connection conn=t1.get();
if(conn!=null){
try{
conn.rollback();
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}