| Author |
Closing The connection (Database)
|
Jigar Naik
Ranch Hand
Joined: Dec 12, 2006
Posts: 744
|
|
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 ]
|
Jigar Naik
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12953
|
|
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.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Rahul Bhattacharjee
Ranch Hand
Joined: Nov 29, 2005
Posts: 2300
|
|
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.
|
Rahul Bhattacharjee
LinkedIn - Blog
|
 |
Jigar Naik
Ranch Hand
Joined: Dec 12, 2006
Posts: 744
|
|
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
Joined: Nov 29, 2005
Posts: 2300
|
|
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.
|
 |
 |
|
|
subject: Closing The connection (Database)
|
|
|