至于Java数据库连接池的原理就不细说了,百度一下大把。在这里只是把个人学习中的结果积累下来。
代码上也基本都有注释。
首先写一个读取本地数据库驱动,数据库用户名、数据库密码、连接数的类。
- XML/HTML 代码复制内容到剪贴板
- package cn.cate.utils;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Properties;
- public class PoolProperties {
- private Properties properties;
- private String driver;
- private String url;
- private String user;
- private String password;
- private int maxConn;
- private int minConn;
- public PoolProperties(){
- properties = new Properties();
- String path = getClass().getProtectionDomain().getCodeSource().getLocation().toString();
- pathpathpath = path.substring(6, path.length());
- if (path.indexOf("WEB-INF") > 0) {
- pathpathpath = path.substring(0, path.indexOf("/WEB-INF") + 9) + "lib/";
- }else{
- path = "d:";
- }
- try {
- this.properties.load(new FileReader(path + "db.pro"));
- }catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch (IOException e) {
- e.printStackTrace();
- }
- this.setDriver(properties.getProperty("driver"));
- this.setUrl(properties.getProperty("url"));
- this.setUser(properties.getProperty("user"));
- this.setPassword(properties.getProperty("password"));
- this.setMaxConn(Integer.parseInt(properties.getProperty("maxConn")));
- this.setMinConn(Integer.parseInt(properties.getProperty("minConn")));
- }
- public Properties getProperties() {
- return properties;
- }
- public void setProperties(Properties properties) {
- this.properties = properties;
- }
- public String getDriver() {
- return driver;
- }
- public void setDriver(String driver) {
- this.driver = driver;
- }
- public String getUrl() {
- return url;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- public String getUser() {
- return user;
- }
- public void setUser(String user) {
- this.user = user;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public int getMaxConn() {
- return maxConn;
- }
- public void setMaxConn(int maxConn) {
- this.maxConn = maxConn;
- }
- public int getMinConn() {
- return minConn;
- }
- public void setMinConn(int minConn) {
- this.minConn = minConn;
- }
- }
db.pro的内容如下:
driver=com.mysql.jdbc.Driver maxConn=10 minConn=1 url=jdbc:mysql://127.0.0.1:3306/cate user=root password=11111
然后定义一个数据库连接池:DBConnectionPool
- Java 代码复制内容到剪贴板
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- /*
- * 此内部类定义了一个连接池,它能够根据要求创建连接,直到预定的最大连接数为止。
- * 在返回连接给客户程序之前,它能够验证连接的有效性。
- */
- public class DBConnectionPool {
- /*
- * 连接池
- */
- private List<Connection> freeConnections = new ArrayList<Connection>();
- private Connection conn = null;
- private int connect = 0; //使用的连接数
- private int maxConn; //最大连接数
- private String driver; //数据库驱动
- private String url; //数据库连接地址
- private String user; //用户名
- private String password; //用户名
- public DBConnectionPool(String driver,String URL,String user,String password,int maxConn){
- this.driver = driver;
- this.url = URL;
- this.user = user;
- this.password = password;
- this.maxConn = maxConn;
- poolInfo();
- }
- /*
- * 显示准备创建连接池的信息
- */
- private void poolInfo(){
- Connection conn = this.newConnection();
- freeConnections.add(conn);
- for(int i = 0;i < this.maxConn - 1;i++){
- Connection freeConn = conn;
- freeConnections.add(freeConn);
- }
- }
- /*
- * 用完,释放一个连接
- */
- public synchronized void freeConnection(Connection conn){
- this.freeConnections.add(conn);
- this.connect--;
- }
- /*
- * 从连接池中获取一个可用连接,当无法从连接池中获取可用连接时,新创建一个连接
- */
- public synchronized Connection getConnection(){
- if(this.freeConnections.size() > 0){
- /*
- * 当在连接池中取出一个连接后,删除此连接
- */
- conn = this.freeConnections.get(0);
- this.freeConnections.remove(0);
- }
- /*
- * 当取出的连接为null时,递归调用自己,直到获得一个可用连接为止
- */
- if(conn == null){
- conn = getConnection();
- }else{
- conn = newConnection();
- }
- if(this.maxConn == 0 || this.maxConn < this.connect){
- /*
- * 等待超过最大连接时
- */
- conn = null;
- }
- if(conn != null){
- this.connect++;
- }
- return conn;
- }
- /*
- * 释放全部连接
- */
- public synchronized void release(){
- Iterator<Connection> allConns = this.freeConnections.iterator();
- while(allConns.hasNext()){
- Connection conn = (Connection)allConns.next();
- try{
- if(null != conn){
- conn.close();
- }
- conn = null;
- }catch(SQLException e){
- e.printStackTrace();
- }
- }
- this.freeConnections.clear();
- }
- /*
- * 创建一个数据库连接对象
- */
- private Connection newConnection(){
- try{
- Class.forName(driver);
- }catch(ClassNotFoundException e2){
- e2.printStackTrace();
- }
- try{
- conn = DriverManager.getConnection(url,user,password);
- }catch(SQLException e3){
- e3.printStackTrace();
- System.exit(0);
- }
- return conn;
- }
- }
最后提供一个连接池的管理类:
- Java 代码复制内容到剪贴板
- import java.sql.Connection;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import cn.cate.utils.PoolProperties;
- /*
- * 连接池的管理类,负责读取配置连接池的文件,并创建连接池
- * 从连接池中获取,释放连接
- */
- public class DBConnectionManager {
- /*
- * 唯一数据库连接池管理实例类
- * 使用单例模式创建
- */
- private static DBConnectionManager instance;
- /*
- * 连接池的集合
- */
- private Hashtable<String,DBConnectionPool> pools = new Hashtable<String,DBConnectionPool>();
- private static String poolName = "MYSQL_MZBA"; //连接池名字
- public static synchronized DBConnectionManager getInstance(){
- if(instance == null){
- instance = new DBConnectionManager();
- }
- return instance;
- }
- /*
- * 只允许内部实例化管理类
- */
- private DBConnectionManager(){
- this.init();
- }
- /*
- * 加载驱动程序
- */
- private void init(){
- PoolProperties poolProperties = new PoolProperties();
- DBConnectionPool pool = new DBConnectionPool(poolProperties.getDriver(), poolProperties.getUrl(),
- poolProperties.getUser(), poolProperties.getPassword(), poolProperties.getMaxConn());
- pools.put(poolName, pool);
- }
- /*
- * 根据连接池的名字得到一个连接
- */
- public Connection getConnection(){
- DBConnectionPool pool = null;
- Connection conn = null;
- pool = pools.get(poolName);
- try{
- conn = pool.getConnection();
- }catch(Exception e){
- e.printStackTrace();
- }
- return conn;
- }
- /*
- * 释放一个连接
- */
- public synchronized void freeConnection(Connection conn){
- DBConnectionPool pool = pools.get(poolName);
- if(pool != null){
- pool.freeConnection(conn);
- }
- }
- /*
- * 释放所有连接
- */
- public synchronized void release(){
- Enumeration<DBConnectionPool> allpools = pools.elements();
- while(allpools.hasMoreElements()){
- DBConnectionPool pool = allpools.nextElement();
- if(pool != null){
- pool.release();
- }
- }
- pools.clear();
- }
- }
本文转自06peng 51CTO博客,原文链接:http://blog.51cto.com/06peng/962458,如需转载请自行联系原作者