Modified Transactor to store sqlConnections internally using the name of the DbSource

as Map key, not the DbSource instance. Using the instance as key is both inefficient and
error prone (see fd0b77bc11).

Additional changes:
- modified getConnection() to check if the DB is oracle. "SELECT 1" is invalid
for Oracle DBs and lead to Helma dropping in-use connections every minute.
- set DbSource name final
This commit is contained in:
Robert Gaggl 2013-04-09 12:52:06 +02:00
parent 8d9bc3afb1
commit 5f18e3ae2d
2 changed files with 15 additions and 11 deletions

View file

@ -32,7 +32,7 @@ import java.util.Hashtable;
public class DbSource {
private static ResourceProperties defaultProps = null;
private Properties conProps;
private String name;
private final String name;
private ResourceProperties props, subProps;
protected String url;
private String driver;

View file

@ -52,10 +52,10 @@ public class Transactor {
protected ITransaction txn;
// Transactions for SQL data sources
private Map sqlConnections;
private Map<String, Connection> sqlConnections;
// Set of SQL connections that already have been verified
private Map testedConnections;
private Map<String, Long> testedConnections;
// when did the current transaction start?
private long tstart;
@ -81,8 +81,8 @@ public class Transactor {
cleanNodes = new HashMap();
parentNodes = new HashSet();
sqlConnections = new HashMap();
testedConnections = new HashMap();
sqlConnections = new HashMap<String, Connection>();
testedConnections = new HashMap<String, Long>();
active = false;
killed = false;
}
@ -238,9 +238,9 @@ public class Transactor {
* @param con the connection
*/
public void registerConnection(DbSource src, Connection con) {
sqlConnections.put(src, con);
sqlConnections.put(src.getName(), con);
// we assume a freshly created connection is ok.
testedConnections.put(src, new Long(System.currentTimeMillis()));
testedConnections.put(src.getName(), new Long(System.currentTimeMillis()));
}
/**
@ -249,16 +249,20 @@ public class Transactor {
* @return the connection
*/
public Connection getConnection(DbSource src) {
Connection con = (Connection) sqlConnections.get(src);
Long tested = (Long) testedConnections.get(src);
Connection con = sqlConnections.get(src.getName());
Long tested = testedConnections.get(src.getName());
long now = System.currentTimeMillis();
if (con != null && (tested == null || now - tested.longValue() > 60000)) {
// Check if the connection is still alive by executing a simple statement.
try {
Statement stmt = con.createStatement();
stmt.execute("SELECT 1");
if (src.isOracle()) {
stmt.execute("SELECT 1 FROM DUAL");
} else {
stmt.execute("SELECT 1");
}
stmt.close();
testedConnections.put(src, new Long(now));
testedConnections.put(src.getName(), new Long(now));
} catch (SQLException sx) {
try {
con.close();