pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
first connection attempt always fails, subsequent always succeed
6+ messages / 4 participants
[nested] [flat]

* first connection attempt always fails, subsequent always succeed
@ 2020-11-12 18:59 Rob Sargent <robjsargent@gmail.com>
  2020-11-12 19:11 ` Re: first connection attempt always fails, subsequent always succeed David Gradwell <david@gradwell.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Rob Sargent @ 2020-11-12 18:59 UTC (permalink / raw)
  To: pgsql-jdbc@lists.postgresql.org

This is an embedded tomcat application, not full-blown apache server.  
Works great when it does...  I haven't wired in a connection supplier 
yet since (for now) there are few clients and they do two reads and one 
write, all very large.

java:

    java -version
    openjdk version "11.0.9" 2020-10-20
    OpenJDK Runtime Environment (build 11.0.9+11-Ubuntu-0ubuntu1.20.04)
    OpenJDK 64-Bit Server VM (build 11.0.9+11-Ubuntu-0ubuntu1.20.04,
    mixed mode, sharing)

tomcat:

    version 9.0.39

poostgres:

    PostgreSQL 12.4

This is the servlet's connection code:

    String templateUrl =System.getProperty("SGSSVR_databaseConnection","jdbc:postgresql://%s/%s");//host(with :port) / dbname String fullurl =String.format(templateUrl,dbhost,dbname);  //will add port to this next
    conn =DriverManager.getConnection(fullurl,dbrole,magicPassword);
    dsl =DSL.using(conn,SQLDialect.POSTGRES);

which generates this stack trace, showing what looks to me like a good 
url (and which works later).

    java.sql.SQLException: No suitable driver found for
    jdbc:postgresql://localhost/version4007
         at
    java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
         at
    java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
         at
    edu.utah.camplab.servlet.AbstractSGSServlet.setDbConn(AbstractSGSServlet.java:52)
         at
    edu.utah.camplab.servlet.ParfileServlet.doGet(ParfileServlet.java:30)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
         at
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
         at
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
         at
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
         at
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
         at
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
         at
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
         at
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
         at
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
         at
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
         at
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
         at
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
         at
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
         at
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
         at
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
         at
    java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
         at
    java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
         at
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
         at java.base/java.lang.Thread.run(Thread.java:834)

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: first connection attempt always fails, subsequent always succeed
  2020-11-12 18:59 first connection attempt always fails, subsequent always succeed Rob Sargent <robjsargent@gmail.com>
@ 2020-11-12 19:11 ` David Gradwell <david@gradwell.com>
  2020-11-12 19:31   ` Re: first connection attempt always fails, subsequent always succeed Rob Sargent <robjsargent@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: David Gradwell @ 2020-11-12 19:11 UTC (permalink / raw)
  To: Rob Sargent <robjsargent@gmail.com>; pgsql-jdbc@lists.postgresql.org <pgsql-jdbc@lists.postgresql.org>

Rob,

If this is obvious apologies, but have you explicitly included an appropriate JDBC driver in your .war build ?  https://jdbc.postgresql.org/download.html   I am currently fighting issues with MacOS apparently not allowing connections to be made (timeout or host not found) and have reverted to a 9.3 build for testing.

Are you using NetBeans  as your IDE ?

David Gradwell

From: Rob Sargent <robjsargent@gmail.com>
Date: Thursday, 12 November 2020 at 18:59
To: "pgsql-jdbc@lists.postgresql.org" <pgsql-jdbc@lists.postgresql.org>
Subject: first connection attempt always fails, subsequent always succeed


This is an embedded tomcat application, not full-blown apache server.  Works great when it does...  I haven't wired in a connection supplier yet since (for now) there are few clients and they do two reads and one write, all very large.

java:

java -version
openjdk version "11.0.9" 2020-10-20
OpenJDK Runtime Environment (build 11.0.9+11-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.9+11-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

tomcat:

version 9.0.39

poostgres:

PostgreSQL 12.4

This is the servlet's connection code:

String templateUrl = System.getProperty("SGSSVR_databaseConnection", "jdbc:postgresql://%s/%s"); //host(with :port) / dbname

String fullurl = String.format(templateUrl, dbhost, dbname);  //will add port to this next

conn = DriverManager.getConnection(fullurl, dbrole, magicPassword);

dsl = DSL.using(conn, SQLDialect.POSTGRES);

which generates this stack trace, showing what looks to me like a good url (and which works later).

java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost/version4007
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
    at edu.utah.camplab.servlet.AbstractSGSServlet.setDbConn(AbstractSGSServlet.java:52)
    at edu.utah.camplab.servlet.ParfileServlet.doGet(ParfileServlet.java:30)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:834)





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: first connection attempt always fails, subsequent always succeed
  2020-11-12 18:59 first connection attempt always fails, subsequent always succeed Rob Sargent <robjsargent@gmail.com>
  2020-11-12 19:11 ` Re: first connection attempt always fails, subsequent always succeed David Gradwell <david@gradwell.com>
