aspose file tools*
The moose likes JDBC and the fly likes connection close and connection null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "connection close and connection null" Watch "connection close and connection null" New topic
Author

connection close and connection null

Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
I have a class




I use this class to access "connection", there are times where connection might be closed, and performing any operation gives me "connection already closed" error.

is there anything like,


So that I can check it in advance before performing the operation?
Any suggestions are welcome. Thanks!


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Calling the method close on a Connection object that is already closed is a no-op.

So if you are getting a connection already closed exception then it's not because of calling the close method. It's because your application is actually trying to use a closed connection not close it.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Yes, there is: java.sql.Connection#isClosed().
Aniket Khadke
Greenhorn

Joined: Jul 13, 2011
Posts: 29

Akhilesh Trivedi wrote:I have a class




I use this class to access "connection", there are times where connection might be closed, and performing any operation gives me "connection already closed" error.

is there anything like,


So that I can check it in advance before performing the operation?
Any suggestions are welcome. Thanks!
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Better make all closed connections disappear from the available connections so that you don't have to do the isClosed check everywhere.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
Martin Vajsar wrote:Yes, there is: java.sql.Connection#isClosed().


Is it only in Java 7, not prior to that?

(EDITED --- seems to be available on previous versions too. thanks!)
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
E Armitage wrote:So if you are getting a connection already closed exception then it's not because of calling the close method. It's because your application is actually trying to use a closed connection not close it.


Yes. As I said, the 'connection' field is common to all methods in MyDBClass. A method may close it, while the other method may not know about it, I want to make sure before hand that connection is not already closed.

If it is closed, I would nullify it and create another one before doing my operation.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
E Armitage wrote:Better make all closed connections disappear from the available connections so that you don't have to do the isClosed check everywhere.


Meaning?
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

It's there right from the beginning (otherwise the JavaDoc would list the version in which it was introduced in the Since: section).
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Akhilesh Trivedi wrote:
E Armitage wrote:Better make all closed connections disappear from the available connections so that you don't have to do the isClosed check everywhere.



Meaning?


Nullify it when you close it instead and make sure all methods always get a new open connection. So don't use an instance connection field. Instead have a getConnection method that always returns an open connection.
When you get it to work then consider using a connection pool.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
E Armitage wrote:When you get it to work then consider using a connection pool.


I have this general opinion that connection pools are only on web/app-servers. Mine is a stand-alone app. will I need to club JNDI?
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Reaad:http://java.sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
Thanks much!
 
 
subject: connection close and connection null