pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dave Cramer <pg@fastcrypt.com>
To: Peter <peter@greatnowhere.com>
Cc: Kris Jurka <books@ejurka.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Totally weird behaviour in org.postgresql.Driver
Date: Thu, 12 Mar 2009 06:42:46 -0400
Message-ID: <491f66a50903120342j67e87c7dob10cfdc23580c8f4@mail.gmail.com> (raw)
In-Reply-To: <002201c9a2f9$edd47510$c97d5f30$@com>
References: <49b7bfef$0$1343$834e42db@reader.greatnowhere.com>
	<49B7CBB6.5020800@opencloud.com>
	<49b7ced9$0$1343$834e42db@reader.greatnowhere.com>
	<Pine.BSO.4.64.0903112335400.25261@leary.csoft.net>
	<002201c9a2f9$edd47510$c97d5f30$@com>

Peter,

Couple things.... do you really need to synchronize twice in every method
???
Have you looked at dbcp or c3po instead of re-inventing the wheel ?

Can you supply us with a test case which does exhibit the bug ?

On Thu, Mar 12, 2009 at 6:04 AM, Peter <peter@greatnowhere.com> wrote:

> >> I added a bunch more synchronized (mutex) blocks in my code to avoid
> >> getConnection being called simultaneously from multiple threads (why
> >> isnt that method synchronized anyway?) - so far so good... keeping
> >> fingers crossed. Maybe should have just subclassed PGSimpleDataSource...
>
> >I'm not sure why it needs to be synchronized.  The attached test case (20
> threads each opening and closing >connections as fast as possible) doesn't
> show any problems for me.  Does it fail for you?  Is there a >particular
> change that fixes it for you?
>
> I'm not sure if it needs to be synchronized either. Most likely I'm running
> into a JVM bug in my particular environment (Tomcat servlet). I adapted
> your
> test case to run inside a servlet and it does not produce any errors.
>
> In my app I'm using singleton class instance to handle all database
> connections, sample code below. If I remove synchronized (dbManager.class)
> blocks the bug is back with a vengeance...
>
> final public class dbManager {
>
>
>    private static PGSimpleDataSource _ds;
>    // Our global instance
>    private static dbManager _dbManager = null;
>
>        /**
>
>         * Attempts to create a valid PG DataSource
>         *
>         * @return a boolean representing whether the DS was successfully
> created
>         */
>        private synchronized static boolean createDataSource() {
>        try {
>                synchronized (dbManager.class) {
>
>                _ds = new PGSimpleDataSource();
>                        _ds.setServerName("myserver");
>                        _ds.setDatabaseName("mydb");
>                        _ds.setPortNumber(5432);
>
>                }
>
>                return true;
>        } catch (Exception ex) {
>            Logger.getLogger(dbManager.class.getName()).log(Level.SEVERE,
> null, ex);
>            return false;
>        }
>        }
>
>    public synchronized static dbManager getInstance(String connString) {
>        if ( _dbManager == null ) {
>                if ( createDataSource() ) {
>                    _dbManager = new dbManager();
>                }
>        }
>
>        return _dbManager;
>    }
>
>    /**
>     *
>     * @return returns a connection created w/ default initial credentials
> and very little access rights
>     */
>    public synchronized Connection getInitialConnection() throws
> SQLException {
>        synchronized (dbManager.class) {
>                return _ds.getConnection("inituser", "initpass");
>        }
>    }
>
>    /**
>     *
>     * @param dbUser
>     * @param dbPass
>     * @return a connection if it can be created, null otherwise
>     */
>    public synchronized Connection getNewUserConnection(String dbUser,
> String dbPass){
>        try {
>
>                synchronized (dbManager.class) {
>
>                        Connection userConn = _ds.getConnection(dbUser,
> dbPass);
>                        return userConn;
>
>                }
>
>            } catch (Exception ex) {
>
> Logger.getLogger(dbManager.class.getName()).log(Level.SEVERE, null, ex);
>                return null;
>                 }
>    }
> }
>
>
> --
> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-jdbc
>

view thread (22+ messages)  latest in thread

Message-ID: <491f66a50903120342j67e87c7dob10cfdc23580c8f4@mail.gmail.com>
Permalink:  ../491f66a50903120342j67e87c7dob10cfdc23580c8f4@mail.gmail.com/
Also on:    postgresql.org/message-id/491f66a50903120342j67e87c7dob10cfdc23580c8f4@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: pg@fastcrypt.com, peter@greatnowhere.com, books@ejurka.com
  Subject: Re: Totally weird behaviour in org.postgresql.Driver
  In-Reply-To: <491f66a50903120342j67e87c7dob10cfdc23580c8f4@mail.gmail.com>
  References: <49b7bfef$0$1343$834e42db@reader.greatnowhere.com> <49B7CBB6.5020800@opencloud.com> <49b7ced9$0$1343$834e42db@reader.greatnowhere.com> <Pine.BSO.4.64.0903112335400.25261@leary.csoft.net> <002201c9a2f9$edd47510$c97d5f30$@com> <491f66a50903120342j67e87c7dob10cfdc23580c8f4@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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