• 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 The connection (Database)

 
Ranch Hand
Posts: 763
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In my web application.

i have one java class which contains more than 20 method. each method does database related operations. so i have created one constructor for that class.
which parse xml for connection and create one connection conn. I'm using 1 connection for all the 20 method.

now ideally where should i close the connection ???

if i close connection in the finally clause of every method. it will give run time exception on next method. as i'm creating connection once when my class is loaded(In Constructor).

some code bellow here can give you ruff idea.


[ May 08, 2007: Message edited by: Jigar Naik ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A very short introduction to using databases in web applications:

In a web application, you should not be opening and closing JDBC connections directly like that. You should configure a database connection pool in your servlet container, and then in your web application lookup the DataSource from JNDI. You get the connection from the DataSource object. After you're done, you just call close() on the connection, which doesn't really close it in that case; it just returns it to the pool.

Instructions for configuring a data source in Tomcat

Your code should look something like this:


One remark about your code above:

Why did you include this? This shouldn't be necessary. I'd remove the call to System.runFinalization() unless there is a very specific reason why you need this.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why did you include this? This shouldn't be necessary. I'd remove the call to System.runFinalization() unless there is a very specific reason why you need this.



I too feel that its not necessary.

Well when you close a connection then exception might occur , so it would be a good feature to check in the finally whether its closed or not again.In case its not closed then close it again and null the reference.
 
Jigar Naik
Ranch Hand
Posts: 763
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Well when you close a connection then exception might occur , so it would be a good feature to check in the finally whether its closed or not again.In case its not closed then close it again and null the reference



if i close connection in finally clause.

than won't it throw exception on next method which also need connection ?

The code bellow will call method of Mail class.

ControllerServlet
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jigar Naik:


if i close connection in finally clause.

than won't it throw exception on next method which also need connection ?



As I and Jasper have already mentioned that when you close a connection that has been taken from a connection pool , in that case the actual connection object will not be closed rather returned to the pool.Again when you need connection then you have to again ask the pool to give you a connection.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic