Announcement Announcement Module
Collapse
No announcement yet.
Question about example code Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Question about example code

    I have a question about some of the sample code contained in Professional Java Development with the Spring Framework.
    It's the MinimalTest program under the heading A Simple Example in the section Introducing Spring's JDBC Framework in chapter 5:


    Code:
    import junit.framework.TestCase;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    public class MinimalTest extends TestCase {
    private DriverManagerDataSource dataSource;
    public void setUp() {
    dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName( "org.hsqldb.jdbcDriver");
    dataSource.setUrl( "jdbc:hsqldb:hsql://localhost:");
    dataSource.setUsername( "sa");
    dataSource.setPassword( "");
    JdbcTemplate jt = new JdbcTemplate(dataSource);
    jt.execute("delete from mytable");
    jt.execute("insert into mytable (id, name) values(1, 'John’)");
    jt.execute("insert into mytable (id, name) values(2, 'Jane’)");
    }
    public void testSomething() {
    // the actual test code goes here
    }
    }
    How is the connection closed in this code? From reading the chapter I think that the DriverManagerDataSource variable dataSource automatically closes the connection when it is finished executing the statement but I am not sure. Thank you for any replies.

  • #2
    I would have a look at JdbcTemplate.execute(PreparedStatementCreator, PreparedStatementCallback), this should make it more clear. Basically as you aren't running in a transaction then the connection will be closed for you by DataSourceUtils.releaseConnection(..).

    DataSourceUtils
    Code:
    	public static void doReleaseConnection(Connection con, DataSource dataSource) throws SQLException {
    		if (con == null) {
    			return;
    		}
    
    		if (dataSource != null) {
    			ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
    			if (conHolder != null && conHolder.hasConnection() && connectionEquals(conHolder.getConnection(), con)) {
    				// It's the transactional Connection: Don't close it.
    				conHolder.released();
    				return;
    			}
    		}
    
    		// Leave the Connection open only if the DataSource is our
    		// special data source, and it wants the Connection left open.
    		if (!(dataSource instanceof SmartDataSource) || ((SmartDataSource) dataSource).shouldClose(con)) {
    			logger.debug("Returning JDBC Connection to DataSource");
    			con.close();
    		}
    	}

    Comment


    • #3
      But that releaseConnection method is not being called anywhere?

      Comment


      • #4
        It's called at the end of the JdbcTemplate.execute(..) method. If you have a look at the code, it's quite easy to understand.
        Code:
        		finally {
        			if (psc instanceof ParameterDisposer) {
        				((ParameterDisposer) psc).cleanupParameters();
        			}
        			JdbcUtils.closeStatement(ps);
        			DataSourceUtils.releaseConnection(con, getDataSource());
        		}

        Comment


        • #5
          Thanks, I see now.
          I can almost feel your rueful shake of the head from here!

          Comment


          • #6
            Hey there's no rueful shake of the head from me . I'm happy to answer any questions and I can help people with, I'm just glad that it all made sense to you!

            Comment


            • #7
              It did, thanks!

              Comment

              Working...
              X