You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-dev@db.apache.org by "David W. Van Couvering" <Da...@Sun.COM> on 2005/11/19 02:00:57 UTC

How does an application get access to JDBC implementation classes

Hi, all.  I would like to know exactly what are the approved/documented 
ways to get a connection.

My reason for asking this is, I want to understand what all touch points 
there are in JDBC that allow you to get access to the first 
implementation class for a given driver.  If I have it right, everything 
hangs off of a Connection, so what are the ways in which you get a 
Connection?

I know one way is to load the driver class and then use 
DriverManager.getConnection().

Another way is, in a managed environment,  use JNDI or some other 
mechanism to load a registered DataSource and then use that to get a 
connection.

Are there other ways?  Can, for instance, a user directly create an 
driver specific DataSource instance (rather than going through JNDI) and 
then use that to create connections?  Or is that not a supported mechanism?

Thanks,

David

Re: How does an application get access to JDBC implementation classes

Posted by "Lance J. Andersen" <La...@Sun.COM>.

David W. Van Couvering wrote:

> Hi, all.  I would like to know exactly what are the 
> approved/documented ways to get a connection.
>
> My reason for asking this is, I want to understand what all touch 
> points there are in JDBC that allow you to get access to the first 
> implementation class for a given driver.  If I have it right, 
> everything hangs off of a Connection, so what are the ways in which 
> you get a Connection?
>
> I know one way is to load the driver class and then use 
> DriverManager.getConnection().
>
> Another way is, in a managed environment,  use JNDI or some other 
> mechanism to load a registered DataSource and then use that to get a 
> connection.
>
> Are there other ways?  Can, for instance, a user directly create an 
> driver specific DataSource instance (rather than going through JNDI) 
> and then use that to create connections?  Or is that not a supported 
> mechanism?

You can directly create an instance of a given DataSource in leiu of 
using DriverManager.  There are a required number of setters and any 
additional properties must all be available via a setter.

>
> Thanks,
>
> David


Re: How does an application get access to JDBC implementation classes

Posted by Francois Orsini <fr...@gmail.com>.
Yes the following below is supported...

ClientDataSource ds = new ClientDataSource();
ds.setServerName("someHost");
ds.setDatabaseName("dbName");
ds.setUser("userName");
ds.setPassword("userPwd");

ds.setPortNumber(portNumt);

Connection conn = ds.getConnection();

Statement stmt = conn.createStatement();

[...]

and same for EmbeddedDataSource

--francois

Re: How does an application get access to JDBC implementation classes

Posted by Knut Anders Hatlen <Kn...@Sun.COM>.
"David W. Van Couvering" <Da...@Sun.COM> writes:

> How do the instances of these get created?  It looks like the data
> sources are generally registered with JNDI using
> getObjectInstance(). But do we allow users to create new instances
> directly (e.g. "new ClientDataSource()")?  I noticed the constructor
> was public...
>
> It looks like Driver creates a new instance of itself when the class
> is loaded.  Do I have that right?

Yes, but...

> But again, we don't support someone
> saying "new ClientDriver()" directly, right?  Again, I noticed that
> this is currently a public method.

I think the constructor has to be public since
Class.forName("org.apache...").newInstance() is the recommended way to
load the Derby driver. Read this discussion for details:
http://www.mail-archive.com/derby-user@db.apache.org/msg01645.html

-- 
Knut Anders


Re: How does an application get access to JDBC implementation classes

Posted by Amit Handa <Am...@Sun.COM>.

David W. Van Couvering wrote:

> Thanks, Dan.  Can you point me to something that talks more about what 
> it means to be a "valid OSGi bundle?"
>
> Lance, do you have any information on how a driver is loaded in JDBC4 
> using the service provider mechanism?
>
First the driver loading mechanism is applicable for non managed 
scenarios or stand alone java programs.
This will realy not have any value in a managed environment.

Second this is based on packaging your driver jar as a Service based 
upon the Jar Specification
(java.util.Service is part of Mustang)
http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Service%20Provider

When a driver is *packaged as a Service*,  you don't need to  explicitly
(1) know the name of the class
(2) load that implementation of java.sql.Driver class explicitly to get 
a connection handle

