Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 66BD8633BB5 for ; Thu, 12 Mar 2009 07:42:51 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 83309-10 for ; Thu, 12 Mar 2009 07:42:44 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from yw-out-2324.google.com (yw-out-2324.google.com [74.125.46.30]) by mail.postgresql.org (Postfix) with ESMTP id B8D1D63316F for ; Thu, 12 Mar 2009 07:42:47 -0300 (ADT) Received: by yw-out-2324.google.com with SMTP id 5so617146ywh.73 for ; Thu, 12 Mar 2009 03:42:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to:cc :content-type; bh=XiRzUQH5GdL6+2cwehmcLexpD2XNzL9St7fG+T1ixMU=; b=grUU/EYWpQgKVL/djarjUXGBPfRDrS1xZKu3uqe6x8ev89Y/7Whmw12Z1Bb2qVgcUJ +4iyw8RhwZxXBJngk4Gulx2nA45+wULA9ygVW2TNN8NhpH48d7RPHdd80RwOXJPzVLbJ qrHw198WTT3FcwuDa17TuprKegDuu/Q4vOMjI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; b=xisaUu9snmShx1Uv3SMN6UEjLfZ0ju58SZoQHtxmr02HHCcvU1l2Z4Y+kM27+m0zSD xs5JPC6glVwD20i+LrHe+Pcopc3QE1L991BfTr6CKSdBF0nHSzEFYdn24+k/lPKvK1I5 2sgVzMMFFRec7M9tejrkw8y0/HoV+s1Vk4/a4= MIME-Version: 1.0 Received: by 10.90.100.20 with SMTP id x20mr1852684agb.37.1236854566348; Thu, 12 Mar 2009 03:42:46 -0700 (PDT) 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> <002201c9a2f9$edd47510$c97d5f30$@com> Date: Thu, 12 Mar 2009 06:42:46 -0400 X-Google-Sender-Auth: c1d64fe2a8c856e2 Message-ID: <491f66a50903120342j67e87c7dob10cfdc23580c8f4@mail.gmail.com> Subject: Re: Totally weird behaviour in org.postgresql.Driver From: Dave Cramer To: Peter Cc: Kris Jurka , pgsql-jdbc@postgresql.org Content-Type: multipart/alternative; boundary=0016361e87f010e7680464e9a519 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 200903/62 X-Sequence-Number: 19392 --0016361e87f010e7680464e9a519 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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 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 > --0016361e87f010e7680464e9a519 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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 t= he bug ?

On Thu, Mar 12, 2009 at 6:04 AM, Peter <peter@greatnowhe= re.com> wrote:
>> I added a bunch more synchronize= d (mutex) blocks in my code to avoid
>> getConnection being called simultaneously from multiple threads (w= hy
>> isnt that method synchronized anyway?) - so far so good... keeping=
>> fingers crossed. Maybe should have just subclassed PGSimpleDataSou= rce...

>I'm not sure why it needs to be synchronized. =A0The attached test = case (20
threads each opening and closing >connections as fast as possible) doesn= 't
show any problems for me. =A0Does it fail for you? =A0Is there a >partic= ular
change that fixes it for you?

I'm not sure if it needs to be synchronized either. Most li= kely I'm running
into a JVM bug in my particular environment (Tomcat servlet). I adapted you= r
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)<= br> blocks the bug is back with a vengeance...

final public class dbManager {


=A0 =A0private static PGSimpleDataSource _ds;
=A0 =A0// Our global instance
=A0 =A0private static dbManager _dbManager =3D null;

=A0 =A0 =A0 =A0/**

=A0 =A0 =A0 =A0 * Attempts to create a valid PG DataSource
=A0 =A0 =A0 =A0 *
=A0 =A0 =A0 =A0 * @return a boolean representing whether the DS was succes= sfully
created
=A0 =A0 =A0 =A0 */
=A0 =A0 =A0 =A0private synchronized static boolean createDataSource() { =A0 =A0 =A0 =A0try {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0synchronized (dbManager.class) {

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0_ds =3D new PGSimpleDataSource();
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0_ds.setServerName("mys= erver");
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0_ds.setDatabaseName("m= ydb");
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0_ds.setPortNumber(5432);
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return true;
=A0 =A0 =A0 =A0} catch (Exception ex) {
=A0 =A0 =A0 =A0 =A0 =A0Logger.getLogger(dbManager.class.getName()).log(Lev= el.SEVERE,
null, ex);
=A0 =A0 =A0 =A0 =A0 =A0return false;
=A0 =A0 =A0 =A0}
=A0 =A0 =A0 =A0}

=A0 =A0public synchronized static dbManager getInstance(String connString)= {
=A0 =A0 =A0 =A0if ( _dbManager =3D=3D null ) {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if ( createDataSource() ) {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0_dbManager =3D new dbManager();
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}
=A0 =A0 =A0 =A0}

=A0 =A0 =A0 =A0return _dbManager;
=A0 =A0}

=A0 =A0/**
=A0 =A0 *
=A0 =A0 * @return returns a connection created w/ default initial credenti= als
and very little access rights
=A0 =A0 */
=A0 =A0public synchronized Connection getInitialConnection() throws
SQLException {
=A0 =A0 =A0 =A0synchronized (dbManager.class) {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return _ds.getConnection("inituser&quo= t;, "initpass");
=A0 =A0 =A0 =A0}
=A0 =A0}

=A0 =A0/**
=A0 =A0 *
=A0 =A0 * @param dbUser
=A0 =A0 * @param dbPass
=A0 =A0 * @return a connection if it can be created, null otherwise
=A0 =A0 */
=A0 =A0public synchronized Connection getNewUserConnection(String dbUser,<= br> String dbPass){
=A0 =A0 =A0 =A0try {

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0synchronized (dbManager.class) {

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Connection userConn =3D _ds= .getConnection(dbUser,
dbPass);
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return userConn;

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}

=A0 =A0 =A0 =A0 =A0 =A0} catch (Exception ex) {

Logger.getLogger(dbManager.class.getName()).log(Level.SEVERE, null, ex); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return null;
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}
=A0 =A0}
}


--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc

--0016361e87f010e7680464e9a519--