Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting error java.sql.SQLException: Invalid state, the Statement object is closed  RSS feed

 
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have following code.

In most cases it works fine but for some cases it will give error "java.sql.SQLException: Invalid state, the Statement object is closed" when i call stmt.close() in finally block. What is possibility of occurring such error.

Thank you
Salman
 
Rancher
Posts: 3492
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What sort of application does this code sit in?

Where does the 'con' variable live?
Is it ever closed?
Can I suggest adding a catch block in that try/catch to see whether the finally is occurring due to a prior exception, so you can log the exception, or wrap the close call (probably preferable) in a try/catch?
 
salman khandu
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does the 'con' variable live? <--- It is static class variable  initialize with the method getConnection() .

Is it ever closed? <-- Managed by connection pooling so no need to close manually.

Can I suggest adding a catch block in that try/catch to see whether the finally is occurring due to a prior exception, so you can log the exception, or wrap the close call (probably preferable) in a try/catch? <- It handles from this method called.


Everything works fine in the most case. But in rare cases, it gives an error  java.sql.SQLException: Invalid state, the Statement object is closed

Thank you
 
Sheriff
Posts: 23506
47
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a threading race condition to me. Especially as the problem occurs rarely for no apparent reason.

Wild guess: Thread 1 gets a connection, executes a query... then Thread 2 gets the same connection but before it can finish executing the query, Thread 1 closes the connection, which automatically closes Thread 2's query.

Or something like that. You've said that the connection is stored in a class-level variable so it's quite possible that (despite your use of a connection pool) both threads use the same connection.
 
Dave Tolls
Rancher
Posts: 3492
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't say what sort of application this sits in.
If (as Paul suggests) it's a multi-threaded environment then a static connection is a recipe for problems.
Since you have a connection pool anyway there is no need for a single static instance.  Just get a connection from the pool for a transaction, then close when finished.

However, as for the SQLException try/catch blocks, since you have no catch block in there then any prior exception (which I think is being thrown) is going to vanish if the close() method throws an exception.
That's why you should wrap the close() in a try/catch so that you do not lose the original exception.

Using the above in your finally ensures that it is the original exception that is passed up, and also prevents a failure to close from causing an error.
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!