Further this feature will not break any backward compatibility, all 
applications running will work fine
with new versions of java as long as you
(1) don't package your driver class as a service AND
(2) keep doing Class.forName(x.y.z);

But this is a good Ease of Development feature where the user need not 
be bothered about
knowing the java.sql.Driver implementation class and then loading it.

thanks,
Amit

> Thanks,
>
> David
>
> Daniel John Debrunner wrote:
>
>> David W. Van Couvering wrote:
>>
>>> OK, I think I may have the answer to my question.  Looking at the JDBC
>>> API, here is what I can figure out.  There are the following Connection
>>> factories that a JDBC implementation must provide:
>>>
>>> Driver
>>> DataSource
>>> XADataSource
>>> ConnectionPoolDataSource
>>>
>>> Next question:
>>>
>>> How do the instances of these get created?  
>>
>>
>>
>> In addition to the other ways already discussed, JDBC 4.0 is adding the
>> automatic registration of JDBC drivers using the "Java Standard Edition
>> Service Provider mechanism". Thus in JDBC 4.0, this is valid JDBC code.
>>
>> public static void main(String[] args)
>> {
>>    // no need to load a driver
>>    Connection conn = DriverManager.getConnection("jdbc:derby:cs");
>> }
>>
>> This may have an impact on those looking at class loaders to support
>> multiple Derby drivers. I didn't find (in a very quick look) any
>> information on Java SE Service Provider, and how it handles class 
>> loading.
>>
>> Also derby.jar is a valid OSGi bundle, this may also have some impact on
>> those looking at class loaders for multiple multi-version derby 
>> instances.
>>
>> And, in fact the code above is valid in JDBC 2.0 and 3.0. If the driver
>> class name is in the system property "jdbc.drivers", then it will be
>> loaded automatically. Again I haven't looked to see how this handles
>> class loaders.
>>
>> Dan.
>>
>>

Re: How does an application get access to JDBC implementation classes

Posted by "Lance J. Andersen" <La...@Sun.COM>.
Amit forwarded info on this, so i think you have what you need.

-lance

David W. Van Couvering wrote:

> Thanks, Dan.  Can you point me to something that talks more about what 
> it means to be a "valid OSGi bundle?"
>
> Lance, do you have any information on how a driver is loaded in JDBC4 
> using the service provider mechanism?
>
> Thanks,
>
> David
>
> Daniel John Debrunner wrote:
>
>> David W. Van Couvering wrote:
>>
>>> OK, I think I may have the answer to my question.  Looking at the JDBC
>>> API, here is what I can figure out.  There are the following Connection
>>> factories that a JDBC implementation must provide:
>>>
>>> Driver
>>> DataSource
>>> XADataSource
>>> ConnectionPoolDataSource
>>>
>>> Next question:
>>>
>>> How do the instances of these get created?  
>>
>>
>>
>> In addition to the other ways already discussed, JDBC 4.0 is adding the
>> automatic registration of JDBC drivers using the "Java Standard Edition
>> Service Provider mechanism". Thus in JDBC 4.0, this is valid JDBC code.
>>
>> public static void main(String[] args)
>> {
>>    // no need to load a driver
>>    Connection conn = DriverManager.getConnection("jdbc:derby:cs");
>> }
>>
>> This may have an impact on those looking at class loaders to support
>> multiple Derby drivers. I didn't find (in a very quick look) any
>> information on Java SE Service Provider, and how it handles class 
>> loading.
>>
>> Also derby.jar is a valid OSGi bundle, this may also have some impact on
>> those looking at class loaders for multiple multi-version derby 
>> instances.
>>
>> And, in fact the code above is valid in JDBC 2.0 and 3.0. If the driver
>> class name is in the system property "jdbc.drivers", then it will be
>> loaded automatically. Again I haven't looked to see how this handles
>> class loaders.
>>
>> Dan.
>>
>>

Re: How does an application get access to JDBC implementation classes

Posted by "David W. Van Couvering" <Da...@Sun.COM>.
Thanks, Dan.  Can you point me to something that talks more about what 
it means to be a "valid OSGi bundle?"

Lance, do you have any information on how a driver is loaded in JDBC4 
using the service provider mechanism?

Thanks,

David

