Announcement Announcement Module
Collapse
No announcement yet.
iBatis & AbstractTransactionalDataSourceSpringContextTests Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • iBatis & AbstractTransactionalDataSourceSpringContextTests

    I am trying to use AbstractTransactionalDataSourceSpringContextTests to test some Dao's written using iBatis. After running the tests I expect any transactions to be rolled back so that the database is untouched. This does not seem to be the case.

    I assume it has something to do with my configuration. Any suggestions on what I am doing wrong would be helpful.

    application-context.xml
    Code:
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="url" value="jdbc:mysql://localhost:3306/horizon"/>
            <property name="username" value="root"/>
            <property name="password" value="hipm3fvn"/>
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    
            <property name="testWhileIdle" value="true"/>
            <property name="validationQuery" value="select 1"/>
    
            <property name="logAbandoned" value="true"/>
            <property name="removeAbandoned" value="true"/>
            <property name="removeAbandonedTimeout" value="3600"/>
    
            <property name="maxWait" value="20"/>
            <property name="maxIdle" value="10"/>
            <property name="maxActive" value="-1"/>
        </bean>
    
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
            <property name="configLocation" value="SqlMapConfig.xml"/>
            <property name="useTransactionAwareDataSource" value="true"/>
            <property name="transactionConfigClass" value="com.ibatis.sqlmap.engine.transaction.jdbc.JdbcTransactionConfig"/>
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <bean id="functionDao" name="functionDao" class="com.hbcbs.hrm.nasco.domain.dao.ibatis.FunctionDaoImpl">
            <property name="sqlMapClient" ref="sqlMapClient"/>
        </bean>
    SqlMapConfig.xml
    Code:
    <sqlMapConfig>
        <settings cacheModelsEnabled="true" enhancementEnabled="true"
            lazyLoadingEnabled="true" maxRequests="32" maxSessions="10" maxTransactions="5"
            useStatementNamespaces="true" />
    
        <sqlMap resource="com/hbcbs/hrm/nasco/domain/dao/ibatis/maps/Mnemonic.xml" />
        <sqlMap resource="com/hbcbs/hrm/nasco/domain/dao/ibatis/maps/Unit.xml" />
        <sqlMap resource="com/hbcbs/hrm/nasco/domain/dao/ibatis/maps/Function.xml" />
    </sqlMapConfig>
    Base abstract class:
    Code:
    public abstract class AbstractDataAccessTest extends AbstractTransactionalDataSourceSpringContextTests {
    
        /**
         * Reference the Spring configuration file for the test case.
         */
        protected String[] getConfigLocations() {
            this.setAutowireMode(this.AUTOWIRE_BY_TYPE);
            return new String[]{"classpath:applicationContext.xml"};
        }
    }
    Test Class:
    Code:
    public class FunctionDaoImplTest extends AbstractDataAccessTest {
    
        private FunctionDao functionDao;
        
        public FunctionDaoImplTest() {
        }
    
        public void setFunctionDao( FunctionDao functionDao ) {
            this.functionDao = functionDao;
        }
        
        @BeforeClass
        public static void setUpClass() throws Exception {
        }
    
        @AfterClass
        public static void tearDownClass() throws Exception {
        }
    
        /**
         * Test of insertFunction method, of class FunctionDaoImpl.
         */
        @Test
        public void testInsertFunction() {
            System.out.println("insertFunction");
    
            Function f = new Function();
            f.setFunctionName("Test Function");
            f.setFunctionRecord("INQ");
            f.setRow(0);
            f.setRow(1);
    
            functionDao.insertFunction( f );
            
            Function f1 = functionDao.selectFunction(new Long(1));
    
            functionDao.deleteFunction(f1.getFunctionId());
            
            assert( f1.getFunctionName().equals( "Test Function"));
        }
    }
    Thanks,
    Pete

  • #2
    1) You are using a JUnit3 base class in a JUnit 4 testcase, fix that first
    2) You are using MySQL make sure you use InnoDB tables not MyISAM the latter aren't transactional and hence rollingback doesn't have any effect.

    Comment

    Working...
    X