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?
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.
You don't need to register Service Locator anywhere, just intialize it with Static key word.
Joined: Jan 22, 2002
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 ]