Servlet开发中JDBC的高级应用(2)
2008-02-23 07:48:22来源:互联网 阅读 ()
public synchronized Connection getConnection() {
Connection con = null;
if (freeConnections.size() > 0) {
// Pick the first Connection in the Vector
// to get round-robin usage
con = (Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
try {
if (con.isClosed()) {
log("Removed bad connection from " name);
// Try again recursively
con = getConnection();
}
}
catch (SQLException e) {
log("Removed bad connection from " name);
// Try again recursively
con = getConnection();
}
}
else if (maxConn == 0 || checkedOut < maxConn) {
con = newConnection();
}
if (con != null) {
checkedOut ;
}
return con;
}
再返回客户之前,isClosed()检查连接是否有效。如果连接被关闭了,或者一个错误发生,该方法递归调用取得另一个连接。
如果没有可用的连接,该方法检查是否最大连接数被设置为0表示无限连接数,或者达到了最大连接数。如果可以创建新的连接,则创建一个新的连接。否则,返回null。
方法newConnection()用来创建一个新的连接。这是一个私有方法,基于用户名和密码来确定是否可以创建新的连接。
private Connection newConnection() {
Connection con = null;
try {
if (user == null) {
con = DriverManager.getConnection(URL);
}
else {
con = DriverManager.getConnection(URL, user, password);
}
log("Created a new connection in pool " name);
}
catch (SQLException e) {
log(e, "Can not create a new connection for " URL);
return null;
}
return con;
}
第二个getConnection()方法带有一个超时参数 timeout,当该参数指定的毫秒数表示客户愿意为一个连接等待的时间。这个方法调用前一个方法。
public synchronized Connection getConnection(long timeout) {
long startTime = new Date().getTime();
Connection con;
while ((con = getConnection()) == null) {
try {
wait(timeout);
}
catch (InterruptedException e) {}
if ((new Date().getTime() - startTime) >= timeout) {
// Timeout has expired
return null;
}
}
return con;
}
五、将一个连接返回池中
DBConnectionPool类中有一个freeConnection方法以返回的连接作为参数,将连接返回连接池。
public synchronized void freeConnection(Connection con) {
// Put the connection at the end of the Vector
freeConnections.addElement(con);
checkedOut--;
notifyAll();
}
六、关闭
大多数servlet引擎提供完整的关闭方法。数据库连接池需要得到通知以正确地关闭所有的连接。DBConnectionManager负责协调关闭事件,但连接由各个连接池自己负责关闭。方法relase()由DBConnectionManager调用。
public synchronized void release() {
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements()) {
Connection con = (Connection) allConnections.nextElement();
try {
con.close();
log("Closed connection for pool " name);
}
catch (SQLException e) {
log(e, "Can not close connection for pool " name);
}
}
freeConnections.removeAllElements();
}
DBConnetionManager的构造函数是私有函数,以避免其他类创建其实例。
private DBConnectionManager() {
init();
}
static synchronized public DBConnectionManager getInstance() {
if (instance == null) {
instance = new DBConnectionManager();
}
clients ;
return instance;
}
单一的实例在第一次调用时创建,以后的调用返回该实例的静态应用。一个计数器纪录所有的客户数,直到客户释放引用。这个计数器在以后用来协调关闭连接池。
一、初始化
构造函数调用一个私有的init()函数初始化对象。
private void init() {
InputStream is = getClass().getResourceAsStream("/db.properties");
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:J2EE初学者需要理解的问题
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