@ 2020-11-12 19:31   ` Rob Sargent <robjsargent@gmail.com>
  2020-11-12 20:05     ` Re: first connection attempt always fails, subsequent always succeed Alan Stange <stange@rentec.com>
  2020-11-12 20:42     ` Re: first connection attempt always fails, subsequent always succeed Mark Rotteveel <mark@lawinegevaar.nl>
  0 siblings, 2 replies; 6+ messages in thread

From: Rob Sargent @ 2020-11-12 19:31 UTC (permalink / raw)
  To: David Gradwell <david@gradwell.com>; pgsql-jdbc@lists.postgresql.org <pgsql-jdbc@lists.postgresql.org>


On 11/12/20 12:11 PM, David Gradwell wrote:
>
> Rob,
>
> If this is obvious apologies, but have you explicitly included an 
> appropriate JDBC driver in your .war build ? 
> https://jdbc.postgresql.org/download.html I am currently fighting 
> issues with MacOS apparently not allowing connections to be made 
> (timeout or host not found) and have reverted to a 9.3 build for testing.
>
> Are you using NetBeans  as your IDE ?
>
> David Gradwell
>
> *From: *Rob Sargent <robjsargent@gmail.com>
> *Date: *Thursday, 12 November 2020 at 18:59
> *To: *"pgsql-jdbc@lists.postgresql.org" <pgsql-jdbc@lists.postgresql.org>
> *Subject: *first connection attempt always fails, subsequent always 
> succeed
>
> This is an embedded tomcat application, not full-blown apache server.  
> Works great when it does...  I haven't wired in a connection supplier 
> yet since (for now) there are few clients and they do two reads and 
> one write, all very large.
>
> java:
>
>     java -version
>     openjdk version "11.0.9" 2020-10-20
>     OpenJDK Runtime Environment (build 11.0.9+11-Ubuntu-0ubuntu1.20.04)
>     OpenJDK 64-Bit Server VM (build 11.0.9+11-Ubuntu-0ubuntu1.20.04,
>     mixed mode, sharing)
>
> tomcat:
>
>     version 9.0.39
>
> poostgres:
>
>     PostgreSQL 12.4
>
> This is the servlet's connection code:
>
>     String templateUrl =
>     System./getProperty/("SGSSVR_databaseConnection",
>     "jdbc:postgresql://%s/%s"); ///host(with :port) / dbname/
>
>     String fullurl = String./format/(templateUrl, dbhost, dbname); 
>     //will add port to this next
>
>     conn = DriverManager./getConnection/(fullurl, dbrole, magicPassword);
>
>     dsl = DSL./using/(conn, SQLDialect./POSTGRES/);
>
> which generates this stack trace, showing what looks to me like a good 
> url (and which works later).
>
>     java.sql.SQLException: No suitable driver found for
>     jdbc:postgresql://localhost/version4007
>         at
>     java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
>
thinking these forums like bottom posting

 From my war file (which I explode)

    WEB-INF/lib/postgresql-42.2.18.jar

I get the same behaviour running from within IntelliJ and from 
gradle/command-line, on ubuntu 20.04

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: first connection attempt always fails, subsequent always succeed
  2020-11-12 18:59 first connection attempt always fails, subsequent always succeed Rob Sargent <robjsargent@gmail.com>
  2020-11-12 19:11 ` Re: first connection attempt always fails, subsequent always succeed David Gradwell <david@gradwell.com>
  2020-11-12 19:31   ` Re: first connection attempt always fails, subsequent always succeed Rob Sargent <robjsargent@gmail.com>
@ 2020-11-12 20:05     ` Alan Stange <stange@rentec.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Alan Stange @ 2020-11-12 20:05 UTC (permalink / raw)
  To: Rob Sargent <robjsargent@gmail.com>; David Gradwell <david@gradwell.com>; pgsql-jdbc@lists.postgresql.org <pgsql-jdbc@lists.postgresql.org>

