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:
parent
8d9bc3afb1
commit
5f18e3ae2d
2 changed files with 15 additions and 11 deletions
|
@ -32,7 +32,7 @@ import java.util.Hashtable;
|
||||||
public class DbSource {
|
public class DbSource {
|
||||||
private static ResourceProperties defaultProps = null;
|
private static ResourceProperties defaultProps = null;
|
||||||
private Properties conProps;
|
private Properties conProps;
|
||||||
private String name;
|
private final String name;
|
||||||
private ResourceProperties props, subProps;
|
private ResourceProperties props, subProps;
|
||||||
protected String url;
|
protected String url;
|
||||||
private String driver;
|
private String driver;
|
||||||
|
|
|
@ -52,10 +52,10 @@ public class Transactor {
|
||||||
protected ITransaction txn;
|
protected ITransaction txn;
|
||||||
|
|
||||||
// Transactions for SQL data sources
|
// Transactions for SQL data sources
|
||||||
private Map sqlConnections;
|
private Map<String, Connection> sqlConnections;
|
||||||
|
|
||||||
// Set of SQL connections that already have been verified
|
// Set of SQL connections that already have been verified
|
||||||
private Map testedConnections;
|
private Map<String, Long> testedConnections;
|
||||||
|
|
||||||
// when did the current transaction start?
|
// when did the current transaction start?
|
||||||
private long tstart;
|
private long tstart;
|
||||||
|
@ -81,8 +81,8 @@ public class Transactor {
|
||||||
cleanNodes = new HashMap();
|
cleanNodes = new HashMap();
|
||||||
parentNodes = new HashSet();
|
parentNodes = new HashSet();
|
||||||
|
|
||||||
sqlConnections = new HashMap();
|
sqlConnections = new HashMap<String, Connection>();
|
||||||
testedConnections = new HashMap();
|
testedConnections = new HashMap<String, Long>();
|
||||||
active = false;
|
active = false;
|
||||||
killed = false;
|
killed = false;
|
||||||
}
|
}
|
||||||
|
@ -238,9 +238,9 @@ public class Transactor {
|
||||||
* @param con the connection
|
* @param con the connection
|
||||||
*/
|
*/
|
||||||
public void registerConnection(DbSource src, Connection con) {
|
public void registerConnection(DbSource src, Connection con) {
|
||||||
sqlConnections.put(src, con);
|
sqlConnections.put(src.getName(), con);
|
||||||
// we assume a freshly created connection is ok.
|
// 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
|
* @return the connection
|
||||||
*/
|
*/
|
||||||
public Connection getConnection(DbSource src) {
|
public Connection getConnection(DbSource src) {
|
||||||
Connection con = (Connection) sqlConnections.get(src);
|
Connection con = sqlConnections.get(src.getName());
|
||||||
Long tested = (Long) testedConnections.get(src);
|
Long tested = testedConnections.get(src.getName());
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
if (con != null && (tested == null || now - tested.longValue() > 60000)) {
|
if (con != null && (tested == null || now - tested.longValue() > 60000)) {
|
||||||
// Check if the connection is still alive by executing a simple statement.
|
// Check if the connection is still alive by executing a simple statement.
|
||||||
try {
|
try {
|
||||||
Statement stmt = con.createStatement();
|
Statement stmt = con.createStatement();
|
||||||
stmt.execute("SELECT 1");
|
if (src.isOracle()) {
|
||||||
|
stmt.execute("SELECT 1 FROM DUAL");
|
||||||
|
} else {
|
||||||
|
stmt.execute("SELECT 1");
|
||||||
|
}
|
||||||
stmt.close();
|
stmt.close();
|
||||||
testedConnections.put(src, new Long(now));
|
testedConnections.put(src.getName(), new Long(now));
|
||||||
} catch (SQLException sx) {
|
} catch (SQLException sx) {
|
||||||
try {
|
try {
|
||||||
con.close();
|
con.close();
|
||||||
|
|
Loading…
Add table
Reference in a new issue