* Implement switchProperties() to allow to switch db connections at runtime.

* Minor cleanup/simplification in getConnection().
This commit is contained in:
hns 2007-03-08 15:40:09 +00:00
parent eb692b71ec
commit 156e65909e

View file

@ -72,16 +72,12 @@ public class DbSource {
con = tx.getConnection(this); con = tx.getConnection(this);
} }
boolean fileUpdated = props.lastModified() > lastRead; boolean fileUpdated = props.lastModified() > lastRead ||
if (!fileUpdated && (defaultProps != null)) { (defaultProps != null && defaultProps.lastModified() > lastRead);
fileUpdated = defaultProps.lastModified() > lastRead;
}
if (con == null || con.isClosed() || fileUpdated) { if (con == null || con.isClosed() || fileUpdated) {
init(); init();
Class.forName(driver);
con = DriverManager.getConnection(url, conProps); con = DriverManager.getConnection(url, conProps);
// con = DriverManager.getConnection(url, user, password);
// If we wanted to use SQL transactions, we'd set autoCommit to // If we wanted to use SQL transactions, we'd set autoCommit to
// false here and make commit/rollback invocations in Transactor methods; // false here and make commit/rollback invocations in Transactor methods;
@ -94,6 +90,20 @@ public class DbSource {
return con; return con;
} }
/**
* Set the db properties to newProps, and return the old properties.
* @param newProps the new properties to use for this db source
* @return the old properties
* @throws ClassNotFoundException if jdbc driver class couldn't be found
*/
public synchronized ResourceProperties switchProperties(ResourceProperties newProps)
throws ClassNotFoundException {
ResourceProperties oldProps = props;
props = newProps;
init();
return oldProps;
}
/** /**
* Initialize the db source from the properties * Initialize the db source from the properties
* *