Kommentar hinzugefügt

git-svn-id: https://svn.libreccm.org/ccm/trunk@884 8810af33-2d31-482b-a856-94f89814c4df
master
quasi 2011-04-30 19:46:07 +00:00
parent 3e139db6bb
commit 8267552268
1 changed files with 33 additions and 34 deletions

View File

@ -41,27 +41,23 @@ import org.apache.log4j.Logger;
* @author Rafael H. Schloming <rhs@mit.edu> * @author Rafael H. Schloming <rhs@mit.edu>
* @version $Id: PooledConnectionSource.java 885 2005-09-20 15:54:06Z sskracic $ * @version $Id: PooledConnectionSource.java 885 2005-09-20 15:54:06Z sskracic $
**/ **/
public class PooledConnectionSource implements ConnectionSource { public class PooledConnectionSource implements ConnectionSource {
private static final Logger s_log = private static final Logger s_log =
Logger.getLogger(PooledConnectionSource.class); Logger.getLogger(PooledConnectionSource.class);
private static CacheTable s_connectionTags = private static CacheTable s_connectionTags =
new CacheTable("jdbcConnectionTags", new CacheTable("jdbcConnectionTags",
RuntimeConfig.getConfig().getJDBCPoolSize() + 10, RuntimeConfig.getConfig().getJDBCPoolSize() + 10,
CacheTable.MAX_CACHE_AGE, CacheTable.MAX_CACHE_AGE,
false); false);
private String m_url; private String m_url;
private int m_size; private int m_size;
private long m_interval; private long m_interval;
private Set m_connections = new HashSet(); private Set m_connections = new HashSet();
private List m_available = new ArrayList(); private List m_available = new ArrayList();
private List m_untested = new ArrayList(); private List m_untested = new ArrayList();
private static boolean s_taggingEnabled = private static boolean s_taggingEnabled =
RuntimeConfig.getConfig().isThreadTaggingEnabled(); RuntimeConfig.getConfig().isThreadTaggingEnabled();
public PooledConnectionSource(String url, int size, long interval) { public PooledConnectionSource(String url, int size, long interval) {
m_url = url; m_url = url;
@ -94,7 +90,7 @@ public class PooledConnectionSource implements ConnectionSource {
} else { } else {
if (s_log.isDebugEnabled()) { if (s_log.isDebugEnabled()) {
s_log.debug("Reacquisition failed: " + pref s_log.debug("Reacquisition failed: " + pref
+ ", tag: " + s_connectionTags.get(pref.toString())); + ", tag: " + s_connectionTags.get(pref.toString()));
} }
return acquire(); return acquire();
} }
@ -113,8 +109,9 @@ public class PooledConnectionSource implements ConnectionSource {
renameThread(result); renameThread(result);
return result; return result;
} else { } else {
try { wait(); } try {
catch (InterruptedException e) { wait();
} catch (InterruptedException e) {
throw new UncheckedWrapperException(e); throw new UncheckedWrapperException(e);
} }
} }
@ -150,12 +147,12 @@ public class PooledConnectionSource implements ConnectionSource {
String sql = ""; String sql = "";
String tag = ""; String tag = "";
switch (database) { switch (database) {
case DbHelper.DB_POSTGRES: case DbHelper.DB_POSTGRES:
sql = "select pg_backend_pid() as tag"; sql = "select pg_backend_pid() as tag";
break; break;
case DbHelper.DB_ORACLE: case DbHelper.DB_ORACLE:
sql = "select userenv('SESSIONID') as tag from dual"; sql = "select userenv('SESSIONID') as tag from dual";
break; break;
} }
PreparedStatement stmt = conn.prepareStatement(sql); PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery(); ResultSet rs = stmt.executeQuery();
@ -171,11 +168,9 @@ public class PooledConnectionSource implements ConnectionSource {
} }
} }
public synchronized void release(Connection conn) { public synchronized void release(Connection conn) {
if (!m_connections.contains(conn)) { if (!m_connections.contains(conn)) {
throw new IllegalArgumentException throw new IllegalArgumentException("connection did come from ths source: " + conn);
("connection did come from ths source: " + conn);
} }
boolean remove; boolean remove;
@ -211,10 +206,13 @@ public class PooledConnectionSource implements ConnectionSource {
} }
private class Poller extends Thread { private class Poller extends Thread {
public void run() { public void run() {
while (true) { while (true) {
try { Thread.sleep(m_interval); } s_log.error("PollerThread run(): " + m_interval);
catch (InterruptedException e) { try {
Thread.sleep(m_interval);
} catch (InterruptedException e) {
throw new UncheckedWrapperException(e); throw new UncheckedWrapperException(e);
} }
testAvailable(); testAvailable();
@ -222,15 +220,19 @@ public class PooledConnectionSource implements ConnectionSource {
} }
} }
// This Thread is running only if ther Poller Thread sends a notify() to
// the synchronized m_untested variable.
private class Tester extends Thread { private class Tester extends Thread {
public void run() { public void run() {
List untested = new ArrayList(); List untested = new ArrayList();
while (true) { while (true) {
untested.clear(); untested.clear();
synchronized (m_untested) { synchronized (m_untested) {
if (m_untested.isEmpty()) { if (m_untested.isEmpty()) {
try { m_untested.wait(); } try {
catch (InterruptedException e) { m_untested.wait();
} catch (InterruptedException e) {
throw new UncheckedWrapperException(e); throw new UncheckedWrapperException(e);
} }
} }
@ -238,18 +240,17 @@ public class PooledConnectionSource implements ConnectionSource {
m_untested.clear(); m_untested.clear();
} }
for (Iterator it = untested.iterator(); it.hasNext(); ) { for (Iterator it = untested.iterator(); it.hasNext();) {
Connection conn = (Connection) it.next(); Connection conn = (Connection) it.next();
SQLException e = test(conn); SQLException e = test(conn);
if (e != null) { if (e != null) {
s_log.warn("connection " + conn s_log.warn("connection " + conn
+ ", tag: " + s_connectionTags.get(conn.toString()) + ", tag: " + s_connectionTags.get(conn.toString())
+ " failed test", e); + " failed test", e);
try { try {
conn.close(); conn.close();
} catch (SQLException ex) { } catch (SQLException ex) {
s_log.warn s_log.warn("error while closing bad connection", ex);
("error while closing bad connection", ex);
} }
} }
release(conn); release(conn);
@ -257,8 +258,7 @@ public class PooledConnectionSource implements ConnectionSource {
} }
} }
} }
private static final String[] TYPES = new String[]{"TABLE"};
private static final String[] TYPES = new String[] { "TABLE" };
private static SQLException test(Connection conn) { private static SQLException test(Connection conn) {
try { try {
@ -272,5 +272,4 @@ public class PooledConnectionSource implements ConnectionSource {
return e; return e;
} }
} }
} }