This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Session Listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session Listener" Watch "Session Listener" New topic
Author

Session Listener

Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
Hi all,

For opening and closing a database connection, can i use HttpSessionListener? When i open a new session, i can open db connection. but i remove the session i will close db connection.. My doubt is that will my methodology affect the other session's db connection???


Thanks,
Abu
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

hey,

Holding a database Connection for this long is probably not a wise idea. I think there is a design pattern connection-per-request, so every request gets its own database connection which is closed as the request is sent back.

This can be easily achieved with the use of a Filter.

Sean


I love this place!
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
hi sean,

Please demonstrate the bit of db code using filter...I still dont get it


Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Sean Clark wrote:Holding a database Connection for this long is probably not a wise idea.

I don't think there's anything particularly unwise about how 'long' a database connection is held. If that was the case, connection pools would have never come to exist. What is usually unwise is how a particular 'live' connection is sustained. In any application which uses database for persistence, establishing new connections consume the greatest amount of time.

Abubacker, whether or not you choose session listeners to maintain the lifecycle of db connections in your system depends on how many parallel sessions you expect for your app to cater to. If say, there's a possibility of 50 - 100 parallel users [a very common scenario for a website], then it can be bad to have that many live db connections. Worse case - your db might stop responding completely. I'd suggest you look into database connection pooling as a more appropriate alternative.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Anirvan Majumdar wrote:I don't think there's anything particularly unwise about how 'long' a database connection is held. If that was the case, connection pools would have never come to exist. What is usually unwise is how a particular 'live' connection is sustained. In any application which uses database for persistence, establishing new connections consume the greatest amount of time.

I was half asleep when I wrote that - I was of course referring to keeping the connections live within the application as opposed to within a connection pool.

I'd suggest you look into database connection pooling as a more appropriate alternative.

I agree that this is advantageous to use, but you still have the same underlying question as to when do you create the connection/get connection from pool and when do you drop the connection/release to pool.

Sean
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
you still have the same underlying question as to when do you create the connection/get connection from pool and when do you drop the connection/release to pool.


I think Sean was right..because i expected that question..thats why i opted for HttpSessionListener which i thought could help me actively get connections whenever my clients open a new session..

Anirvan, Is there any get active connections while per user request. Because connection pooling mechanism is worth mentioning here. But The question is how to give the Connection Object...


Abu.A
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Abubacker Siddik wrote:Please demonstrate the bit of db code using filter...I still dont get it


Are you trying to say that you want to use filters for this?? Generally you create DAO classes for handling database related work. They should handle the database connections themselves (or through a call to a method in a common class which will give them a database connection). And the best way is to use a connection pool as Anirvan said, creating a new connection every time you connect to a database takes more resources and time...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Well, what you could do is - set up the connection pool in the init of a startup servlet, or contextInitialized of a context Listener class. Session listeners aren't really the best of places for initialisation of resources which have an application scope.
Expose the connection pool as a datasource using JNDI, or simply expose an instance of your connection pool driver class. Thereafter, as Sean mentioned, the best way to get and return a connection from the pool will be on a per-request-basis. However, I won't really suggest doing so using Filters, since there might be quite a few request calls to your server for which there are no db operations to be performed. If you're using any of the J2EE patterns, then typically your data value object implementation layer classes would be the best places to get and close a connection.
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
Well Guys, I think you have to come to my question right now.
Anirvan, I can use connection pooling using the application server. I wonder if i use ContextListener for this purpose, as long as my server is running, my connections will be there..Doesnt it seem unwise?


Ankit Garg, Ya i know there is a pattern called DAO which we could use for interacting DB calls. Sean suggested me to use filters, thats y i asked to show a demo..
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Abubacker Siddik wrote:Well Guys, I think you have to come to my question right now.
Anirvan, I can use connection pooling using the application server. I wonder if i use ContextListener for this purpose, as long as my server is running, my connections will be there..Doesnt it seem unwise?

Ankit Garg, Ya i know there is a pattern called DAO which we could use for interacting DB calls. Sean suggested me to use filters, thats y i asked to show a demo..

By coming to your question, do you want us to agree with an incorrect answer you've already decided upon? AFAIK, we've tried to answer your question to the best of our abilities. Using session listeners or filters boils down to a question of purpose. Ankit mentioned DAO, because that's the correct way to go about it. Sean mentioned filters, because that can be an easy [but not necessarily efficient] way to go about it. The choice afterall, is yours alone to make depending on which way seems more applicable to your case.
Why ContextListeners/startup servlets are better, is a design consideration. Like I mentioned before, application scope resources are best initialised and used through these components rather than a SessionListener. If you read through the db connection pooling article I referred before, I think your question about keeping connections alive for too long would be suitably answered. Lookout for something about stale connections there. And no, it's not at all 'unwise'.

I hope that should bring us to your question now?
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
Fine dude.. you cleared me enough.. My question to choose between sessionlistener and contextlistener is cleared.

Thanks Guys.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Apologies guys, I have spent too much time recently using ORMs (Hibernate) where it is good practise (and shown on the Hibernate site) to implement it using a filter in web applications.

You should probably be using a DAO type thing with JDBC.

I shall leave it at that... sorry guys!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Sean: Are you talking about an OSiV filter? Define "good practice"--there are many reasons why that's *not* a good practice at all and can lead to poor performance.
praneeth ram
Ranch Hand

Joined: Sep 21, 2009
Posts: 60
Abubacker Siddik wrote:Hi all,

For opening and closing a database connection, can i use HttpSessionListener? When i open a new session, i can open db connection. but i remove the session i will close db connection.. My doubt is that will my methodology affect the other session's db connection???


Thanks,
Abu



if other components are partisipated in that sessions at that time those are also effected.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session Listener
 
Similar Threads
Closing a db connection
Session Variables and DB access...
If my hibernate-cfg.xml has multiple session factories then how can I Load appropriate SessionFactor
session listener
Hibernate with Application server connection pool