Announcement Announcement Module
Collapse
No announcement yet.
Rollback not rolling back! SQLServer & AbstractTransactionalDataSourceSpringContextT Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Rollback not rolling back! SQLServer & AbstractTransactionalDataSourceSpringContextT

    Looks like I have hit the same problem as a previous poster who didn't get a solution (http://forum.springframework.org/showthread.php?t=43519). Hopefully if I try posting again I'll have more luck!

    Using AbstractTransactionalDataSourceSpringContextTests to run tests against a SQLServer 8 database. Tests run, log says transaction is rolled back, but querying the database shows inserts done by the test are still there.

    Thought it might be that autocommit was on by default, so I added a Hibernate property to set autocommit=false. Didn't solve the problem.

    Below are the details. Any help appreciated. It's been one of those days...

    DAO
    Code:
    public class TemplateDao extends HibernateDaoSupport implements TemplateDaoI {
    
    	public void create(Template template) {
    		
    		if (template != null) {
    			getHibernateTemplate().save(template);
    		}
    	}
    }
    Extract from app context
    Code:
    <bean id="dataSource"
    	class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    	<property name="driverClassName">
    		<value>net.sourceforge.jtds.jdbc.Driver</value>
    	</property>
    	<property name="url">
    		<value>
    			jdbc:jtds:sqlserver://MSERV1:11000/APP_DB			</value>
    	</property>
    	<property name="username">
    		<value>admin</value>
    	</property>
    	<property name="password">
    		<value>admin</value>
    	</property>
    </bean>
    
    
    <bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    	<property name="dataSource" ref="dataSource" />
    
    	<property name="mappingLocations">
    		<value>								classpath*:com/myco/mypro/domain/**/*.hbm.xml
    		</value>
    	</property>
    
    	<property name="hibernateProperties">
    		<props>
    			<prop key="hibernate.dialect">
    					org.hibernate.dialect.SQLServerDialect
    			</prop>
    			<prop key="hibernate.show_sql">true</prop>
    			<prop key="hibernate.cache.provider_class">
    					org.hibernate.cache.EhCacheProvider
    			</prop>
    			<prop key="hibernate.connection.autocommit">
    				false
    			</prop>
    		</props>
    	</property>
    </bean>
    
    <bean id="templateDao" class="com.myco.mypro.dao.TemplateDao">
    	<property name="sessionFactory">
    		<ref bean="sessionFactory" />
    	</property>
    </bean>
    
    <!-- transaction manager -->
    <bean id="txManager"
    	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- map method names to transactional behaviour -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
    	<tx:attributes>
    		<tx:method name="read*" read-only="true" propagation="REQUIRED" />
    		<tx:method name="*" propagation="REQUIRED" />
    	</tx:attributes>
    </tx:advice>
    
    
    <!-- Match tx confiig with DAOs -->
    <aop:config>
    	<aop:pointcut id="daoOperation"
    		expression="execution(* com.myco.mypro.dao.*.*(..))" />
    	<aop:advisor advice-ref="txAdvice" pointcut-ref="daoOperation" />
    </aop:config>

    The Test

    Code:
    public class TemplateDaoTest extends AbstractTransactionalDataSourceSpringContextTests  {
    	private TemplateDaoI templateDao;
    
    	public void testCreateTemplate() throws Exception {
    		Template t = new Template();
    		t.setMimeType("application/pdf");
    		t.setDescription("Test Template 1");
    
    		templateDao.create(t);
    
    		assertNotNull(t.getId());
    		assertEquals(Long.valueOf(0), t.getVersion());
    	}
    
    	public void setTemplateDao(TemplateDaoI templateDao) {
    		this.templateDao = templateDao;
    	}
    
    	protected String[] getConfigLocations() {
    		return new String[] { "classpath:config/testApplicationContext.xml" };
    	}
    }
    Log Output from Tests

    Code:
    28-Nov-2007 16:20:46 org.springframework.test.AbstractSingleSpringContextTests loadContextLocations
    .......
    28-Nov-2007 16:20:49 org.hibernate.impl.SessionFactoryObjectFactory addInstance
    INFO: Not binding factory to JNDI, no JNDI name configured
    28-Nov-2007 16:20:50 org.springframework.jdbc.datasource.JdbcTransactionObjectSupport <clinit>
    INFO: JDBC 3.0 Savepoint class is available
    28-Nov-2007 16:20:50 org.springframework.test.AbstractTransactionalSpringContextTests startNewTransaction
    INFO: Began transaction (1): transaction manager [[email protected]e29820]; default rollback = true
    Hibernate: insert into Template (VERSION, MIME_TYPE, DESCRIPTION) values (?, ?, ?)
    28-Nov-2007 16:20:50 org.springframework.test.AbstractTransactionalSpringContextTests endTransaction
    INFO: Rolled back transaction after test execution

  • #2
    DataSourceTransactionManager doesn't work for Hibernate.

    Comment

    Working...
    X