Results 1 to 3 of 3

Thread: Autocommit is disabled automatically, so nothing in DB

  1. #1
    Join Date
    Feb 2011
    Location
    Barneveld, The Netherlands
    Posts
    4

    Default Autocommit is disabled automatically, so nothing in DB

    I have recenty started researching the use of Spring Data Repositories and created a small test application. Besides the standard definition of a domain object, a repository interface extended from CRUDRepository I have the following context file:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans  http://www.springframework.org/schem...-beans-3.0.xsd
    		http://www.springframework.org/schema/context  http://www.springframework.org/schem...ontext-3.0.xsd
    		http://www.springframework.org/schema/tx  http://www.springframework.org/schem...ing-tx-3.0.xsd
    		http://www.springframework.org/schema/aop  http://www.springframework.org/schem...ng-aop-3.0.xsd
    		http://www.springframework.org/schema/data/jpa  http://www.springframework.org/schem...ng-jpa-1.1.xsd
    		">
    
    	<jpa:repositories base-package="com.infor.ion.boddesk.server.repositories" />
    
    	<context:property-placeholder location="classpath:jdbc.properties" />
    
    	<tx:annotation-driven transaction-manager="transactionManager" />
    	
    	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    
    	<bean id="entityManagerFactory"	class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource">
    			<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    				<property name="driverClassName" value="${jdbc.driverClassName}" />
    				<property name="url" value="${jdbc.url}" />
    				<property name="username" value="${jdbc.userName}" />
    				<property name="password" value="${jdbc.password}" />
    			 </bean>
    		</property>
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="database" value="${jpa.database}" />
    				<property name="showSql" value="${jpa.showSql}" />
    				<property name="databasePlatform" value="${jpa.dialect}" />
    			</bean>
    		</property>
    		<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />								    
    		<property name="loadTimeWeaver">
    			<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    		</property>
    		<property name="persistenceUnitName" value="employeePU"></property>
    	</bean>
    
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
    	
    </beans>
    and a small JUnit test:
    Code:
    package com.infor.ion.boddesk.server.dao;
    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.infor.ion.boddesk.server.model.Employee;
    import com.infor.ion.boddesk.server.repositories.EmployeeRepository;
    
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:persistContext.xml")
    @Transactional
    public class DaoTest {
    	
    	private static Logger logger = LoggerFactory.getLogger(DaoTest.class);
    
    	@Autowired
    	private EmployeeRepository dao;
    	
    	@Test
    	public void testDao(){
    		logger.info("Test 'testDao' started .....");
    		Employee employee = new Employee();
    		employee.setAddress("Adress 1");
    		employee.setEmail("Email 1");
    		employee.setName("User name");
    		
    		Employee savedUser = dao.save(employee);
    		assertNotNull(savedUser.getId());
    		assertEquals(employee.getAddress(), savedUser.getAddress());
    		assertEquals(employee.getEmail(), savedUser.getEmail());
    		assertEquals(employee.getName(), savedUser.getName());
    		logger.info("Test 'testDao' done.");
    	}
    	
    }
    The test succeeds but the record isn't stored in the database. When setting the debug logging on I noticed the following:

    13:45:06.287 [main] DEBUG org.hibernate.engine.jdbc.internal.LogicalConnecti onImpl - Obtained JDBC connection
    13:45:06.287 [main] DEBUG org.hibernate.engine.transaction.internal.jdbc.Jdb cTransaction - initial autocommit status: true
    13:45:06.287 [main] DEBUG org.hibernate.engine.transaction.internal.jdbc.Jdb cTransaction - disabling autocommit
    13:45:06.289 [main] INFO com.infor.ion.boddesk.server.dao.DaoTest - Test 'testDao' started .....
    13:45:06.307 [main] DEBUG org.hibernate.engine.spi.ActionQueue - Executing identity-insert immediately
    13:45:06.327 [main] DEBUG org.hibernate.SQL - insert into Employee (address, dateOfBirth, designation, email, name, tenantId, version) values (?, ?, ?, ?, ?, ?, ?)
    13:45:06.359 [main] DEBUG org.hibernate.id.IdentifierGeneratorHelper - Natively generated identity: 9
    13:45:06.362 [main] INFO com.infor.ion.boddesk.server.dao.DaoTest - Test 'testDao' done.
    13:45:06.363 [main] DEBUG org.hibernate.engine.transaction.spi.AbstractTrans actionImpl - rolling back
    13:45:06.365 [main] DEBUG org.hibernate.engine.transaction.internal.jdbc.Jdb cTransaction - rolled JDBC Connection
    13:45:06.365 [main] DEBUG org.hibernate.engine.transaction.internal.jdbc.Jdb cTransaction - re-enabling autocommit

    Can anybody explain to me why this happens? And what I should do in the test class to make the record persistent?

    Thanks in advance.

  2. #2
    Join Date
    Oct 2011
    Posts
    9

    Default

    The root cause of this behavior is that @Transactional tests are rolled back by default. See here. That's the supporting point:
    By default, the framework will create and roll back a transaction for each test.
    So, the issue isn't because "Autocommit is disabled automatically" but because of roll back. That's exactly what happened to your test:
    13:45:06.363 [main] DEBUG org.hibernate.engine.transaction.spi.AbstractTrans actionImpl - rolling back
    13:45:06.365 [main] DEBUG org.hibernate.engine.transaction.internal.jdbc.Jdb cTransaction - rolled JDBC Connection

  3. #3
    Join Date
    Feb 2011
    Location
    Barneveld, The Netherlands
    Posts
    4

    Default

    Thanks for the quick reply and the very helpfull answer. By adding the
    @TransactionConfiguration(defaultRollback=false)
    to the test the rollback was suppressed and the test worked as expected.

Posting Permissions

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