Announcement Announcement Module
Collapse
No announcement yet.
AbstractTransactionalSpringContextTests does not RollBack Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AbstractTransactionalSpringContextTests does not RollBack

    Hi!

    I am trying to use AbstractTransactionalSpringContextTests so that my tests rollback all my database work after having finished. But it is not working. Here is my configuration:
    - spring 1.2.6
    - hibernate 3
    - sqlserver 2000

    I firstly designed my DB, and then using reverse engineer hibernate tool create my POJOs, DAOs and hbm mapping files.

    Context related to the test:
    Code:
    <bean id="employee"
    		class="com.amadeus.merchstore.employee.Employee">
    		<property name="name">
    			<value>Juanjito</value>
    		</property>
    	</bean>
    
    	<bean id="myTransManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref bean="mySessionFactory" />
    		</property>
    	</bean>
    
    	<bean id="employeeDAO"
    		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager">
    			<ref local="myTransManager" />
    		</property>
    		<property name="target">
    			<bean
    				class="com.amadeus.merchstore.employee.dao.hibernate.HibernateEmployeeDAO">
    			</bean>
    		</property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="save*">PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED</prop>
    			</props>
    		</property>
    	</bean>
    Context related to database setup
    Code:
    <!-- DataSource -->
    	<bean id="myDataSource"
    		class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close">
    		<property name="driverClassName"
    			value="net.sourceforge.jtds.jdbc.Driver" />
    		<property name="url"
    			value="jdbc:jtds:sqlserver://localhost:1433/MerchStore" />
    		<property name="username" value="root" />
    		<property name="password" value="root" />
    		<!-- 	<property name="default_schema" value="dbo" /> -->
    	</bean>
    
    	<!-- Hibernate Session Factory -->
    	<bean id="mySessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource" ref="myDataSource" />
    		<property name="mappingResources">
    			<!-- hbm files -->
    			<list>
    				<value>
    	com\amadeus\merchstore\employee\dao\hibernate\Employee.hbm.xml
    				</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">
    					org.hibernate.dialect.SQLServerDialect
    				</prop>
    			</props>
    		</property>
    	</bean>
    My Test gets correctly populated with auto-wiring. And logs said that RollBack has been done, but Data has been persisted.

    What am i missing?

    Thank you a lot for any help!

  • #2
    Is it possible to see the test and the logs?

    Comment


    • #3
      Of course it is!

      Code:
      import junit.framework.Test;
      import junit.framework.TestSuite;
      
      import org.springframework.orm.hibernate3.HibernateTransactionManager;
      import org.springframework.test.AbstractTransactionalSpringContextTests;
      
      
      
      public class EmployeeDAOTest extends AbstractTransactionalSpringContextTests {
      
      	public EmployeeDAO employeeDAO;
      	public Employee employee;
      	public HibernateTransactionManager myTransManager;
      	
      	
      	
      	public static Test suite() {
      		return new TestSuite(EmployeeDAOTest.class);
      	}
      
      	
      	public void testSaveCountDelete() {
      		employeeDAO.save(employee);
      		assertTrue("",true);
      		
      	}
      
      	protected String[] getConfigLocations() {
      		return new String[] { "testContext.xml", "beans.xml" };
      	}
      
      	public Employee getEmployee() {
      		return employee;
      	}
      
      	public void setEmployee(Employee employee) {
      		this.employee = employee;
      	}
      	
      	public HibernateTransactionManager getMyTransManager() {
      		return myTransManager;
      	}
      
      	public void setMyTransManager(HibernateTransactionManager myTransManager) {
      		this.myTransManager = myTransManager;
      	}
      
      	
      	public EmployeeDAO getEmployeeDAO() {
      		return this.employeeDAO;
      	}
      
      	public void setEmployeeDAO(EmployeeDAO EmployeeDAO) {
      		this.employeeDAO = EmployeeDAO;
      	}
      }
      just one test that saves an Employee.

      Logs for running this junit test:
      Code:
      06-sep-2007 15:34:00 org.springframework.test.AbstractSpringContextTests loadContextLocations
      INFO: Loading config for: testContext.xml,beans.xml
      ...
      ...
      
      INFO: Using default transaction strategy (direct JDBC transactions)
      06-sep-2007 15:34:02 org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
      INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory buildSettings
      INFO: Automatic flush during beforeCompletion(): disabled
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory buildSettings
      INFO: Automatic session close at end of transaction: disabled
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory buildSettings
      INFO: Scrollable result sets: enabled
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory buildSettings
      INFO: JDBC3 getGeneratedKeys(): enabled
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory buildSettings
      INFO: Connection release mode: on_close
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory buildSettings
      INFO: Default batch fetch size: 1
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory buildSettings
      INFO: Generate SQL with comments: disabled
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory buildSettings
      INFO: Order SQL updates by primary key: disabled
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
      INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
      06-sep-2007 15:34:02 org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
      INFO: Using ASTQueryTranslatorFactory
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory buildSettings
      INFO: Query language substitutions: {}
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory buildSettings
      INFO: Second-level cache: enabled
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory buildSettings
      INFO: Query cache: disabled
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory createCacheProvider
      INFO: Cache provider: org.hibernate.cache.EhCacheProvider
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory buildSettings
      INFO: Optimize cache for minimal puts: disabled
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory buildSettings
      INFO: Structured second-level cache entries: disabled
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory buildSettings
      INFO: Statistics: disabled
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory buildSettings
      INFO: Deleted entity synthetic identifier rollback: disabled
      06-sep-2007 15:34:02 org.hibernate.cfg.SettingsFactory buildSettings
      INFO: Default entity-mode: pojo
      06-sep-2007 15:34:02 org.hibernate.impl.SessionFactoryImpl <init>
      INFO: building session factory
      06-sep-2007 15:34:02 org.hibernate.impl.SessionFactoryObjectFactory addInstance
      INFO: Not binding factory to JNDI, no JNDI name configured
      06-sep-2007 15:34:02 org.hibernate.impl.SessionFactoryImpl checkNamedQueries
      INFO: Checking 0 named queries
      06-sep-2007 15:34:02 org.springframework.jdbc.datasource.JdbcTransactionObjectSupport <clinit>
      INFO: JDBC 3.0 Savepoint class is available
      06-sep-2007 15:34:02 org.springframework.test.AbstractTransactionalSpringContextTests onSetUp
      INFO: Began transaction: transaction manager [[email protected]918f0]; defaultRollback true
      06-sep-2007 15:34:02 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
      INFO: Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
      06-sep-2007 15:34:02 org.springframework.jdbc.support.SQLErrorCodesFactory <init>
      INFO: SQLErrorCodes loaded: [DB2, HSQL, MS-SQL, MySQL, Oracle, Informix, PostgreSQL, Sybase]
      06-sep-2007 15:34:03 org.springframework.test.AbstractTransactionalSpringContextTests endTransaction
      INFO: Rolled back transaction after test execution
      Log is not complete, because of forums limitations. But i have removed not interesting parts. I could send you the complete log if you want.

      Thank you a lot!!

      Comment


      • #4
        The really interesting part is still missing - your DAO implementation itself.

        Comment


        • #5
          I just call getHibernateTemplate().saveOrUpdate(employee).

          I am still lost :S

          Thank you again!

          Comment

          Working...
          X