http://www.csdn.net/expert/topic/607/607845.xml?temp=.3101313
解决方案 »
- IBM Rational Application Developer J2C 连接SAP的困惑
- 关于socket通信问题
- 如何把系统时间转换成unix时间
- 唉~JTabbedPane的问题,请老大们帮忙吧
- 异常处理的问题,请大家帮我解决,谢谢!
- 谁知道哪有《21天学通Java2第三版》的电子书下载,最好是英文版
- 用jad反编译某些文件后,会有 JVM INSTR...类似的代码,这是什么?
- java时间问题
- list中如何统一列中不同名字元素的个数
- 怎样实现监控各服务器
- 連結 Mysql 失敗
- 请问各位,哪里有好的OA系统介绍啊,现我们要做一个java版的OA系统,没这方面的经验,请大家给点这方面的资料啊,谢谢啦
import java.util.*;
import java.io.*;class ConnectionReaper extends Thread { private JDCConnectionPool pool;
private final long delay=300000; ConnectionReaper(JDCConnectionPool pool) {
this.pool=pool;
} public void run() {
while(true) {
try {
sleep(delay);
} catch( InterruptedException e) { }
pool.reapConnections();
}
}
}public class JDCConnectionPool { private Vector connections;
private String url, user, password;
final private long timeout=60000;
private ConnectionReaper reaper;
final private int poolsize=10; public JDCConnectionPool(String url, String user, String password) {
this.url = url;
this.user = user;
this.password = password;
connections = new Vector(poolsize);
reaper = new ConnectionReaper(this);
reaper.start();
} public synchronized void reapConnections() { long stale = System.currentTimeMillis() - timeout;
Enumeration connlist = connections.elements();
while((connlist != null) && (connlist.hasMoreElements())) {
JDCConnection conn = (JDCConnection)connlist.nextElement(); if((conn.inUse()) && (stale >conn.getLastUse()) &&
(!conn.validate())) {
removeConnection(conn);
}
}
} public synchronized void closeConnections() {
Enumeration connlist = connections.elements(); while((connlist != null) && (connlist.hasMoreElements())) {
JDCConnection conn = (JDCConnection)connlist.nextElement();
removeConnection(conn);
}
} private synchronized void removeConnection(JDCConnection conn) {
connections.removeElement(conn);
}
public synchronized Connection getConnection() throws SQLException { JDCConnection c;
for(int i = 0; i < connections.size(); i++) {
c = (JDCConnection)connections.elementAt(i);
if (c.lease()) {
return c;
}
} Connection conn = DriverManager.getConnection(url, user, password);
c = new JDCConnection(conn, this);
c.lease();
connections.addElement(c);
return c;
} public synchronized void returnConnection(JDCConnection conn) {
conn.expireLease();
}
}
import java.util.*;
import java.io.*;public class JDCConnection implements Connection { private JDCConnectionPool pool;
private Connection conn;
private boolean inuse;
private long timestamp;
public JDCConnection(Connection conn, JDCConnectionPool pool) {
this.conn=conn;
this.pool=pool;
this.inuse=false;
this.timestamp=0;
} public synchronized boolean lease() {
if(inuse) {
return false;
} else {
inuse=true;
timestamp=System.currentTimeMillis();
return true;
}
}
public boolean validate() {
try {
conn.getMetaData();
}catch (Exception e) {
return false;
}
return true;
} public boolean inUse() {
return inuse;
} public long getLastUse() {
return timestamp;
} public void close() throws SQLException {
pool.returnConnection(this);
} protected void expireLease() {
inuse=false;
} protected Connection getConnection() {
return conn;
} public PreparedStatement prepareStatement(String sql) throws SQLException {
return conn.prepareStatement(sql);
} public CallableStatement prepareCall(String sql) throws SQLException {
return conn.prepareCall(sql);
} public Statement createStatement() throws SQLException {
return conn.createStatement();
} public String nativeSQL(String sql) throws SQLException {
return conn.nativeSQL(sql);
} public void setAutoCommit(boolean autoCommit) throws SQLException {
conn.setAutoCommit(autoCommit);
} public boolean getAutoCommit() throws SQLException {
return conn.getAutoCommit();
} public void commit() throws SQLException {
conn.commit();
} public void rollback() throws SQLException {
conn.rollback();
} public boolean isClosed() throws SQLException {
return conn.isClosed();
} public DatabaseMetaData getMetaData() throws SQLException {
return conn.getMetaData();
} public void setReadOnly(boolean readOnly) throws SQLException {
conn.setReadOnly(readOnly);
}
public boolean isReadOnly() throws SQLException {
return conn.isReadOnly();
} public void setCatalog(String catalog) throws SQLException {
conn.setCatalog(catalog);
} public String getCatalog() throws SQLException {
return conn.getCatalog();
} public void setTransactionIsolation(int level) throws SQLException {
conn.setTransactionIsolation(level);
} public int getTransactionIsolation() throws SQLException {
return conn.getTransactionIsolation();
} public SQLWarning getWarnings() throws SQLException {
return conn.getWarnings();
} public void clearWarnings() throws SQLException {
conn.clearWarnings();
}
}
import java.util.*;
public class JDCConnectionDriver implements Driver { public static final String URL_PREFIX = "jdbc:jdc:";
private static final int MAJOR_VERSION = 1;
private static final int MINOR_VERSION = 0;
private JDCConnectionPool pool; public JDCConnectionDriver(String driver, String url,
String user, String password)
throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
SQLException
{
DriverManager.registerDriver(this);
Class.forName(driver).newInstance();
pool = new JDCConnectionPool(url, user, password);
} public Connection connect(String url, Properties props)
throws SQLException {
if(!url.startsWith(URL_PREFIX) {
return null;
}
return pool.getConnection();
} public boolean acceptsURL(String url) {
return url.startsWith(URL_PREFIX);
} public int getMajorVersion() {
return MAJOR_VERSION;
} public int getMinorVersion() {
return MINOR_VERSION;
} public DriverPropertyInfo[] getPropertyInfo(String str, Properties props) {
return new DriverPropertyInfo[0];
} public boolean jdbcCompliant() {
return false;
}
}
还有个管理连接池的类(DBConnectManager)没写出来,应该比较简单!
DBConnectManager应该是对DBConnectionPool的封装,并保证生成一个或
DBConnectionPool的实例!
import oracle.jdbc.driver.OracleDriver;
import java.sql.*;
import java.util.*;public class DBConnectionPool {private static String driver = "oracle.jdbc.driver.OracleDriver";
private String url = "JDBC:Oracle:thin:@soft2server:1521:oracledb";
private Vector pool = new Vector();
private String user ="dickensi";
private String password = "112233";
private static int MAXCON = 10; public DBConnectionPool() {
} public boolean newConnection(){
Connection con = null;
if(pool.size() < MAXCON)
{
try{
con = DriverManager.getConnection(url,user,password);
}
catch(Exception e){System.out.println("Database connect failure!");}
pool.add(con);
System.out.println("Create a connection["+(pool.size()-1)+"]"+con.toString());
return true;
}
return false;
} public Connection getConnection(){
if(pool.size() <= 0)
{
while(newConnection());
}
Connection con = (Connection) pool.firstElement();
pool.remove(0);
return con;
} public void realseConnection(Connection con){
try{
if(pool.size()<10){
pool.add(con);
}
else{con.close();}
con = null;
}
catch(Exception e){}
} public void realeasePool(){
Connection con = null;
for(int i=0;i<pool.size();i++)
{
con = (Connection)pool.get(i);
try{
con.close();
}
catch(Exception e){}
}
pool.clear();
System.out.println("realease all pool to "+ pool.size());
} public void getPoolInfo(){
for(int i =0;i<pool.size();i++)
{
System.out.println(""+i+pool.get(i).toString());
}
} public static void main(String[] args) {
Connection[] con = new Connection[20];
try{
Class.forName(driver);
}
catch(Exception e)
{System.out.println("Db driver load failure!");}
DBConnectionPool dbPool = new DBConnectionPool();
while(dbPool.newConnection()) ;
System.out.println("----------------------------initial---------------------------");
// dbPool.getPoolInfo();
for(int i=0;i<15;i++)
{
con[i] = dbPool.getConnection();
System.out.println("----------------------------get one---------------------------");
dbPool.getPoolInfo();
}
dbPool.getPoolInfo();
for(int i=14;i>=0;i--)
{
// System.out.println("realease:"+con[i].toString());
// try{
// con[i].close();
// }catch (Exception ee){}
dbPool.realseConnection(con[i]);
System.out.println("----------------------------realease one---------------------------");
// dbPool.getPoolInfo();
}
System.out.println("--------------------------realease all------------------------");
dbPool.getPoolInfo();
dbPool.realeasePool();
}
}