* Add test if SQL connections are alive before returning them in getConnection().

* Add some javadoc comments and rename sqlCon to sqlConnections.
This commit is contained in:
hns 2006-05-11 18:36:56 +00:00
parent 7298b70d3b
commit 16046c5e71

View file

@ -20,6 +20,8 @@ import helma.objectmodel.DatabaseException;
import helma.objectmodel.ITransaction; import helma.objectmodel.ITransaction;
import java.sql.Connection; import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.*; import java.util.*;
/** /**
@ -48,7 +50,10 @@ public class Transactor extends Thread {
protected ITransaction txn; protected ITransaction txn;
// Transactions for SQL data sources // Transactions for SQL data sources
protected HashMap sqlCon; private HashMap sqlConnections;
// Set of SQL connections that already have been verified
private HashSet testedConnections;
// when did the current transaction start? // when did the current transaction start?
private long tstart; private long tstart;
@ -71,13 +76,14 @@ public class Transactor extends Thread {
cleanNodes = new HashMap(); cleanNodes = new HashMap();
parentNodes = new HashSet(); parentNodes = new HashSet();
sqlCon = new HashMap(); sqlConnections = new HashMap();
testedConnections = new HashSet();
active = false; active = false;
killed = false; killed = false;
} }
/** /**
* * Mark a Node as modified/created/deleted during this transaction
* *
* @param node ... * @param node ...
*/ */
@ -92,7 +98,7 @@ public class Transactor extends Thread {
} }
/** /**
* * Unmark a Node that has previously been marked as modified during the transaction
* *
* @param node ... * @param node ...
*/ */
@ -105,7 +111,7 @@ public class Transactor extends Thread {
} }
/** /**
* * Keep a reference to an unmodified Node local to this transaction
* *
* @param node ... * @param node ...
*/ */
@ -120,7 +126,7 @@ public class Transactor extends Thread {
} }
/** /**
* * Keep a reference to an unmodified Node local to this transaction
* *
* @param key ... * @param key ...
* @param node ... * @param node ...
@ -134,7 +140,7 @@ public class Transactor extends Thread {
} }
/** /**
* * Get a reference to an unmodified Node local to this transaction
* *
* @param key ... * @param key ...
* *
@ -170,7 +176,9 @@ public class Transactor extends Thread {
* @param con ... * @param con ...
*/ */
public void registerConnection(DbSource src, Connection con) { public void registerConnection(DbSource src, Connection con) {
sqlCon.put(src, con); sqlConnections.put(src, con);
// we assume a freshly created connection is ok.
testedConnections.add(src);
} }
/** /**
@ -181,7 +189,22 @@ public class Transactor extends Thread {
* @return ... * @return ...
*/ */
public Connection getConnection(DbSource src) { public Connection getConnection(DbSource src) {
return (Connection) sqlCon.get(src); Connection con = (Connection) sqlConnections.get(src);
if (con != null && !testedConnections.contains(src)) {
// Check if the connection is still alive by executing a simple statement.
try {
Statement stmt = con.createStatement();
stmt.execute("SELECT 1");
stmt.close();
testedConnections.add(src);
} catch (SQLException sx) {
try {
con.close();
} catch (SQLException ignore) {/* nothing to do */}
return null;
}
}
return con;
} }
/** /**
@ -208,6 +231,7 @@ public class Transactor extends Thread {
active = true; active = true;
tstart = System.currentTimeMillis(); tstart = System.currentTimeMillis();
tname = name; tname = name;
testedConnections.clear();
} }
/** /**
@ -238,7 +262,7 @@ public class Transactor extends Thread {
deletedNodes = new ArrayList(); deletedNodes = new ArrayList();
modifiedParentNodes = new ArrayList(); modifiedParentNodes = new ArrayList();
} }
if (!dirtyNodes.isEmpty()) { if (!dirtyNodes.isEmpty()) {
Object[] dirty = dirtyNodes.values().toArray(); Object[] dirty = dirtyNodes.values().toArray();
@ -309,9 +333,9 @@ public class Transactor extends Thread {
} }
} }
} }
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
if (!parentNodes.isEmpty()) { if (!parentNodes.isEmpty()) {
// set last subnode change times in parent nodes // set last subnode change times in parent nodes
for (Iterator i = parentNodes.iterator(); i.hasNext(); ) { for (Iterator i = parentNodes.iterator(); i.hasNext(); ) {
@ -322,17 +346,18 @@ public class Transactor extends Thread {
} }
} }
} }
if (hasListeners) { if (hasListeners) {
nmgr.fireNodeChangeEvent(insertedNodes, updatedNodes, nmgr.fireNodeChangeEvent(insertedNodes, updatedNodes,
deletedNodes, modifiedParentNodes); deletedNodes, modifiedParentNodes);
} }
// clear the node collections // clear the node collections
dirtyNodes.clear(); dirtyNodes.clear();
cleanNodes.clear(); cleanNodes.clear();
parentNodes.clear(); parentNodes.clear();
testedConnections.clear();
if (active) { if (active) {
active = false; active = false;
nmgr.db.commitTransaction(txn); nmgr.db.commitTransaction(txn);
@ -376,6 +401,7 @@ public class Transactor extends Thread {
dirtyNodes.clear(); dirtyNodes.clear();
cleanNodes.clear(); cleanNodes.clear();
parentNodes.clear(); parentNodes.clear();
testedConnections.clear();
// close any JDBC connections associated with this transactor thread // close any JDBC connections associated with this transactor thread
closeConnections(); closeConnections();
@ -428,8 +454,8 @@ public class Transactor extends Thread {
*/ */
public void closeConnections() { public void closeConnections() {
// nmgr.app.logEvent("Cleaning up Transactor thread"); // nmgr.app.logEvent("Cleaning up Transactor thread");
if (sqlCon != null) { if (sqlConnections != null) {
for (Iterator i = sqlCon.values().iterator(); i.hasNext();) { for (Iterator i = sqlConnections.values().iterator(); i.hasNext();) {
try { try {
Connection con = (Connection) i.next(); Connection con = (Connection) i.next();
@ -440,7 +466,7 @@ public class Transactor extends Thread {
} }
} }
sqlCon.clear(); sqlConnections.clear();
} }
} }