You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by James Carman <ja...@carmanconsulting.com> on 2002/02/27 21:06:57 UTC

JDBCRealm ?

Why does the JDBCRealm class actually instantiate the JDBC driver object and use it directly to establish connections?  The DriverManager class should be used to actually establish the connection to the database.  The open method should read something like this...

protected Connection open() throws SQLException 
{
    if (dbConnection == null)
    {
        try 
        {
           Class.forName( driverName );
            dbConnection = DriverManager.getConnection( connectionURL, connectionName, connectionPassword );
        }
        catch( ClassNotFoundException cnf )
        {
            throw new SQLException(cnf.getMessage());
        }
    }
    return dbConnection;
}

Doesn't this make more sense as opposed to...

protected Connection open() throws SQLException {

        // Do nothing if there is a database connection already open
        if (dbConnection != null)
            return (dbConnection);
        
        // Instantiate our database driver if necessary
        if (driver == null) {
            try {
                Class clazz = Class.forName(driverName);
                driver = (Driver) clazz.newInstance();
            } catch (Throwable e) {
                throw new SQLException(e.getMessage());
            }
        }

        // Open a new connection
        Properties props = new Properties();
        if (connectionName != null)
            props.put("user", connectionName);
        if (connectionPassword != null)
            props.put("password", connectionPassword);
        dbConnection = driver.connect(connectionURL, props);
        dbConnection.setAutoCommit(false);
        return (dbConnection);

    }