On 11/12/20 2:31 PM, Rob Sargent wrote:
>
>
> On 11/12/20 12:11 PM, David Gradwell wrote:
>>
>> Rob,
>>
>>  
>>
>> If this is obvious apologies, but have you explicitly included an
>> appropriate JDBC driver in your .war build ? 
>> https://jdbc.postgresql.org/download.html
>> <https://jdbc.postgresql.org/download.html>   I am currently fighting
>> issues with MacOS apparently not allowing connections to be made
>> (timeout or host not found) and have reverted to a 9.3 build for testing.
>>
>>  
>>
>> Are you using NetBeans  as your IDE ?
>>
>>  
>>
>> David Gradwell
>>
>>  
>>
>> *From: *Rob Sargent <robjsargent@gmail.com>
>> *Date: *Thursday, 12 November 2020 at 18:59
>> *To: *"pgsql-jdbc@lists.postgresql.org" <pgsql-jdbc@lists.postgresql.org>
>> *Subject: *first connection attempt always fails, subsequent always
>> succeed
>>
>>  
>>
>> This is an embedded tomcat application, not full-blown apache
>> server.  Works great when it does...  I haven't wired in a connection
>> supplier yet since (for now) there are few clients and they do two
>> reads and one write, all very large.
>>
>> java:
>>
>>     java -version
>>     openjdk version "11.0.9" 2020-10-20
>>     OpenJDK Runtime Environment (build 11.0.9+11-Ubuntu-0ubuntu1.20.04)
>>     OpenJDK 64-Bit Server VM (build 11.0.9+11-Ubuntu-0ubuntu1.20.04,
>>     mixed mode, sharing)
>>
>> tomcat: 
>>
>>     version 9.0.39
>>
>> poostgres:
>>
>>     PostgreSQL 12.4
>>
>> This is the servlet's connection code:
>>
>>     String templateUrl =
>>     System./getProperty/("SGSSVR_databaseConnection",
>>     "jdbc:postgresql://%s/%s"); ///host(with :port) / dbname/
>>
>>     String fullurl = String./format/(templateUrl, dbhost, dbname); 
>>     //will add port to this next
>>
>>     conn = DriverManager./getConnection/(fullurl, dbrole, magicPassword);
>>
>>     dsl = DSL./using/(conn, SQLDialect./POSTGRES/);
>>
>> which generates this stack trace, showing what looks to me like a
>> good url (and which works later).
>>
>>     java.sql.SQLException: No suitable driver found for
>>     jdbc:postgresql://localhost/version4007
>>         at
>>     java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
>>
> thinking these forums like bottom posting
>
> From my war file (which I explode)
>
>     WEB-INF/lib/postgresql-42.2.18.jar
>
> I get the same behaviour running from within IntelliJ and from
> gradle/command-line, on ubuntu 20.0
>

Do you have a call

Class.forName("org.postgresql.Driver");

in the code to load the postgresql driver into the application?

Alan

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: first connection attempt always fails, subsequent always succeed
  2020-11-12 18:59 first connection attempt always fails, subsequent always succeed Rob Sargent <robjsargent@gmail.com>
  2020-11-12 19:11 ` Re: first connection attempt always fails, subsequent always succeed David Gradwell <david@gradwell.com>
  2020-11-12 19:31   ` Re: first connection attempt always fails, subsequent always succeed Rob Sargent <robjsargent@gmail.com>
@ 2020-11-12 20:42     ` Mark Rotteveel <mark@lawinegevaar.nl>
  2020-11-12 21:41       ` Re: first connection attempt always fails, subsequent always succeed Rob Sargent <robjsargent@gmail.com>
  1 sibling, 1 reply; 6+ messages in thread

From: Mark Rotteveel @ 2020-11-12 20:42 UTC (permalink / raw)
  To: pgsql-jdbc@lists.postgresql.org

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





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: first connection attempt always fails, subsequent always succeed
  2020-11-12 18:59 first connection attempt always fails, subsequent always succeed Rob Sargent <robjsargent@gmail.com>
  2020-11-12 19:11 ` Re: first connection attempt always fails, subsequent always succeed David Gradwell <david@gradwell.com>
  2020-11-12 19:31   ` Re: first connection attempt always fails, subsequent always succeed Rob Sargent <robjsargent@gmail.com>
  2020-11-12 20:42     ` Re: first connection attempt always fails, subsequent always succeed Mark Rotteveel <mark@lawinegevaar.nl>
@ 2020-11-12 21:41       ` Rob Sargent <robjsargent@gmail.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Rob Sargent @ 2020-11-12 21:41 UTC (permalink / raw)
  To: Mark Rotteveel <mark@lawinegevaar.nl>; +Cc: pgsql-jdbc@lists.postgresql.org

Bingo.  I have that in one servlet, the writer, whose code pre-dates the readers.  I’ll push that down one class.
Thanks so much.


> On Nov 12, 2020, at 1:42 PM, Mark Rotteveel <mark@lawinegevaar.nl> wrote:
> 
> 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
> 
> 






^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2020-11-12 21:41 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-11-12 18:59 first connection attempt always fails, subsequent always succeed Rob Sargent <robjsargent@gmail.com>
2020-11-12 19:11 ` David Gradwell <david@gradwell.com>
2020-11-12 19:31   ` Rob Sargent <robjsargent@gmail.com>
2020-11-12 20:05     ` Alan Stange <stange@rentec.com>
2020-11-12 20:42     ` Mark Rotteveel <mark@lawinegevaar.nl>
2020-11-12 21:41       ` Rob Sargent <robjsargent@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox