tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Carman" <ja...@carmanconsulting.com>
Subject JDBCRealm ?
Date Wed, 27 Feb 2002 20:06:57 GMT
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);

    }

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message