Now checks for update before returning a connection.
DbSources are now handled by the application instead of the server.
This commit is contained in:
parent
045ba6fb41
commit
a436d221d1
1 changed files with 26 additions and 9 deletions
|
@ -5,7 +5,7 @@ package helma.objectmodel;
|
|||
|
||||
import java.sql.*;
|
||||
import java.util.Hashtable;
|
||||
import helma.objectmodel.db.Transactor;
|
||||
import helma.objectmodel.db.*;
|
||||
|
||||
/**
|
||||
* This class describes a releational data source (URL, driver, user and password).
|
||||
|
@ -14,26 +14,30 @@ import helma.objectmodel.db.Transactor;
|
|||
public class DbSource {
|
||||
|
||||
private String name;
|
||||
private SystemProperties props;
|
||||
private static SystemProperties defaultProps = null;
|
||||
protected String url;
|
||||
private String driver;
|
||||
protected String user;
|
||||
private String password;
|
||||
|
||||
public DbSource (String name) throws ClassNotFoundException {
|
||||
private long lastRead = 0l;
|
||||
|
||||
public DbSource (String name, SystemProperties props) throws ClassNotFoundException {
|
||||
this.name = name;
|
||||
IServer.dbSources.put (name.toLowerCase (), this);
|
||||
url = IServer.dbProps.getProperty (name+".url");
|
||||
driver = IServer.dbProps.getProperty (name+".driver");
|
||||
Class.forName (driver);
|
||||
user = IServer.dbProps.getProperty (name+".user");
|
||||
password = IServer.dbProps.getProperty (name+".password");
|
||||
this.props = props;
|
||||
init ();
|
||||
IServer.getLogger().log ("created db source ["+name+", "+url+", "+driver+", "+user+"]");
|
||||
}
|
||||
|
||||
public Connection getConnection () throws ClassNotFoundException, SQLException {
|
||||
Transactor tx = (Transactor) Thread.currentThread ();
|
||||
Connection con = tx.getConnection (this);
|
||||
if (con == null || con.isClosed ()) {
|
||||
boolean fileUpdated = props.lastModified () > lastRead;
|
||||
if (!fileUpdated && defaultProps != null)
|
||||
fileUpdated = defaultProps.lastModified () > lastRead;
|
||||
if (con == null || con.isClosed () || fileUpdated) {
|
||||
init ();
|
||||
Class.forName (driver);
|
||||
con = DriverManager.getConnection (url, user, password);
|
||||
// If we wanted to use SQL transactions, we'd set autoCommit to
|
||||
|
@ -44,6 +48,15 @@ public class DbSource {
|
|||
return con;
|
||||
}
|
||||
|
||||
private void init () throws ClassNotFoundException {
|
||||
lastRead = defaultProps == null ? props.lastModified () : Math.max (props.lastModified (), defaultProps.lastModified ());
|
||||
url = props.getProperty (name+".url");
|
||||
driver = props.getProperty (name+".driver");
|
||||
Class.forName (driver);
|
||||
user = props.getProperty (name+".user");
|
||||
password = props.getProperty (name+".password");
|
||||
}
|
||||
|
||||
public String getDriverName () {
|
||||
return driver;
|
||||
}
|
||||
|
@ -52,6 +65,10 @@ public class DbSource {
|
|||
return name;
|
||||
}
|
||||
|
||||
public static void setDefaultProps (SystemProperties props) {
|
||||
defaultProps = props;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue