Announcement Announcement Module
No announcement yet.
About release connection in finally block Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • About release connection in finally block

    Hi Spring guys:
    A Sun's engineer in our company told us that the code in finally block may not be executed immediately in high volume conditions. The test engineers in our company told us that they found that the connection may not be released immdiately too when the connection is closed in finally block (The code is our a utility class that may act as the same as spring's JdbcTemplate). I can not believe this, so i checked you guys' code "JdbcTemplate". I found that you guys do the same thing as me: to release resouce in finally block
    	public Object execute(final StatementCallback action) {
    		Connection con = DataSourceUtils.getConnection(getDataSource());
    		Statement stmt = null;
    		try {
    			Connection conToUse = con;
    			if (this.nativeJdbcExtractor != null &&
    					this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()) {
    				conToUse = this.nativeJdbcExtractor.getNativeConnection(con);
    			stmt = conToUse.createStatement();
    			DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
    			Statement stmtToUse = stmt;
    			if (this.nativeJdbcExtractor != null) {
    				stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt);
    			Object result = action.doInStatement(stmtToUse);
    			SQLWarning warning = stmt.getWarnings();
    			return result;
    		catch (SQLException ex) {
    			throw getExceptionTranslator().translate("executing StatementCallback", null, ex);
    		finally {
    			DataSourceUtils.closeConnectionIfNecessary(con, getDataSource());

    Can you guys give me some test results about this. And did you guys come with the same problems like our test engineer? Thanks.

  • #2
    The engineer told us that the return clause can not be written in try {} block in high volume condition, otherwise this style code may incur problems too!
    He suggests that the code should look as following:

    //close your resource
    }catch(Exception e){
    //close your resource
    return result;


    • #3
      What exactly are high volume conditions?

      And, more important, is this proposition somehow backed up (e.g. in the VM or language specification)?

      If not, I would not spend too much trust on such assertions. Even when they come from an engineer from Sun.

      Just my 2 cents,


      • #4
        I think the Sun engineer should read the language spec. It's very clear on this, and I've never heard anything of this nature. Additionally, a lot of code out there right now would be broken if this was the case...