1

Recently, I have been facing a problem with throwing exception in JDBC topic.

I synchronized Connection object by using 2 synchronized methods: getConnection() and releaseConnection(). Then another method which removes a row from database as follow:

public void removeItem(int itemID) throws ItemNotFound { PreparedStatement ps = null; String query = "DELETE * FROM Student.Book WHERE id = ?"; getConnection(); try { ps = con.prepareStatement(query); ps.setInt(1, bookID); ps.executeUpdate(); } catch (SQLException sqle) { this.close(null, null, ps); // method to close PreparedStatement and ResultSet releaseConnection(); throw new BookNotFoundException("Book not found!"); } finally { this.close(null, null, ps); releaseConnection(); } } 

Everything works well if no exception occurs. In case exception occurs, in catch block after releaseConnection() method, throw new BookNotFoundException("Book not found!") hangs up!! If i comment releaseConnection() method then it throws normally?

1
  • When putting code in a question or an answer, consider wrapping it with <pre> and <code>. For example: <pre><code>I'm noob and that's not bad.</code></pre> Commented Jan 5, 2011 at 18:35

4 Answers 4

5

releaseConnection(); is both in your finally and your catch. I don't think you want it in your catch block.

Sign up to request clarification or add additional context in comments.

Comments

2

You're releasing the connection twice. The finally block will always run, even if a catch block runs. My guess is it's doing the throw, then running the finally block, which tries to release the connection a second time. I'm not sure how you implemented releaseConnection(), but if that blocks when the connection has already been released then this would explain the problem.

I think if you just remove the close and releaseConnection from the catch block it should work fine.

Comments

1

You're releasing twice. with the finally, it looks like this in the exception case:

ps = con.prepareStatement(query); ps.setInt(1, bookID); ps.executeUpdate(); // catch this.close(null, null, ps); releaseConnection(); // finally this.close(null, null, ps); releaseConnection(); // exception flow throw new BookNotFoundException("Book not found!"); 

which probably isn't what you want.

you could remove the close/release stuff in your catch and just leave the throw, and it should work.

Comments

0

when you throw the exception, the releaseConnection() method is called twice; once in the exception handler and once in the finally.

Try a variation of this:

 public void removeItem(int itemID) throws ItemNotFound { PreparedStatement ps = null; String query = "DELETE * FROM Student.Book WHERE id = ?"; getConnection(); try { ps = con.prepareStatement(query); ps.setInt(1, bookID); ps.executeUpdate(); } catch (SQLException sqle) { this.close(null, null, ps); // method to close PreparedStatement and // ResultSet releaseConnection(); throw new BookNotFoundException("Book not found!"); } finally { if (isOpen()) { this.close(null, null, ps); releaseConnection(); } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.