Re: keeping Connection alive

From: Andreas <ml(at)3(dot)141592654(dot)de>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: keeping Connection alive
Date: 2009-12-13 01:37:44
Message-ID: 21846658.35.1260668264808.JavaMail.root@store1.zcs.ext.wpsrv.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hi Jovan,

thanks for your reply!

> On Dec 12, 2009, at 6:43 PM, Andreas Brandl wrote:
> > I'm using a PostgreSQL as backend for a Java application, which may
> idle for quite a while (i.e. hours) and now and then issue a query on
> the backend.
>
> Is this a standard Java application or is it running in a Java EE or
> servlet container?

This is a standard Java application.

> If you're not using an application server, and/or don't need pooling,
> perhaps it would be better to use the regular jdbc api something like
> this:
> Class.forName("org.postgresql.Driver");
> Connection conn = DriverManager.getConnection(jdbcConnUrl, username,
> password);
> // then conn.createStatement , etc.
>
> Then you can call conn.close() and create a new connection when
> necessary.

I did work with a pure Connection before, until I realized things go wrong when application is idle for long time.

In either case, the problem is how to detect whether a Connection is valid or not. As far as I know there is no native mechanism for checking the state. So one approach may be to implement a DataSource which does check (i.e. "SELECT 1") validity before returning the Connection (and reconnect if its not valid, i.e. throwing a SQLException?).

Is there any better way to achieve this? After all this might mean a lot of overhead (checking every time DataSource.getConnection() is called)...

Thanks,
Andreas

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Craig Ringer 2009-12-13 02:52:03 Re: keeping Connection alive
Previous Message F. Jovan Jester 2009-12-13 00:42:36 Re: keeping Connection alive