当前位置: 技术问答>java相关
谁有连结池的例子?
来源: 互联网 发布时间:2015-05-17
本文导语: 谢谢了 | package pool; import java.sql.*; import java.util.*; import java.io.*; class ConnectionReaper extends Thread { private JDCConnectionPool pool; private final long delay=300000; ConnectionReaper(JDCCon...
谢谢了
|
package pool;
import java.sql.*;
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
import java.sql.*;
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
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!