Question

I am trying to execute a SQL script in my JUnit test using Spring. The script is being used to set up data for the tests. However, when the script is run, the INSERTs in the script are being committed after each test. Spring documentation says not to expect rollback with DDL, but everything in my script is DML. All it contains is INSERT statements and obtaining the last insert ID (SET @blah = LAST_INSERT_ID()).

Am I configuring something wrong? I am using this against a MySQL database. Our configuration is as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext.xml" })
@TransactionConfiguration(defaultRollback = true)
public class OrderTestCase extends AbstractTransactionalJUnit4SpringContextTests {

    @Before
    public void runSql() {
        String fileName = StringUtils.replace(getClass().getName(), ".", "/") + ".sql";
        Resource resource = applicationContext.getResource(fileName);
        if (resource.exists()) {
            executeSqlScript(fileName, false);
        } else {
            LOGGER.debug("Resource doesn't exist: {}", resource);
        }
    }

@Test
public void testLoadOrders() {
    Collection<Order> orders= dao.findAll();
    assertTrue(orders.size() == 3);
}
}

Here is what I THINK is happening based on some research. The first call to executeSqlScript is running in a separate transaction. Spring's SimpleJdbcTemplate.update method is called by executeSqlScript. Because this is a scoped to a JDBC connection, which is obtained from a connection pool, I am not guaranteed to get the same connection on subsequent accesses to the DB and therefore can't be guaranteed to run in the same transaction.

If I were to do all my DB operations through the TransactionManager or (Hibernate Session Factory) then it would work because of how the internals scope the transactions. My options here are to:

  1. Figure out how to run SimpleJdbcTemplate.update and the subsequent actual code I'm testing in the same transaction. I think I can possibly do this, but my efforts so far have been fruitless.

  2. Do all my test data set up through the SessionFactory. So, instead of executing straight SQL scripts through JDBC, I would be populating model objects and persisting them through a Hibernate DAO.

Am I on the right track here? Can anyone provide any more guidance?

Was it helpful?

Solution 2

I was able to solve this by adding the following 'jpaVendorAdapter' to the declaration of my entityManagerFactory.

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
</bean>

OTHER TIPS

You probably have auto-commit enabled on the db connection.

<bean id="dataSource" 
    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="${jdbc.driverClassName}"/>
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
  <property name="defaultAutoCommit" value="false"/>
</bean>

Note that you might also be passing this argument in the jdbc url.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top