⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbconnectionpool.java

📁 Java数据库连接池的应用
💻 JAVA
字号:
package com.hoperun.connectionPool;
/**
 * DBConnectionPool
 *@author zhou_chenxi
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Timer;

public class DBConnectionPool implements TimerListener {
    //The number of connections in use.
	private int checkedOut;
    //load the connection in the free connectionpool.
	public static ArrayList<Connection> freeConnections=new ArrayList<Connection>();
    //The min number of connections in connectionpool.
	private int minConn;
    //The max number of connections in connectionpool.
	private int maxConn;
    //The name of connectionpool.
	private String name;
	//The url of the datebase.
	private String url;
    //The uername to connect the datebase.
	private String user;
    //The password to connect the datebase.
	private String password;
    //Timer.
	public Timer timer;
	
    /**
     * The public structure function.
     * @param con
     */
	public DBConnectionPool(String name,String URL,String user,String password,int maxConn){
		this.name=name;
		this.url=URL;
        this.user=user;
	    this.password=password;
	    this.maxConn=maxConn;
	}
	
	/**
	 * Return the connection to connectionpool while finishing using.
	 * @param con
	 */
	public synchronized void freeConnection(Connection con){
        //Add to the end of the free connectionpool.
		freeConnections.add(con);
        //The conected number reduce.
		checkedOut--; 
	}
	

    /**
     * Get a connection丆the timeout is the time to wait. 
     * @param timeout
     */
	public synchronized Connection getConnection(long timeout){
		//New a connection.
		Connection con = null;
		//If there is a free connection.Get it!
		if(freeConnections.size()>0){
		   con=(Connection)freeConnections.get(0);
		   //Remove the first element. 
		   freeConnections.remove(0);
		   //If there is no connection,continue get.
		   if(con==null)
			   con=getConnection(timeout); 
		  }
		//New a connection if the last step failure.
		else con=newConnection(); 
		//If have not set a maxConn or the checkedOut is full.Can't new aconnection.
		if(maxConn==0||maxConn<=checkedOut){
			con=null;
			}
		if(con!=null){
			//Make the connection number add 1.
		   checkedOut++;
		   }
		return con;
		}
	
    /**
     * Break the connection丆release the resource
     */
	public synchronized void release(){
		//If there are connections.Get them first.
		if (freeConnections.size()>0) {
        	for(int i=0;i<freeConnections.size();i++){
        	Connection con=(Connection)freeConnections.get(i);
        	//Close the connections.
        	if (con!=null)
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
        	}
		}
		freeConnections.clear();
	}
	
    /**
     * New a connection.
     */
	private Connection newConnection(){
		  Connection con = null;    
          try {    
              if (user == null) {    
                  con = DriverManager.getConnection(url);    
              } else {    
                  con = DriverManager.getConnection(url, user, password);    
              } 
          } catch (SQLException e) {    
        	  e.printStackTrace();
              return null;    
          }    
          return con;    
      } 
	
    //timer	
	public synchronized void timerEvent(){
		
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -