Author: cbegin
Date: Mon Sep  7 08:04:57 2009
New Revision: 812028

URL: http://svn.apache.org/viewvc?rev=812028&view=rev
Log:
re-implemented discriminators

Modified:
    
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/NewResultSetHandler.java
    
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/test/java/org/apache/ibatis/executor/BaseExecutorTest.java

Modified: 
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/NewResultSetHandler.java
URL: 
http://svn.apache.org/viewvc/ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/NewResultSetHandler.java?rev=812028&r1=812027&r2=812028&view=diff
==============================================================================
--- 
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/NewResultSetHandler.java
 (original)
+++ 
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/executor/resultset/NewResultSetHandler.java
 Mon Sep  7 08:04:57 2009
@@ -96,12 +96,15 @@
 
   private void handleResultSet(ResultSet rs, ResultMap resultMap, 
ResultHandler resultHandler, RowLimit rowLimit) throws SQLException {
     final ResultContext resultContext = new ResultContext();
-    final List<String> unmappedColumnNames = getUnmappedColumnNames(rs, 
resultMap);
+    final List<String> mappedColumnNames = new ArrayList<String>();
+    final List<String> unmappedColumnNames = new ArrayList<String>();
     skipRows(rs, rowLimit);
     while (shouldProcessMoreRows(rs, resultContext.getResultCount(), 
rowLimit)) {
-      final Object resultObject = createResultObject(rs, resultMap);
+      final ResultMap discriminatedResultMap = 
resolveDiscriminatedResultMap(rs, resultMap);
+      final Object resultObject = createResultObject(rs, 
discriminatedResultMap );
       final MetaObject metaObject = MetaObject.forObject(resultObject);
-      applyPropertyMappings(rs, resultMap, metaObject);
+      getMappedAndUnmappedColumnNames(rs, discriminatedResultMap, 
mappedColumnNames, unmappedColumnNames);
+      applyPropertyMappings(rs, discriminatedResultMap , mappedColumnNames, 
metaObject);
       applyAutomaticMappings(rs, unmappedColumnNames, metaObject);
       resultContext.nextResultObject(resultObject);
       resultHandler.handleResult(resultContext);
@@ -120,15 +123,17 @@
     }
   }
 
-  private void applyPropertyMappings(ResultSet rs, ResultMap resultMap, 
MetaObject metaObject) throws SQLException {
+  private void applyPropertyMappings(ResultSet rs, ResultMap resultMap, 
List<String> mappedColumnNames, MetaObject metaObject) throws SQLException {
     final List<ResultMapping> propertyMappings = 
resultMap.getPropertyResultMappings();
     for (ResultMapping propertyMapping : propertyMappings) {
       final TypeHandler typeHandler = propertyMapping.getTypeHandler();
       if (typeHandler != null) {
         final String property = propertyMapping.getProperty();
         final String column = propertyMapping.getColumn();
-        final Object value = typeHandler.getResult(rs, column);
-        metaObject.setValue(property, value);
+        if (mappedColumnNames.contains(column.toUpperCase())) {
+          final Object value = typeHandler.getResult(rs, column);
+          metaObject.setValue(property, value);
+        }
       }
     }
   }
@@ -147,19 +152,21 @@
     }
   }
 
-  private List<String> getUnmappedColumnNames(ResultSet rs, ResultMap 
resultMap) throws SQLException {
+  private void getMappedAndUnmappedColumnNames(ResultSet rs, ResultMap 
resultMap, List<String> mappedColumnNames, List<String> unmappedColumnNames) 
throws SQLException {
+    mappedColumnNames.clear();
+    unmappedColumnNames.clear();
     final ResultSetMetaData rsmd = rs.getMetaData();
     final int columnCount = rsmd.getColumnCount();
-    final List<String> columnNames = new ArrayList<String>();
     final Set<String> mappedColumns = resultMap.getMappedColumns();
     for(int i=1; i<=columnCount; i++) {
       final String columnName = configuration.isUseColumnLabel() ? 
rsmd.getColumnLabel(i) : rsmd.getColumnName(i);
       final String upperColumnName = columnName.toUpperCase();
-      if (!mappedColumns.contains(upperColumnName)) {
-        columnNames.add(columnName);
+      if (mappedColumns.contains(upperColumnName)) {
+        mappedColumnNames.add(columnName);
+      } else {
+        unmappedColumnNames.add(columnName);
       }
     }
-    return columnNames;
   }
 
   private Object createResultObject(ResultSet rs, ResultMap resultMap) throws 
SQLException {
@@ -188,6 +195,28 @@
     }
   }
 
+   public ResultMap resolveDiscriminatedResultMap(ResultSet rs, ResultMap 
resultMap) throws SQLException {
+    final Discriminator discriminator = resultMap.getDiscriminator();
+    if (discriminator != null) {
+      final Object value = getDiscriminatorValue(rs, discriminator);
+      final String discriminatedMapId = 
discriminator.getMapIdFor(String.valueOf(value));
+      if (configuration.hasResultMap(discriminatedMapId)) {
+        return configuration.getResultMap(discriminatedMapId);
+      }
+    }
+    return resultMap;
+  }
+
+  private Object getDiscriminatorValue(ResultSet rs, Discriminator 
discriminator) throws SQLException {
+    final ResultMapping resultMapping = discriminator.getResultMapping();
+    final TypeHandler typeHandler = resultMapping.getTypeHandler();
+    if (typeHandler != null) {
+      return typeHandler.getResult(rs, resultMapping.getColumn());
+    } else {
+      throw new ExecutorException("No type handler could be found to map the 
property '" + resultMapping.getProperty() + "' to the column '" + 
resultMapping.getColumn() + "'.  One or both of the types, or the combination 
of types is not supported.");
+    }
+  }
+
   private ResultSet getNextResultSet(Statement stmt) throws SQLException {
     // Making this method tolerant of bad JDBC drivers
     try {

Modified: 
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/test/java/org/apache/ibatis/executor/BaseExecutorTest.java
URL: 
http://svn.apache.org/viewvc/ibatis/java/ibatis-3/trunk/ibatis-3-core/src/test/java/org/apache/ibatis/executor/BaseExecutorTest.java?rev=812028&r1=812027&r2=812028&view=diff
==============================================================================
--- 
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/test/java/org/apache/ibatis/executor/BaseExecutorTest.java
 (original)
+++ 
ibatis/java/ibatis-3/trunk/ibatis-3-core/src/test/java/org/apache/ibatis/executor/BaseExecutorTest.java
 Mon Sep  7 08:04:57 2009
@@ -7,6 +7,7 @@
 import org.apache.ibatis.transaction.jdbc.JdbcTransaction;
 import static org.junit.Assert.*;
 import org.junit.Test;
+import org.junit.Ignore;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
@@ -29,57 +30,71 @@
   public void shouldInsertNewAuthor() throws Exception {
     DataSource ds = createBlogDataSource();
     Connection connection = ds.getConnection();
-    Executor executor = createExecutor(new JdbcTransaction(connection, false));
-    Author author = new Author(99, "someone", "******", "[email protected]", 
null, Section.NEWS);
-    MappedStatement insertStatement = 
ExecutorTestHelper.prepareInsertAuthorMappedStatement(config);
-    MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config);
-    int rows = executor.update(insertStatement, author);
-    List<Author> authors = executor.query(selectStatement, 99, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
-    executor.flushStatements();
-    executor.rollback(true);
-    assertEquals(1, authors.size());
-    assertEquals(author.toString(), authors.get(0).toString());
-    assertTrue(1 == rows || BatchExecutor.BATCH_UPDATE_RETURN_VALUE == rows);
+    try {
+      Executor executor = createExecutor(new JdbcTransaction(connection, 
false));
+      Author author = new Author(99, "someone", "******", 
"[email protected]", null, Section.NEWS);
+      MappedStatement insertStatement = 
ExecutorTestHelper.prepareInsertAuthorMappedStatement(config);
+      MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config);
+      int rows = executor.update(insertStatement, author);
+      List<Author> authors = executor.query(selectStatement, 99, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
+      executor.flushStatements();
+      executor.rollback(true);
+      assertEquals(1, authors.size());
+      assertEquals(author.toString(), authors.get(0).toString());
+      assertTrue(1 == rows || BatchExecutor.BATCH_UPDATE_RETURN_VALUE == rows);
+    } finally {
+      connection.close();
+    }
   }
 
   @Test
   public void shouldSelectAllAuthorsAutoMapped() throws Exception {
     DataSource ds = createBlogDataSource();
     Connection connection = ds.getConnection();
-    Executor executor = createExecutor(new JdbcTransaction(connection, false));
-    MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectAllAuthorsAutoMappedStatement(config);
-    List<Author> authors = executor.query(selectStatement, null, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
-    assertEquals(2, authors.size());
-    Author author = authors.get(0);
-    // id,username, password, email, bio, favourite_section
-    // (101,'jim','********','[email protected]','','NEWS');
-    assertEquals(101, author.getId());
-    assertEquals("jim", author.getUsername());
-    assertEquals("[email protected]", author.getEmail());
-    assertEquals("", author.getBio());
-    assertEquals(Section.NEWS, author.getFavouriteSection());
+    try {
+      Executor executor = createExecutor(new JdbcTransaction(connection, 
false));
+      MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectAllAuthorsAutoMappedStatement(config);
+      List<Author> authors = executor.query(selectStatement, null, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
+      assertEquals(2, authors.size());
+      Author author = authors.get(0);
+      // id,username, password, email, bio, favourite_section
+      // (101,'jim','********','[email protected]','','NEWS');
+      assertEquals(101, author.getId());
+      assertEquals("jim", author.getUsername());
+      assertEquals("[email protected]", author.getEmail());
+      assertEquals("", author.getBio());
+      assertEquals(Section.NEWS, author.getFavouriteSection());
+    } finally {
+      connection.rollback();
+      connection.close();
+    }
   }
 
   @Test
   public void shouldInsertNewAuthorWithAutoKey() throws Exception {
     DataSource ds = createBlogDataSource();
     Connection connection = ds.getConnection();
-    Executor executor = createExecutor(new JdbcTransaction(connection, false));
-    Author author = new Author(-1, "someone", "******", "[email protected]", 
null, Section.NEWS);
-    MappedStatement insertStatement = 
ExecutorTestHelper.prepareInsertAuthorMappedStatementWithAutoKey(config);
-    MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config);
-    int rows = executor.update(insertStatement, author);
-    assertTrue(rows > 0 || rows == BatchExecutor.BATCH_UPDATE_RETURN_VALUE);
-    if (rows == BatchExecutor.BATCH_UPDATE_RETURN_VALUE) {
-      executor.flushStatements();
-    }
-    assertTrue(-1 != author.getId());
-    if (author.getId() != BatchExecutor.BATCH_UPDATE_RETURN_VALUE) {
-      List<Author> authors = executor.query(selectStatement, author.getId(), 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
-      executor.rollback(true);
-      assertEquals(1, authors.size());
-      assertEquals(author.toString(), authors.get(0).toString());
-      assertTrue(author.getId() >= 10000);
+    try {
+      Executor executor = createExecutor(new JdbcTransaction(connection, 
false));
+      Author author = new Author(-1, "someone", "******", 
"[email protected]", null, Section.NEWS);
+      MappedStatement insertStatement = 
ExecutorTestHelper.prepareInsertAuthorMappedStatementWithAutoKey(config);
+      MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config);
+      int rows = executor.update(insertStatement, author);
+      assertTrue(rows > 0 || rows == BatchExecutor.BATCH_UPDATE_RETURN_VALUE);
+      if (rows == BatchExecutor.BATCH_UPDATE_RETURN_VALUE) {
+        executor.flushStatements();
+      }
+      assertTrue(-1 != author.getId());
+      if (author.getId() != BatchExecutor.BATCH_UPDATE_RETURN_VALUE) {
+        List<Author> authors = executor.query(selectStatement, author.getId(), 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
+        executor.rollback(true);
+        assertEquals(1, authors.size());
+        assertEquals(author.toString(), authors.get(0).toString());
+        assertTrue(author.getId() >= 10000);
+      }
+    } finally {
+      connection.rollback();
+      connection.close();
     }
   }
 
@@ -87,83 +102,108 @@
   public void shouldInsertNewAuthorByProc() throws Exception {
     DataSource ds = createBlogDataSource();
     Connection connection = ds.getConnection();
-    Executor executor = createExecutor(new JdbcTransaction(connection, false));
-    Author author = new Author(97, "someone", "******", "[email protected]", 
null, null);
-    MappedStatement insertStatement = 
ExecutorTestHelper.prepareInsertAuthorProc(config);
-    MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config);
-    int rows = executor.update(insertStatement, author);
-    List<Author> authors = executor.query(selectStatement, 97, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
-    executor.flushStatements();
-    executor.rollback(true);
-    assertEquals(1, authors.size());
-    assertEquals(author.toString(), authors.get(0).toString());
+    try {
+      Executor executor = createExecutor(new JdbcTransaction(connection, 
false));
+      Author author = new Author(97, "someone", "******", 
"[email protected]", null, null);
+      MappedStatement insertStatement = 
ExecutorTestHelper.prepareInsertAuthorProc(config);
+      MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config);
+      int rows = executor.update(insertStatement, author);
+      List<Author> authors = executor.query(selectStatement, 97, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
+      executor.flushStatements();
+      executor.rollback(true);
+      assertEquals(1, authors.size());
+      assertEquals(author.toString(), authors.get(0).toString());
+    } finally {
+      connection.rollback();
+      connection.close();
+    }
   }
 
   @Test
   public void shouldInsertNewAuthorUsingSimpleNonPreparedStatements() throws 
Exception {
     DataSource ds = createBlogDataSource();
     Connection connection = ds.getConnection();
-    Executor executor = createExecutor(new JdbcTransaction(connection, false));
-    Author author = new Author(99, "someone", "******", "[email protected]", 
null, null);
-    MappedStatement insertStatement = 
ExecutorTestHelper.createInsertAuthorWithIDof99MappedStatement(config);
-    MappedStatement selectStatement = 
ExecutorTestHelper.createSelectAuthorWithIDof99MappedStatement(config);
-    int rows = executor.update(insertStatement, null);
-    List<Author> authors = executor.query(selectStatement, 99, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
-    executor.flushStatements();
-    executor.rollback(true);
-    assertEquals(1, authors.size());
-    assertEquals(author.toString(), authors.get(0).toString());
-    assertTrue(1 == rows || BatchExecutor.BATCH_UPDATE_RETURN_VALUE == rows);
+    try {
+      Executor executor = createExecutor(new JdbcTransaction(connection, 
false));
+      Author author = new Author(99, "someone", "******", 
"[email protected]", null, null);
+      MappedStatement insertStatement = 
ExecutorTestHelper.createInsertAuthorWithIDof99MappedStatement(config);
+      MappedStatement selectStatement = 
ExecutorTestHelper.createSelectAuthorWithIDof99MappedStatement(config);
+      int rows = executor.update(insertStatement, null);
+      List<Author> authors = executor.query(selectStatement, 99, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
+      executor.flushStatements();
+      executor.rollback(true);
+      assertEquals(1, authors.size());
+      assertEquals(author.toString(), authors.get(0).toString());
+      assertTrue(1 == rows || BatchExecutor.BATCH_UPDATE_RETURN_VALUE == rows);
+    } finally {
+      connection.rollback();
+      connection.close();
+    }
   }
 
   @Test
   public void shouldUpdateAuthor() throws Exception {
     DataSource ds = createBlogDataSource();
     Connection connection = ds.getConnection();
-    Executor executor = createExecutor(new JdbcTransaction(connection, false));
-    Author author = new Author(101, "someone", "******", "[email protected]", 
null, Section.NEWS);
-    MappedStatement updateStatement = 
ExecutorTestHelper.prepareUpdateAuthorMappedStatement(config);
-    MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config);
-    int rows = executor.update(updateStatement, author);
-    List<Author> authors = executor.query(selectStatement, 101, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
-    executor.flushStatements();
-    executor.rollback(true);
-    assertEquals(1, authors.size());
-    assertEquals(author.toString(), authors.get(0).toString());
-    assertTrue(1 == rows || BatchExecutor.BATCH_UPDATE_RETURN_VALUE == rows);
+    try {
+      Executor executor = createExecutor(new JdbcTransaction(connection, 
false));
+      Author author = new Author(101, "someone", "******", 
"[email protected]", null, Section.NEWS);
+      MappedStatement updateStatement = 
ExecutorTestHelper.prepareUpdateAuthorMappedStatement(config);
+      MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config);
+      int rows = executor.update(updateStatement, author);
+      List<Author> authors = executor.query(selectStatement, 101, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
+      executor.flushStatements();
+      executor.rollback(true);
+      assertEquals(1, authors.size());
+      assertEquals(author.toString(), authors.get(0).toString());
+      assertTrue(1 == rows || BatchExecutor.BATCH_UPDATE_RETURN_VALUE == rows);
+    } finally {
+      connection.rollback();
+      connection.close();
+    }
   }
 
   @Test
   public void shouldDeleteAuthor() throws Exception {
     DataSource ds = createBlogDataSource();
     Connection connection = ds.getConnection();
-    Executor executor = createExecutor(new JdbcTransaction(connection, false));
-    Author author = new Author(101, null, null, null, null, null);
-    MappedStatement deleteStatement = 
ExecutorTestHelper.prepareDeleteAuthorMappedStatement(config);
-    MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config);
-    int rows = executor.update(deleteStatement, author);
-    List<Author> authors = executor.query(selectStatement, 101, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
-    executor.flushStatements();
-    executor.rollback(true);
-    assertEquals(0, authors.size());
-    assertTrue(1 == rows || BatchExecutor.BATCH_UPDATE_RETURN_VALUE == rows);
+    try {
+      Executor executor = createExecutor(new JdbcTransaction(connection, 
false));
+      Author author = new Author(101, null, null, null, null, null);
+      MappedStatement deleteStatement = 
ExecutorTestHelper.prepareDeleteAuthorMappedStatement(config);
+      MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config);
+      int rows = executor.update(deleteStatement, author);
+      List<Author> authors = executor.query(selectStatement, 101, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
+      executor.flushStatements();
+      executor.rollback(true);
+      assertEquals(0, authors.size());
+      assertTrue(1 == rows || BatchExecutor.BATCH_UPDATE_RETURN_VALUE == rows);
+    } finally {
+      connection.rollback();
+      connection.close();
+    }
   }
 
   @Test
   public void shouldSelectDiscriminatedProduct() throws Exception {
     DataSource ds = createJPetstoreDataSource();
     Connection connection = ds.getConnection();
-    Executor executor = createExecutor(new JdbcTransaction(connection, false));
-    MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectDiscriminatedProduct(config);
-    List<Map> products = executor.query(selectStatement, null, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
-    connection.rollback();
-    assertEquals(16, products.size());
-    for (Map m : products) {
-      if ("REPTILES".equals(m.get("category"))) {
-        assertNull(m.get("name"));
-      } else {
-        assertNotNull(m.get("name"));
+    try {
+      Executor executor = createExecutor(new JdbcTransaction(connection, 
false));
+      MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectDiscriminatedProduct(config);
+      List<Map> products = executor.query(selectStatement, null, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
+      connection.rollback();
+      assertEquals(16, products.size());
+      for (Map m : products) {
+        if ("REPTILES".equals(m.get("category"))) {
+          assertNull(m.get("name"));
+        } else {
+          assertNotNull(m.get("name"));
+        }
       }
+    } finally {
+      connection.rollback();
+      connection.close();
     }
   }
 
@@ -171,40 +211,51 @@
   public void shouldSelect10DiscriminatedProducts() throws Exception {
     DataSource ds = createJPetstoreDataSource();
     Connection connection = ds.getConnection();
-    Executor executor = createExecutor(new JdbcTransaction(connection, false));
-    MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectDiscriminatedProduct(config);
-    List<Map> products = executor.query(selectStatement, null, 4, 10, 
Executor.NO_RESULT_HANDLER);
-    connection.rollback();
-    assertEquals(10, products.size());
-    for (Map m : products) {
-      if ("REPTILES".equals(m.get("category"))) {
-        assertNull(m.get("name"));
-      } else {
-        assertNotNull(m.get("name"));
+    try {
+      Executor executor = createExecutor(new JdbcTransaction(connection, 
false));
+      MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectDiscriminatedProduct(config);
+      List<Map> products = executor.query(selectStatement, null, 4, 10, 
Executor.NO_RESULT_HANDLER);
+      connection.rollback();
+      assertEquals(10, products.size());
+      for (Map m : products) {
+        if ("REPTILES".equals(m.get("category"))) {
+          assertNull(m.get("name"));
+        } else {
+          assertNotNull(m.get("name"));
+        }
       }
+    } finally {
+      connection.rollback();
+      connection.close();
     }
+
   }
 
   @Test
   public void shouldSelectTwoSetsOfAuthorsViaProc() throws Exception {
     DataSource ds = createBlogDataSource();
     Connection connection = ds.getConnection();
-    connection.setAutoCommit(false);
-    Executor executor = createExecutor(new JdbcTransaction(connection, false));
-    MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectTwoSetsOfAuthorsProc(config);
-    List<List> authorSets = executor.query(selectStatement, new HashMap() {
-      {
-        put("id1", 101);
-        put("id2", 102);
-      }
-    }, Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, 
Executor.NO_RESULT_HANDLER);
-    connection.rollback();
-    assertEquals(2, authorSets.size());
-    for (List authors : authorSets) {
-      assertEquals(2, authors.size());
-      for (Object author : authors) {
-        assertTrue(author instanceof Author);
+    try {
+      connection.setAutoCommit(false);
+      Executor executor = createExecutor(new JdbcTransaction(connection, 
false));
+      MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectTwoSetsOfAuthorsProc(config);
+      List<List> authorSets = executor.query(selectStatement, new HashMap() {
+        {
+          put("id1", 101);
+          put("id2", 102);
+        }
+      }, Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, 
Executor.NO_RESULT_HANDLER);
+      connection.rollback();
+      assertEquals(2, authorSets.size());
+      for (List authors : authorSets) {
+        assertEquals(2, authors.size());
+        for (Object author : authors) {
+          assertTrue(author instanceof Author);
+        }
       }
+    } finally {
+      connection.rollback();
+      connection.close();
     }
   }
 
@@ -212,69 +263,89 @@
   public void shouldSelectAuthorViaOutParams() throws Exception {
     DataSource ds = createBlogDataSource();
     Connection connection = ds.getConnection();
-    connection.setAutoCommit(false);
-    Executor executor = createExecutor(new JdbcTransaction(connection, false));
-    MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectAuthorViaOutParams(config);
-    Author author = new Author(102, null, null, null, null, null);
-    executor.query(selectStatement, author, Executor.NO_ROW_OFFSET, 
Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
-    connection.rollback();
-
-    assertEquals("sally", author.getUsername());
-    assertEquals("********", author.getPassword());
-    assertEquals("[email protected]", author.getEmail());
-    assertEquals(null, author.getBio());
+    try {
+      connection.setAutoCommit(false);
+      Executor executor = createExecutor(new JdbcTransaction(connection, 
false));
+      MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectAuthorViaOutParams(config);
+      Author author = new Author(102, null, null, null, null, null);
+      executor.query(selectStatement, author, Executor.NO_ROW_OFFSET, 
Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
+      connection.rollback();
+
+      assertEquals("sally", author.getUsername());
+      assertEquals("********", author.getPassword());
+      assertEquals("[email protected]", author.getEmail());
+      assertEquals(null, author.getBio());
+    } finally {
+      connection.rollback();
+      connection.close();
+    }
   }
 
   @Test
   public void shouldFetchPostsForBlog() throws Exception {
     DataSource ds = createBlogDataSource();
     Connection connection = ds.getConnection();
-    Executor executor = createExecutor(new JdbcTransaction(connection, false));
-    MappedStatement selectBlog = 
ExecutorTestHelper.prepareComplexSelectBlogMappedStatement(config);
-    MappedStatement selectPosts = 
ExecutorTestHelper.prepareSelectPostsForBlogMappedStatement(config);
-    config.addMappedStatement(selectBlog);
-    config.addMappedStatement(selectPosts);
-    List<Post> posts = executor.query(selectPosts, 1, Executor.NO_ROW_OFFSET, 
Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
-    executor.flushStatements();
-    assertEquals(2, posts.size());
-    assertNotNull(posts.get(1).getBlog());
-    assertEquals(1, posts.get(1).getBlog().getId());
-    executor.rollback(true);
+    try {
+      Executor executor = createExecutor(new JdbcTransaction(connection, 
false));
+      MappedStatement selectBlog = 
ExecutorTestHelper.prepareComplexSelectBlogMappedStatement(config);
+      MappedStatement selectPosts = 
ExecutorTestHelper.prepareSelectPostsForBlogMappedStatement(config);
+      config.addMappedStatement(selectBlog);
+      config.addMappedStatement(selectPosts);
+      List<Post> posts = executor.query(selectPosts, 1, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
+      executor.flushStatements();
+      assertEquals(2, posts.size());
+      assertNotNull(posts.get(1).getBlog());
+      assertEquals(1, posts.get(1).getBlog().getId());
+      executor.rollback(true);
+    } finally {
+      connection.rollback();
+      connection.close();
+    }
   }
 
   @Test
   public void shouldFetchOneOrphanedPostWithNoBlog() throws Exception {
     DataSource ds = createBlogDataSource();
     Connection connection = ds.getConnection();
-    Executor executor = createExecutor(new JdbcTransaction(connection, false));
-    MappedStatement selectBlog = 
ExecutorTestHelper.prepareComplexSelectBlogMappedStatement(config);
-    MappedStatement selectPost = 
ExecutorTestHelper.prepareSelectPostMappedStatement(config);
-    config.addMappedStatement(selectBlog);
-    config.addMappedStatement(selectPost);
-    List<Post> posts = executor.query(selectPost, 5, Executor.NO_ROW_OFFSET, 
Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
-    executor.flushStatements();
-    executor.rollback(true);
-    assertEquals(1, posts.size());
-    Post post = posts.get(0);
-    assertNull(post.getBlog());
+    try {
+      Executor executor = createExecutor(new JdbcTransaction(connection, 
false));
+      MappedStatement selectBlog = 
ExecutorTestHelper.prepareComplexSelectBlogMappedStatement(config);
+      MappedStatement selectPost = 
ExecutorTestHelper.prepareSelectPostMappedStatement(config);
+      config.addMappedStatement(selectBlog);
+      config.addMappedStatement(selectPost);
+      List<Post> posts = executor.query(selectPost, 5, Executor.NO_ROW_OFFSET, 
Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
+      executor.flushStatements();
+      executor.rollback(true);
+      assertEquals(1, posts.size());
+      Post post = posts.get(0);
+      assertNull(post.getBlog());
+    } finally {
+      connection.rollback();
+      connection.close();
+    }
   }
 
   @Test
   public void shouldFetchPostWithBlogWithCompositeKey() throws Exception {
     DataSource ds = createBlogDataSource();
     Connection connection = ds.getConnection();
-    Executor executor = createExecutor(new JdbcTransaction(connection, false));
-    MappedStatement selectBlog = 
ExecutorTestHelper.prepareSelectBlogByIdAndAuthor(config);
-    MappedStatement selectPost = 
ExecutorTestHelper.prepareSelectPostWithBlogByAuthorMappedStatement(config);
-    config.addMappedStatement(selectBlog);
-    config.addMappedStatement(selectPost);
-    List<Post> posts = executor.query(selectPost, 2, Executor.NO_ROW_OFFSET, 
Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
-    executor.flushStatements();
-    assertEquals(1, posts.size());
-    Post post = posts.get(0);
-    assertNotNull(post.getBlog());
-    assertEquals(101, post.getBlog().getAuthor().getId());
-    executor.rollback(true);
+    try {
+      Executor executor = createExecutor(new JdbcTransaction(connection, 
false));
+      MappedStatement selectBlog = 
ExecutorTestHelper.prepareSelectBlogByIdAndAuthor(config);
+      MappedStatement selectPost = 
ExecutorTestHelper.prepareSelectPostWithBlogByAuthorMappedStatement(config);
+      config.addMappedStatement(selectBlog);
+      config.addMappedStatement(selectPost);
+      List<Post> posts = executor.query(selectPost, 2, Executor.NO_ROW_OFFSET, 
Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
+      executor.flushStatements();
+      assertEquals(1, posts.size());
+      Post post = posts.get(0);
+      assertNotNull(post.getBlog());
+      assertEquals(101, post.getBlog().getAuthor().getId());
+      executor.rollback(true);
+    } finally {
+      connection.rollback();
+      connection.close();
+    }
   }
 
 
@@ -297,6 +368,8 @@
       executor.rollback(true);
     } finally {
       config.setLazyLoadingEnabled(true);
+      connection.rollback();
+      connection.close();
     }
   }
 
@@ -304,15 +377,20 @@
   public void shouldMapConstructorResults() throws Exception {
     DataSource ds = createBlogDataSource();
     Connection connection = ds.getConnection();
-    Executor executor = createExecutor(new JdbcTransaction(connection, false));
-    MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectOneAuthorMappedStatementWithConstructorResults(config);
-    List<Author> authors = executor.query(selectStatement, 102, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
-    executor.flushStatements();
-    executor.rollback(true);
-    assertEquals(1, authors.size());
+    try {
+      Executor executor = createExecutor(new JdbcTransaction(connection, 
false));
+      MappedStatement selectStatement = 
ExecutorTestHelper.prepareSelectOneAuthorMappedStatementWithConstructorResults(config);
+      List<Author> authors = executor.query(selectStatement, 102, 
Executor.NO_ROW_OFFSET, Executor.NO_ROW_LIMIT, Executor.NO_RESULT_HANDLER);
+      executor.flushStatements();
+      executor.rollback(true);
+      assertEquals(1, authors.size());
 
-    Author author = authors.get(0);
-    assertEquals(102, author.getId());
+      Author author = authors.get(0);
+      assertEquals(102, author.getId());
+    } finally {
+      connection.rollback();
+      connection.close();
+    }
   }
 
   protected abstract Executor createExecutor(Transaction transaction);


Reply via email to