• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet Context and database connection  RSS feed

 
damian croft
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
irst of all, my apologies, this is an unashamed cross post from the sun forums, but I didn't get an answer there, so may I try here?

Secondly, it isn't directly realted to the exam, but it does come out of the HFS book, so there might well be someone here who is able to help me.

Following the "Head First Servlets and JSP" philosphy, I create my database connection in the conntextInitialised() method of my ServletContextListener class and set it as an attribute of my ServletContext. Then, inside my servlet code, I can retrieve the connection object calling getAttribute() on my ServletContext. That all works fine.

But after a few hours, the connection times out, so I have to do the following;

which strikes me as a rather inelegant solution.

How can I improve on this? Should I be using a DataSource object?

My webapp doesn't have to make a lot of calls to the database, so the problem is not so much about sharing connections, rather its about keeping the database connection "alive" so I can avoid the above code.

Any suggestions on this would be a great help. Thanks to all

Damian.

ps. Just a note. I passed the exam last week with 79%. It was a bit harder than I expected (or rather, I'd been hoping for more, no-brainer type questions), but I'd prepared well and studied hard. As a note of encouragement to anyone about to take the exam.... I scored about 58% on the HFS mock exam three days before the exam - but after that I pulled out all the stops and studied 8-9 hours a day for the last three days. It's not an easy exam, but the HFS book is a real must-have and all the words of encouragement in this forum go a long way to soothing troubled brains in the last days of revision!
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes this is the likely behavior. You had better go along with connection pooling mechanism of your servlet container or application server.

If using tomcat see DBCP.

And congrats you passed the exam. 79% is good.
[ March 06, 2005: Message edited by: Adeel Ansari ]
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm, I seriously doubt whether the book really expects you to create a database connection and store it for later use in the ServletContext.
Doing so is extremely bad practice and leads to all kinds of trouble (of which connection timeouts are the least and are actually a good thing).

Most likely you misunderstood the example and it was really using the connection in that method only inside the method and releasing it when done (I don't have the book but that's the only way people with any experience would use a database connection at that place in their code, and the writers of the book are experienced).
 
damian croft
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeroed,
Thanks for your reply. Yup, I take your point, and I too had always had some doubts about its wisdom, since I'd never come across it anywhere before. But since the book had more or less become my bible in the last couple of months, I'd blindly followed its advice. I've just checked in the book and on page 166 it has the following code:


Or have I really, really misunderstood it???

Thanks too Adeel for your reply. I've done a bit of digging around on dbcp as you suggested and came up with an article which I think is going to help (not had ime to do anything more than read it through yet):
http://java.sun.com/developer/Books/javaserverpages/tomcat/Sams-Tomcat-KS_ch09.pdf
And yes, Jeroed, I think it reinforces what you were saying.
 
damian croft
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry Jeroen - spelt your name wrong
 
Thomas Whitmore
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Damian, Jeroen,

Depends on your database. MySQL and other databases will definitely discard connections after a period of inactivity. This is a bugger for code which would otherwise be correct to cache database connections.

Jeroen makes a point -- keeping a DB Connection per servlet, is not usual practice. This probably wouldn't work correctly for threaded requests, once servlet was under load. Separate connections would typically be used for separate requests.

Single connection might be shared for 'singular' services such as key allocators etc, the problem here is that there is no portable api or SQL to check a connection is still good. The most performant technique is to detect failed queries and update the connection, but this is more complex to code. Most session activity (as opposed to keying/ management) still uses separate connections.

Our JDO product uses this technique, and the solution performs well. Let us know if this helps,


Cheers,
Thomas Whitmore
www.powermapjdo.com
 
damian croft
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Thomas for your reply.
Yes, your software looks interesting.....
I'll download a copy and give it a try.

Regards,
Damian
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!