private static ThreadLocal<Connection> t1=new ThreadLocal<Connection>();
来源:互联网 发布:php date 时区 编辑:程序博客网 时间:2024/05/03 09:26
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);
}
}
}
}
- private static ThreadLocal<Connection> t1=new ThreadLocal<Connection>();
- private static ThreadLocal<Connection> t1=new ThreadLocal<Connection>();
- 采用ThreadLocal维护Connection
- 采用ThreadLocal维护Connection
- ThreadLocal封装Connection--事务统一管理
- ThreadLocal封装Connection--事务统一管理
- ThreadLocal封装Connection--事务统一管理
- ThreadLocal封装Connection--事务统一管理
- ThreadLocal封装Connection--事务统一管理
- 用ThreadLocal来管理Connection和Transaction
- ThreadLocal对Connection封装的实现
- 使用ThreadLocal封装connection的属性
- ThreadLocal<T> threadLocal = new ThreadLocal<T>(); threadLocal.set(T param); 增加一个线程局部变量
- ThreadLocal封装connection--隔离了相关资源,防止线程安全问题
- 关于用 ThreadLocal 管理 Connection 的一些总结
- 【java项目实战】ThreadLocal封装Connection,实现同一线程共享资源
- 为避免多线程Connection混乱使用ThreadLocal来封装事务
- 【菜鸟学Java】14:使用ThreadLocal对Connection进行封装
- String sql="update person set name=? where id=?";
- java数组
- 免证书在MacOSX10.8.2上使用XCode4.5.2上真机调试
- 对话 UNIX: 更多 shell 脚本技术
- S3C DMA使用方法,2410-2440 dma介绍
- private static ThreadLocal<Connection> t1=new ThreadLocal<Connection>();
- Android退出整个应用之完美解决方案
- 关于Cisco交换机接口模式的详细介绍
- 开源代码Tree20
- [js][json]前台页面中json和字符串相互转化
- [EK-LM3S8962]简易版智能家居:lwIP TCP/IP协议栈在ARM开发板实现嵌入式网络服务器(上)
- 口语228
- [cocos2dx]bin/resources.ap_ does not exist 的解决方法
- 推送相关的学习资料