Results 1 to 4 of 4

Thread: About release connection in finally block

  1. #1
    Join Date
    Aug 2004
    Posts
    19

    Default 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
    Code:
    	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();
    			throwExceptionOnWarningIfNotIgnoringWarnings(warning);
    			return result;
    		}
    		catch (SQLException ex) {
    			throw getExceptionTranslator().translate("executing StatementCallback", null, ex);
    		}
    		finally {
    			JdbcUtils.closeStatement(stmt);
    			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.
    Regards
    Eisen

  2. #2
    Join Date
    Aug 2004
    Posts
    19

    Default

    BTW:
    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:

    Code:
    try{
    //close your resource
    }catch(Exception e){
    //close your resource
    }finally{
    }
    
    return result;

  3. #3
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    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,
    Andreas

  4. #4
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    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...
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

Similar Threads

  1. Connection closed after transaction commit
    By alirussi in forum Data
    Replies: 4
    Last Post: Dec 17th, 2011, 06:41 AM
  2. Replies: 6
    Last Post: May 25th, 2005, 01:56 AM
  3. Replies: 4
    Last Post: May 20th, 2005, 03:20 AM
  4. Replies: 0
    Last Post: Apr 6th, 2005, 08:24 AM
  5. Acegi Security release 0.7.0 is out
    By Ben Alex in forum Announcements
    Replies: 0
    Last Post: Jan 19th, 2005, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •