It's not a secret anymore!*
The moose likes JDBC and the fly likes access connection pool in data layer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "access connection pool in data layer" Watch "access connection pool in data layer" New topic
Author

access connection pool in data layer

Ningfeng Xu
Greenhorn

Joined: Jan 22, 2002
Posts: 18
In a typical web application, there are 3 tiers, presentation, business logic, and data access. Data layer has no access to any parameters in presentation/web tier, like request, session, context. All data layer needs is passed down the 2 top tiers though method signatures.

Now for traditional database programming, I can put code like DriverManager.getConnection(url) to create database connection whenever I want, and such code in data layer is just right, no offense to the tiered philosophy.

In case of database connection pooling, naturally I would let the web container create the connection pool in servlet context listener. Then how can I reference the pool in data layer, without passing this as parameter though top layers of data access?

Currently I have two solutions:

1 using the following code in data layer:

InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB");
conn = ds.getConnection();

This code may be very expensive.

2 create a singleton referring the connection pool in servlet context
listener, then get reference of this singleton in data layer.

Any other thoughts? Thanks.


SCJP2
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
Use option #1 along with the Service Locator design pattern.
Ningfeng Xu
Greenhorn

Joined: Jan 22, 2002
Posts: 18
Scott,

I looked into the service locator in the link. Kind of confused. If the web tier service locator(is a singleton) is not registered in any long-lived object, say, like servlet context(web application/web container), would it be garbage collected somtime later? My understanding is for singleton, it has to be referred so kept alive, preventing from GC.

Any input?

Thanks.

Ningfeng
Jignesh Patel
Ranch Hand

Joined: Nov 03, 2001
Posts: 626

Here is one sample of Service Locator.

You don't need to register Service Locator anywhere, just intialize it with Static key word.
Ningfeng Xu
Greenhorn

Joined: Jan 22, 2002
Posts: 18
Thank all you guys!

This is my current solution:

1 currently no cache/service locator, since at this time I only have this connection pool necessary for global access.
2 use singletion, not registered anywhere;
3 instantiate the singleton in servlet context listener, once and only once, here and only here; so in case the singleton is out of memory or lost, I will be aware of it!

It is interesting that I could not find any helpful official documentation on class garbage collection, Xonclassgc...

I am not quiet convinced about the singleton without registration yet. I like the context listener(create instance when container starts up) , plus, I haven't used it before! A good chance to try...


[ October 15, 2006: Message edited by: Ningfeng Xu ]
 
jQuery in Action, 2nd edition
 
subject: access connection pool in data layer