当前位置: 技术问答>java相关
我找了个ConnectionPool,请问怎么用呢?
来源: 互联网 发布时间:2015-06-16
本文导语: 已经编译通过 import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.text.*; import java.sql.*; import java.awt.*; import java.awt.image.*; public class ConnectionPool { static private ConnectionPool instance; ...
已经编译通过
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.text.*;
import java.sql.*;
import java.awt.*;
import java.awt.image.*;
public class ConnectionPool
{
static private ConnectionPool instance; //生成本类的唯一实例
private int client;
private String userName;
private String passWord;
private Vector freeConn = new Vector();
private String dbURL;
private int maxConn;
private PrintWriter logPw;
/**
*返回该类的唯一实例,如没有,则创建
**/
static synchronized public ConnectionPool getInstance()
{
if(instance == null)
{
instance = new ConnectionPool();
}
return instance;
}
private ConnectionPool()
{
super();
String logfile = "c:/log.log";
userName = "aa";
passWord = "aa";
dbURL = "jdbc:oracle:thin:@192.168.0.251:1521:oracle";
try
{
maxConn = Integer.parseInt("100");
}
catch(NumberFormatException e)
{
log("error in format number",e);
}
try
{
logPw = new PrintWriter(new FileWriter(logfile,true),true);
}
catch(IOException ioException)
{
//com.evoln.tool.file.PrintMessage("ConnectionPool.java","ConnectionPool","ConnectionFile",ioException.toString());
}
loadDrivers();
}
private void loadDrivers()
{
String driverName = "oracle.jdbc.driver.OracleDriver";
try
{
Driver driver = (Driver)Class.forName(driverName).newInstance();
DriverManager.registerDriver(driver);
log("success in register driver: " + driverName);
}
catch(Exception e)
{
log("error in register driver: " + driverName,e);
}
}
private void log(String message)
{
//logPw.println(datetime.DateToStr() + " " + datetime.TimeToStr() + ": " + message);
}
private void log(String message,Throwable e)
{
//logPw.println(datetime.DateToStr() + " " + datetime.TimeToStr() + ": " + message + " " + e.toString());
}
//将不在使用的连接返回连接池
public synchronized void freeConnection(Connection conn)
{
freeConn.addElement(conn);
client--;
notifyAll();
}
//从连接池中得到一个连接
public synchronized Connection getConnection()
{
Connection conn = null;
if(freeConn.size() > 0)
{
conn = (Connection)freeConn.firstElement();
freeConn.removeElementAt(0);
try
{
if(conn.isClosed())
{
conn = getConnection();
log("delete a connection from pool");
}
}
catch(SQLException sqlException)
{
conn = getConnection();
log("error in delete a connection from pool",sqlException);
}
}
else if(client = timeout)
{
return null;
}
}
return conn;
}
private Connection newConnection()
{
Connection conn = null;
try
{
if(userName.equals(null))
{
conn = DriverManager.getConnection(dbURL);
}
else
{
conn = DriverManager.getConnection(dbURL,userName,passWord);
}
}
catch(SQLException sqlException)
{
log("error in create new connection " ,sqlException);
}
return conn;
}
public synchronized void closeAllConn()
{
Enumeration allConn = freeConn.elements();
while(allConn.hasMoreElements())
{
Connection conn = (Connection)allConn.nextElement();
try
{
conn.close();
}
catch(SQLException sqlException)
{
log("error in close connection ",sqlException);
}
}
}
}
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.text.*;
import java.sql.*;
import java.awt.*;
import java.awt.image.*;
public class ConnectionPool
{
static private ConnectionPool instance; //生成本类的唯一实例
private int client;
private String userName;
private String passWord;
private Vector freeConn = new Vector();
private String dbURL;
private int maxConn;
private PrintWriter logPw;
/**
*返回该类的唯一实例,如没有,则创建
**/
static synchronized public ConnectionPool getInstance()
{
if(instance == null)
{
instance = new ConnectionPool();
}
return instance;
}
private ConnectionPool()
{
super();
String logfile = "c:/log.log";
userName = "aa";
passWord = "aa";
dbURL = "jdbc:oracle:thin:@192.168.0.251:1521:oracle";
try
{
maxConn = Integer.parseInt("100");
}
catch(NumberFormatException e)
{
log("error in format number",e);
}
try
{
logPw = new PrintWriter(new FileWriter(logfile,true),true);
}
catch(IOException ioException)
{
//com.evoln.tool.file.PrintMessage("ConnectionPool.java","ConnectionPool","ConnectionFile",ioException.toString());
}
loadDrivers();
}
private void loadDrivers()
{
String driverName = "oracle.jdbc.driver.OracleDriver";
try
{
Driver driver = (Driver)Class.forName(driverName).newInstance();
DriverManager.registerDriver(driver);
log("success in register driver: " + driverName);
}
catch(Exception e)
{
log("error in register driver: " + driverName,e);
}
}
private void log(String message)
{
//logPw.println(datetime.DateToStr() + " " + datetime.TimeToStr() + ": " + message);
}
private void log(String message,Throwable e)
{
//logPw.println(datetime.DateToStr() + " " + datetime.TimeToStr() + ": " + message + " " + e.toString());
}
//将不在使用的连接返回连接池
public synchronized void freeConnection(Connection conn)
{
freeConn.addElement(conn);
client--;
notifyAll();
}
//从连接池中得到一个连接
public synchronized Connection getConnection()
{
Connection conn = null;
if(freeConn.size() > 0)
{
conn = (Connection)freeConn.firstElement();
freeConn.removeElementAt(0);
try
{
if(conn.isClosed())
{
conn = getConnection();
log("delete a connection from pool");
}
}
catch(SQLException sqlException)
{
conn = getConnection();
log("error in delete a connection from pool",sqlException);
}
}
else if(client = timeout)
{
return null;
}
}
return conn;
}
private Connection newConnection()
{
Connection conn = null;
try
{
if(userName.equals(null))
{
conn = DriverManager.getConnection(dbURL);
}
else
{
conn = DriverManager.getConnection(dbURL,userName,passWord);
}
}
catch(SQLException sqlException)
{
log("error in create new connection " ,sqlException);
}
return conn;
}
public synchronized void closeAllConn()
{
Enumeration allConn = freeConn.elements();
while(allConn.hasMoreElements())
{
Connection conn = (Connection)allConn.nextElement();
try
{
conn.close();
}
catch(SQLException sqlException)
{
log("error in close connection ",sqlException);
}
}
}
}
|
ConnectionPool myPool = ConnectionPool.getInstance();//得到链接池实例
Connection conn=myPool.getConnection(3000);//返回一个链接,设置3秒钟超时
PreparedStatement pstmt = conn.prepareStatement(strSQL);
ResultSet rs = pstmt.executeQuery();
Connection conn=myPool.getConnection(3000);//返回一个链接,设置3秒钟超时
PreparedStatement pstmt = conn.prepareStatement(strSQL);
ResultSet rs = pstmt.executeQuery();
|
你必须把不用的连接用
freeConnection方法返回给连接池
不然pool怎么知道哪个连接没有使用
freeConnection方法返回给连接池
不然pool怎么知道哪个连接没有使用
|
好处就是可以控制链接数量和共享对数据库的链接
全局可以调用该链接,有效的程序不会每次都去建立链接
用完就释放链接,可以用pool进行对链接管理
全局可以调用该链接,有效的程序不会每次都去建立链接
用完就释放链接,可以用pool进行对链接管理
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。