• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

connection close and connection null

 
Ranch Hand
Posts: 1609
  • 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!
 
Rancher
Posts: 989
9
  • 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.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is: java.sql.Connection#isClosed().
 
Greenhorn
Posts: 29
Eclipse IDE Tomcat Server Java
  • 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
  • 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: 1609
  • 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: 1609
  • 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: 1609
  • 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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • 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
  • 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: 1609
  • 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
  • Likes 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: 1609
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks much!
 
Do you want ants? Because that's how you get ants. And a tiny ads:
ScroogeXHTML 8.7 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/730700/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic