• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

connection close and connection null

 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3747
62
Chrome Netbeans IDE Oracle
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is: java.sql.Connection#isClosed().
 
Aniket Khadke
Greenhorn
Posts: 29
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3747
62
Chrome Netbeans IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reaad:http://java.sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks much!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic