温故而知新(一)JDBC封装

package com.JDBC.firstJDBC;

import java.util.*;
import java.io.*;
import java.sql.*;

/*此类用于封装Java程序链接数据库所需要配置的JDBC操作
 * 
 * */
public class SingletonJDBCUtil{
	private static Properties info=new Properties();
	private static Connection conn=null;
	
	static{
		try{
			//用于读取配置文件中的字节流
			//getResourceAsStream方法用于类加载器在加载的时候并加载制指定的资源。
			 InputStream is=JDBCUtil.class.getResourceAsStream("/com" +
			 		"/JDBC/firstJDBC/config.properties");
			 
			 info.load(is);
			 is.close();
		}catch(Exception e){
			//此处只能跑出此种异常,这种异常专门用来在初始化快中抛出的
			throw new ExceptionInInitializerError(e);
		}
	}
	
	//返回一个连接
	public static Connection getConnection()throws Exception{
		
      if(conn==null){
		Class.forName(info.getProperty("driver"));
		conn=DriverManager.getConnection(info.getProperty("url"),
				 info.getProperty("username"),info.getProperty("pwd"));	
		}
      return conn;
	}
	//释放一个资源
	public static void release(ResultSet rs,Statement stm,Connection cn){
		if(rs!=null) try{rs.close();}catch(Exception e){}
		if(stm!=null) try{stm.close();}catch(Exception e){}
		if(cn!=null) try{cn.close();}catch(Exception e){}
	} 

}
  但上述连接代码存在严重的资源浪费的情况,比如任何一个对象只要调用JDBCUtil的getConnection()方法,就会得到一个连接,这样大大的浪费了资源,以下通过singleton设计模式进行改进后的代码如下:
 
package com.JDBC.firstJDBC;

import java.util.*;
import java.io.*;
import java.sql.*;

/*此类用于封装Java程序链接数据库所需要配置的JDBC操作
 * 
 * */
public class SingletonJDBCUtil{
	private static Properties info=new Properties();
	private static Connection conn=null;
	
	static{
		try{
			//用于读取配置文件中的字节流
			//getResourceAsStream方法用于类加载器在加载的时候并加载制指定的资源。
			 InputStream is=JDBCUtil.class.getResourceAsStream("/com" +
			 		"/JDBC/firstJDBC/config.properties");
			 
			 info.load(is);
			 is.close();
		}catch(Exception e){
			//此处只能跑出此种异常,这种异常专门用来在初始化快中抛出的
			throw new ExceptionInInitializerError(e);
		}
	}
	
	//返回一个连接
	public static Connection getConnection()throws Exception{
		
      if(conn==null){
		Class.forName(info.getProperty("driver"));
		conn=DriverManager.getConnection(info.getProperty("url"),
				 info.getProperty("username"),info.getProperty("pwd"));	
		}
      return conn;
	}
	//释放一个资源
	public static void release(ResultSet rs,Statement stm,Connection cn){
		if(rs!=null) try{rs.close();}catch(Exception e){}
		if(stm!=null) try{stm.close();}catch(Exception e){}
		if(cn!=null) try{cn.close();}catch(Exception e){}
	} 

}
 这种代码仍然存在问题,当多个线程同时调用该连接的时候,当某一个线程关闭该连接后,其他线程由于还有某些操作没有提交而导致业务的错误.因此,需要一个线程一个连接,代码如下:

  

package com.JDBC.firstJDBC;

import java.util.*;
import java.io.*;
import java.sql.*;

/*此类用于封装Java程序链接数据库所需要配置的JDBC操作
 * 
 * */
public class BestUtil{
	
	private static Properties info=new Properties();
	
	
	static{
		try{
			//用于读取配置文件中的字节流
			//getResourceAsStream方法用于类加载器在加载的时候并加载制指定的资源。
			 InputStream is=JDBCUtil.class.getResourceAsStream("/com" +
			 		"/JDBC/firstJDBC/config.properties");
			 
			 info.load(is);
			 is.close();
		}catch(Exception e){
			//此处只能跑出此种异常,这种异常专门用来在初始化快中抛出的
			throw new ExceptionInInitializerError(e);
		}
	}
	//返回一个连接
	private static final ThreadLocal<Connection> tl=new ThreadLocal<Connection>(); 
	public static Connection getConnection()throws Exception{
	  Connection conn=tl.get();
	  if(conn==null){
		Class.forName(info.getProperty("driver"));
		conn=DriverManager.getConnection(info.getProperty("url"),
				 info.getProperty("username"),info.getProperty("pwd"));	
		         tl.set(conn);
	  }
	  return conn;
	}
	//释放一个资源
	public static void release(ResultSet rs,Statement stm,Connection cn){
		if(rs!=null) try{rs.close();}catch(Exception e){}
		if(stm!=null) try{stm.close();}catch(Exception e){}
		if(cn!=null) try{cn.close();}catch(Exception e){}
	}
}

 

这种模式解决了在前面出现并发操作的问题,是一种你比较优秀的数据连接封装操作,其中用到了ThreadLocal类,每一个线程都有一个用于存储线程的局部变量(线程独占数据,用Map对象来存储数据):由于线程的局部变是属于java.lang包的 defalut类型,因此,要访问该对象就必须用调用ThreadLocal对象来获的里面的对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值