• Post Reply Bookmark Topic Watch Topic
  • New Topic

Destroying java bean and odbc connection  RSS feed

 
Michael Crutcher
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to Jsp/Servlets so I might be completely off base in the approach I took. If this is the case and you have a better way to do what I'm about to describe, please let me know.
I have a set of jsp pages that I want list boxes on. The values that will populate these list boxes reside in various tables on a database. Different pages have different sets of list boxes that they need. The way I thought that I'd do this is through a java bean. Each property of the java bean would return a String that represented the list box. The actual logic of the get method would actually make the call to the database and format the response. This works perfectly, and is a pretty nice solution on the jsp side, but I have one problem:
How do I close the odbc connection?
As part of the constructor I could populate all of the properties for the bean, and then close the connection at the end of the constructor. I don't want to do this, though, because each page will only need a couple of list boxes, and there are literally hundreds to choose for. I could also add a get method that returns a blank string and as a side effect closes the connection. I don't want to do this, however, because its ugly and I don't want the jsp designer to have to remember to add the dummy getProperty value. The bean is page-scoped and I want it destroyed as soon as the user moves to another page. I don't want to wait for the user 's session to time out.
Any suggestions?
Michael Crutcher
 
Jesse Beaumont
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you just open and close a jdbc connection for every method? If you use a connection pooling solution the overhead of that is fairly low, because the connections remain open in the pool anyway. There are tons of free connection pooling implementations (or they are fairly easy to put together if that is simpler)
Jesse
 
Michael Crutcher
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you recommend one connection pooling solution?
 
Philip Shanks
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One approach that I have used is to store a reference to the Connection object as a Session property. This makes the same connection object available to all pages in the session. The connection is then closed when the session finally times out.
A plus for this approach is that I don't have to implement any special connection pooling (I am using a Tomcat server).
Of course, this is not a good idea for a high traffic site, but that depends on the application and its clientele.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!