Daniel John Debrunner wrote:
> David W. Van Couvering wrote:
> 
>>OK, I think I may have the answer to my question.  Looking at the JDBC
>>API, here is what I can figure out.  There are the following Connection
>>factories that a JDBC implementation must provide:
>>
>>Driver
>>DataSource
>>XADataSource
>>ConnectionPoolDataSource
>>
>>Next question:
>>
>>How do the instances of these get created?  
> 
> 
> In addition to the other ways already discussed, JDBC 4.0 is adding the
> automatic registration of JDBC drivers using the "Java Standard Edition
> Service Provider mechanism". Thus in JDBC 4.0, this is valid JDBC code.
> 
> public static void main(String[] args)
> {
>    // no need to load a driver
>    Connection conn = DriverManager.getConnection("jdbc:derby:cs");
> }
> 
> This may have an impact on those looking at class loaders to support
> multiple Derby drivers. I didn't find (in a very quick look) any
> information on Java SE Service Provider, and how it handles class loading.
> 
> Also derby.jar is a valid OSGi bundle, this may also have some impact on
> those looking at class loaders for multiple multi-version derby instances.
> 
> And, in fact the code above is valid in JDBC 2.0 and 3.0. If the driver
> class name is in the system property "jdbc.drivers", then it will be
> loaded automatically. Again I haven't looked to see how this handles
> class loaders.
> 
> Dan.
> 
> 

Re: How does an application get access to JDBC implementation classes

Posted by Daniel John Debrunner <dj...@debrunners.com>.
David W. Van Couvering wrote:
> OK, I think I may have the answer to my question.  Looking at the JDBC
> API, here is what I can figure out.  There are the following Connection
> factories that a JDBC implementation must provide:
> 
> Driver
> DataSource
> XADataSource
> ConnectionPoolDataSource
> 
> Next question:
> 
> How do the instances of these get created?  

In addition to the other ways already discussed, JDBC 4.0 is adding the
automatic registration of JDBC drivers using the "Java Standard Edition
Service Provider mechanism". Thus in JDBC 4.0, this is valid JDBC code.

public static void main(String[] args)
{
   // no need to load a driver
   Connection conn = DriverManager.getConnection("jdbc:derby:cs");
}

This may have an impact on those looking at class loaders to support
multiple Derby drivers. I didn't find (in a very quick look) any
information on Java SE Service Provider, and how it handles class loading.

Also derby.jar is a valid OSGi bundle, this may also have some impact on
those looking at class loaders for multiple multi-version derby instances.

And, in fact the code above is valid in JDBC 2.0 and 3.0. If the driver
class name is in the system property "jdbc.drivers", then it will be
loaded automatically. Again I haven't looked to see how this handles
class loaders.

Dan.



Re: How does an application get access to JDBC implementation classes

Posted by "David W. Van Couvering" <Da...@Sun.COM>.
OK, I think I may have the answer to my question.  Looking at the JDBC 
API, here is what I can figure out.  There are the following Connection 
factories that a JDBC implementation must provide:

Driver
DataSource
XADataSource
ConnectionPoolDataSource

Next question:

How do the instances of these get created?  It looks like the data 
sources are generally registered with JNDI using getObjectInstance(). 
But do we allow users to create new instances directly (e.g. "new 
ClientDataSource()")?  I noticed the constructor was public...

It looks like Driver creates a new instance of itself when the class is 
loaded.  Do I have that right?  But again, we don't support someone 
saying "new ClientDriver()" directly, right?  Again, I noticed that this 
is currently a public method.

Thanks,

David

David W. Van Couvering wrote:
> Hi, all.  I would like to know exactly what are the approved/documented 
> ways to get a connection.
> 
> My reason for asking this is, I want to understand what all touch points 
> there are in JDBC that allow you to get access to the first 
> implementation class for a given driver.  If I have it right, everything 
> hangs off of a Connection, so what are the ways in which you get a 
> Connection?
> 
> I know one way is to load the driver class and then use 
> DriverManager.getConnection().
> 
> Another way is, in a managed environment,  use JNDI or some other 
> mechanism to load a registered DataSource and then use that to get a 
> connection.
> 
> Are there other ways?  Can, for instance, a user directly create an 
> driver specific DataSource instance (rather than going through JNDI) and 
> then use that to create connections?  Or is that not a supported mechanism?
> 
> Thanks,
> 
> David