Re: first connection attempt always fails, subsequent always succeed - Mailing list pgsql-jdbc

From Mark Rotteveel
Subject Re: first connection attempt always fails, subsequent always succeed
Date
Msg-id 20e3d445-8a1b-15c1-39de-d2ff003929d0@lawinegevaar.nl
Whole thread Raw
In response to Re: first connection attempt always fails, subsequent always succeed  (Rob Sargent <robjsargent@gmail.com>)
Responses Re: first connection attempt always fails, subsequent always succeed  (Rob Sargent <robjsargent@gmail.com>)
List pgsql-jdbc
On 12-11-2020 20:31, Rob Sargent wrote:
> thinking these forums like bottom posting
> 
>  From my war file (which I explode)
> 
>     WEB-INF/lib/postgresql-42.2.18.jar

If the JDBC driver is included in the WAR, then you need to explicitly 
load the driver with `Class.forName("org.postgresql.Driver")`, because 
automatic driver loading doesn't work when the driver is on an web 
application classpath; it will only work if the driver is on the initial 
classpath (ie in Tomcats lib folder).

The second attempt likely works because you or your code did something 
that triggered the driver to load (ie directly or indirectly loading the 
org.postgresql.Driver class).

That said, using `DriverManager.getConnection` in a web application is 
generally not a good idea. For performance it is usually better to use a 
connection pool (eg the one built-in in Tomcat, or a third-party lib 
like HikariCP).

Mark
-- 
Mark Rotteveel



pgsql-jdbc by date:

Previous
From: Alan Stange
Date:
Subject: Re: first connection attempt always fails, subsequent always succeed
Next
From: Rob Sargent
Date:
Subject: Re: first connection attempt always fails, subsequent always succeed