Connection Pooling JAVA Implementation __java

Source: Internet
Author: User
Package com.dalong.connectionpool;
Import Java.util. HashMap;
Import Java.util.Vector;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.*;
/**
* <p>title: </p>
* <p>Description:test</p>
* <p>copyright:copyright (c) 2003</p>
* <p>Company:home</p>
* @author Dalong
* @version 1.0
*/

public class ConnectionPool {

The manager of the connection pool, initialized first, has only one object, manages the connection pool
private static HashMap connectionpoolmanager=new HashMap ();
No used connection pool, using vector to achieve synchronization
private static Vector Nouseconnectionpool;
No used connection pool
private static HashMap Nowuseconnectionpool;

private static String dbdriver= "Odbc:jdbc:OdbcJdbcDriver";
private static String dburl= "Dalong@xx";
private static String username= "Dalong";
private static String userpassword= "Dalong";

Default to 100 connection pools
private static int max_pool=100;

SingleTon design Pattern
Private ConnectionPool (String driver,string url,string name,string password,int max)
Throws ClassNotFoundException {
Class.forName (driver);
Dburl=url;
Username=name;
Userpassword=password;
Max_pool=max;
}
public static ConnectionPool getconnmanagerinstance (String poolname)
Throws classnotfoundexception{
ConnectionPool temppool= (ConnectionPool) connectionpoolmanager.get (poolname);
if (temppool==null) {
Temppool=new ConnectionPool (Dbdriver,dburl,username,userpassword,max_pool);
Connectionpoolmanager.put (Poolname,temppool);
return temppool;
}else
return temppool;
}

Get a real link through a connection pool
public static Connection getconnection () throws java.sql.sqlexception{
Connection Conn=null;
Synchronized (Nouseconnectionpool) {
if (Nouseconnectionpool.size () >0) {
Conn= (Connection) nouseconnectionpool.firstelement ();
Nouseconnectionpool.remove (conn);
Return conn;
}
}
If the database connection pool does not have a link, create a
if (conn==null) {
Conn=createconnection (Dbdriver,dburl,username,userpassword);
}else if (conn.isclosed ()) {
Nowuseconnectionpool.remove (conn);
Conn=createconnection (Dbdriver,dburl,username,userpassword);
}
Conn.setautocommit (FALSE);
Nowuseconnectionpool.put (Conn,conn);
Return conn;
}

If the connection pool is not linked, you need to produce a link
private static Connection createconnection (String driver,string url,string user,string password)
Throws java.sql.sqlexception{
Connection conn=drivermanager.getconnection (Url,user,password);
Return conn;
}
public static void Releaseconnection (Connection conn,boolean iscommit)
Throws java.sql.sqlexception{
if (iscommit)
Conn.commit ();
Else
Conn.rollback ();
Nowuseconnectionpool.remove (conn);
if (nouseconnectionpool.size () + nowuseconnectionpool.size () <max_pool) {
Synchronized (Nouseconnectionpool) {
NOUSECONNECTIONPOOL.ADD (conn);
}
}else{
Conn.close ();
}
}

public static void Main (string[] args) {
Test simulation 10 Customers
for (int i = 0; i < i++) {
try {
XXXX is typically read as a property file
ConnectionPool pool = connectionpool.getconnmanagerinstance ("xxxx");
Connection conn = Pool.getconnection ();

}catch (SQLException ex1) {
Handling Exceptions
}
catch (ClassNotFoundException ex) {
Handling Exceptions
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.