Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

access connection pool in data layer

 
Ningfeng Xu
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use option #1 along with the Service Locator design pattern.
 
Ningfeng Xu
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 626
Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic