Index: JDBCStore.java =================================================================== --- JDBCStore.java (revision 766720) +++ JDBCStore.java (working copy) @@ -38,6 +38,8 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.Properties; +import javax.sql.DataSource; +import javax.naming.*; /** * Implementation of the Store interface that stores @@ -48,7 +50,7 @@ * @version $Revision$, $Date$ */ -public class JDBCStore +public class JDBCStore2 extends StoreBase implements Store { /** @@ -72,6 +74,11 @@ protected String threadName = "JDBCStore"; /** + * The connection datasource + */ + protected String datasourceName = null; + + /** * The connection username to use when trying to connect to the database. */ protected String connectionName = null; @@ -217,6 +224,23 @@ } /** + * Return the datasource name to use to connect to the database. + * + */ + public String getDatasourceName() { + return datasourceName; + } + + /** + * Set the datasource name to use to connect to the database. + * + * @param datasourceName datasource name + */ + public void setDatasourceName(String datasourceName) { + this.datasourceName = datasourceName; + } + + /** * Set the driver for this Store. * * @param driverName The new driver @@ -745,6 +769,11 @@ InputStream in = null; synchronized (this) { + // If sessions already exist in DB, remove and insert again. + // TODO: + // * Check if ID exists in database and if so use UPDATE. + remove(session.getIdInternal()); + int numberOfTries = 2; while (numberOfTries > 0) { Connection _conn = getConnection(); @@ -752,11 +781,6 @@ return; } - // If sessions already exist in DB, remove and insert again. - // TODO: - // * Check if ID exists in database and if so use UPDATE. - remove(session.getIdInternal()); - try { bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(new BufferedOutputStream(bos)); @@ -850,8 +874,47 @@ * @exception SQLException if a database error occurs */ protected Connection open() throws SQLException { + if (datasourceName == null) { + dbConnection = openDirectConnection(); + } else { + dbConnection = openJNDIConnection(); + } + return dbConnection; + } + /** + * Open (if necessary) and return a database connection for use by + * this Realm. + * + * @exception SQLException if a database error occurs + */ + protected Connection openJNDIConnection() throws SQLException { + // Do nothing if there is a database connection already open + if (dbConnection != null && !dbConnection.isClosed()) + return (dbConnection); + + try { + Context initialContext = new InitialContext(); + DataSource ds = (DataSource)initialContext.lookup(datasourceName); + return ds.getConnection(); + } catch (NamingException ex) { + throw new SQLException("Unable to get datasource " + + datasourceName + " : " + ex.getMessage()); + } + + } + + + /** + * Open (if necessary) and return a database connection for use by + * this Realm. + * + * @exception SQLException if a database error occurs + */ + protected Connection openDirectConnection() throws SQLException { + + // Do nothing if there is a database connection already open if (dbConnection != null) return (dbConnection); @@ -955,7 +1018,9 @@ * @param conn The connection to be released */ protected void release(Connection conn) { - ; + if (datasourceName != null) { + close(conn); + } } /**