Re: driver initialization and connection separation

From: Oliver Jowett <oliver(at)opencloud(dot)com>
To: Richard Troy <rtroy(at)ScienceTools(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: driver initialization and connection separation
Date: 2010-01-31 02:08:27
Message-ID: 4B64E61B.3010302@opencloud.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Richard Troy wrote:

> It seems to me that there's something missing from the initialization
> code, namely to remove the SSL features if the driver was already loaded.
> It should only be loaded when a URL asking for it comes along with the
> option ssl=true (for example). However, one could easily argue that it's
> not the initialization code that needs help but the code that constructs a
> new connection; it makes just as much sense that the request for a new
> connection give the appropriate type of connection, with or without SSL
> depending on whether the option in the URL was specified.

Driver initialization has nothing to do with this; you are making
assumptions about Class.forName() that are not correct. It does not
"initialize" the driver in any way other than what the JVM requires for
any other class (see below)

If you get a different sort of SSL-vs-not-SSL connection for the same
connection URL depending on what connections you got in the past - that
*is* a bug. Do you have a testcase showing the problem?

> ...Your follow-on comment (below) about classes being initialized only
> once isn't helpful here because it isn't clear to me whether this is a
> case of initializing an object or loading _code_ that instantiates
> objects. In other words, even though I've been using Java since it's first
> release, I've never before had to worry about the code that instantiates
> objects changing while my code is running, so I haven't thought about it
> much. To my mind, each instance of my objects which then instantiate other
> objects - like JDBC connection objects - should get clean, separate and
> distinct objects, so in effect the driver code is loaded multiple times.
> But as I said, I could be wrong - I don't know much about how the JVM
> manages code that's loaded, but my suspicion is that it'll manage the
> individual instances of loading the same driver separately.

Class.forName()'s comment about initialization is talking about class
initialization as described in the JVM spec - it involves (for example)
running static initializer blocks in the class. This is guaranteed to
happen before certain uses of the class (e.g. calling any method), and
can be forced early via Class.forName(). But it is only ever done once
per class.

If you load the same class in a different classloader, it's effectively
a different class, so will be separately initialized. (But I don't
recommend doing that with the JDBC driver, because it'll mess with
driver registration)

-O

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Lew 2010-01-31 02:08:50 Re: driver initialization and connection separation
Previous Message Richard Troy 2010-01-31 01:31:19 Re: driver initialization and connection separation