📄 connectionpool.java
字号:
/**
* Copyright (C) 2003 Manfred Andres
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package freecs.auth.sqlConnectionPool;
import freecs.Server;
import java.sql.*;
import java.util.Properties;
import java.util.Vector;
import java.util.Enumeration;
public class ConnectionPool {
String url, user, password;
Properties props;
PoolElement pool[];
int p = 0, size =0, idCnt=0, maxStatements;
long validity;
public ConnectionPool (int size, String url, String user, String password, int maxStatements, long validity) throws Exception {
this.url = url;
this.size = size;
this.user = user;
this.password = password;
this.maxStatements = maxStatements;
this.validity = validity;
pool = new PoolElement[size];
}
/**
* close every connection to the jdbc-source and throw an exception if there was an error
* @throws Exception
*/
public void shutdown () throws Exception {
for (int i = 0; i < pool.length; i++) {
if (pool[i] == null) continue;
pool[i].cleanup ();
}
}
/**
* creates an connectionpool-element
* @return the PoolElement connected to the jdbc-datasource
* @throws Exception
*/
private PoolElement createPoolElement () throws Exception {
idCnt++;
if (idCnt == Integer.MAX_VALUE) idCnt = 0;
Connection con = DriverManager.getConnection (url, user, password);
return new PoolElement (con, maxStatements, validity, idCnt);
}
/**
* Gets the next available and valid poolelement or tryes to create a new one
* (after connectionpool-size tryes we will throw an exception and login will fail because
* of technical error)
* @return the valid and available PoolElement
* @throws Exception
*/
public PoolElement getPoolElement () throws Exception {
int c = 0, fc = 0;
if (p >= size) p = 0;
PoolElement el = null;
Vector v = new Vector ();
synchronized (pool) {
while (el == null) {
for ( ; c < size; c++, p++) {
if (p >= size) {
p = 0;
}
el = pool[p];
if (el == null || !el.isValid ()) try {
pool[p] = el = createPoolElement ();
} catch (Exception e) {
if (!v.contains (e.toString ())) v.addElement (e.toString ());
continue;
} else if (el.isActive ()) {
continue;
}
break;
}
if (el != null && el.isValid ())
break;
// if there is an invalid element, we will call cleanup on this poolelement
// freeing resources and making it possible to reestablis a new PoolElement
if (el != null) try {
el.cleanup ();
el = null;
} catch (Exception e) {
Server.debug ("ConnectionPool.getPoolElement: error during cleaning up invalid PoolElement", e, Server.MSG_ERROR, Server.LVL_MAJOR);
return null;
}
fc++;
if (fc > size) {
StringBuffer sb = new StringBuffer ("Got no available connection during last ").append (size).append (" runs. The exceptions are as follows:\r\n ");
for (Enumeration e = v.elements (); e.hasMoreElements (); ) {
String ex = (String) e.nextElement ();
sb.append ("\r\n ").append (ex);
}
throw new Exception (sb.toString ());
}
}
el.setActive ();
return el;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -