Results 1 to 3 of 3

Thread: Rollback with Hibernate ist not working as expected

  1. #1
    Join Date
    Mar 2012
    Posts
    2

    Default Rollback with Hibernate ist not working as expected

    Hi,
    i have the following Problem in my Spring/Hibernate application:
    I have created some Unit-Tests to test my Database-Layer code.
    I want to save a list of ojects into my Oracle-Database. All the save operations are excecuted in the same transaction. If one of these saves fails due to a database error (Unique-Constraint) the elements written before the error occured are not rolled back.
    Here is my Configuration:
    A Dao which does the work

    Code:
     public void save(List<E> elements) throws HibernateException
        {
            for (E element : elements)
            {
                sessionFactory.getCurrentSession().saveOrUpdate(element);
            }
        }
    And a service class that calls the dao
    Code:
    	@Transactional(readOnly = false)
    	public void saveAll(List<E> list) {
    		dao.save(list);
    	}
    I call the saveAll function from a Spring Unit Test environment with a list containing three elements.
    The second element contains a key which does already exist in the database table so a error is thrown when the transaction ist commited.
    Although the transaction seems to be rolled back the first entry is written to the database. If i move the false entry to the end of the list both the first and second entry are commited.

    Here is my database configuration:
    Code:
    	<bean id="datasource" 
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
    		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    		<property name="url"
    			value="jdbc:oracle:thin:@xxx" />
    		<property name="username" value="xx" />
    		<property name="password" value="xx" />
    		
    	</bean>
    	<!-- Transaktionsmanagement -->
    	<bean id="transactionManager" scope="singleton" 
    		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="datasource"></property>
    	</bean>
    		<!-- Transaktionsmanagement �ber Annotation. -->
    	<tx:annotation-driven />
    
    <bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource" ref="datasource" />
    
    		<property name="mappingResources">
    			<list>
    		...
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">
    					org.hibernate.dialect.Oracle10gDialect</prop>
    				<prop key="hibernate.cache.use_second_level_cache">false</prop>
    				<prop key="hibernate.cache.use_query_cache">false</prop>
    				<prop key="hibernate.connection.autocommit">false</prop>
    			</props>
    		</property>
    	</bean>
    I am using Hibernate 3.6.4 and Spring 3.0.5
    Appatently the session data is written after the session.close in org.springframework.orm.hibernate3.SessionFactoryU tils.closeSessionOrRegisterDeferredClose(session, sessionFactory)
    At the moment this only occurs in an testing environment and i can't test if it does in produciton because this does not exist.
    Is this an error or am i missing something?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Please use the forum search as this question has been answered before...

    Use the correct transaction manager, you are using hibernate so use HibernateTransactionManager not DataSourceTransactionManager.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Mar 2012
    Posts
    2

    Default

    Quote Originally Posted by Marten Deinum View Post
    Please use the forum search as this question has been answered before...

    Use the correct transaction manager, you are using hibernate so use HibernateTransactionManager not DataSourceTransactionManager.
    Thanks. That did it. It seems that after 2 days of searching i just hadn't the right keywords to find the right thread.

    Will this be working with org.springframework.transaction.jta.WebSphereUowTr ansactionManager? I will use this one in our productive environment.

Tags for this Thread

Posting Permissions

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