• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Closing Database connections

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Pagination using Scriptlet that works great where I am using two database connections to the MySQL database. Now I want to close the connections correctly but cant seem to figure out the best way to do it. I assume for JSP this is okay to open up the database connections the way this program is running but was wondering how do I correctly close the connections.

My attempt below seems to freeze the page where it wont come up.
Please advise because I have tried several attempts of closing the connections but everytime the page doesnt come up. If I get rid of my try/catch/finally blocks the page works great.


[ May 12, 2007: Message edited by: Mike Jenkins ]
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Mike,

you code does not compile anymore if you define ocon etc inside the catch block. Define them before the try block.

Herman
PS: this page is just for studying right?
If not have a look at http://displaytag.sourceforge.net/11/
 
Mike Jenkins
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,

It now works great and thanks for solving my problem. This is a real web page and is not for study. I working with a very small MySQL database that has 1000 records taking up less than 1 MB of space. I cant download any display tag library due to the environment and limitations I am in so I have to manually create this Pagination.
Can someone advise if the way I am doing my Pagination is efficient where I connect to the database 2 times and have to close it 2 times. Is there a better way to do it?
[ May 14, 2007: Message edited by: Mike Jenkins ]
 
Herman Schelti
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Mike,

You can use the first connection object ("ocon") for both select-statements, so you don't need the second one (the "con").

If the data in your database doesn't change much, you could store the results in the session-scope from the user or even in the applicationcontext-scope (if you have enough memory for that).
So you just have to do the sql-query once.

Herman

PS: your code would be even more efficient if you would use connection-pooling. Your application-server probably provides that.
Or use the JSP for the view only, instead of using it for backend-stuff

And what about those limitations? Does your company not allow you to use well-tested and well-known open-source software?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic