Results 1 to 5 of 5

Thread: Problem with transactionManager. The rollback does not work

  1. #1
    Join Date
    Jan 2012
    Location
    Brazil, Pernambuco - PE
    Posts
    3

    Default Problem with transactionManager. The rollback does not work

    Hi guys

    I have a problem with transactionManager. The rollback does not work. I'm using Spring 3.0.6 and Hibernate 3.9.0

    My settings


    context.xml
    Code:
    		<context:annotation-config/>
    		<context:component-scan base-package="project.cmi"/>
    		<tx:annotation-driven transaction-manager="txManager"/>
    
    		<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    			<property name="locations" value="classpath:project/cmi/jdbc.properties"/>
    		</bean>
    
    		<bean id="cmiDS" 
    			  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>
    
    		<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    			<property name="dataSource" ref="cmiDS"/>
    		</bean>
    
    		<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">		
    			<property name="dataSource" ref="cmiDS"/>
    			<property name="packagesToScan" value="project.cmi.domain"/>			
    			<property name="hibernateProperties">
    				<value>
    					hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
    					hibernate.show_sql=true
    					hibernate.format_sql=true
    				</value>
    			</property>		
    		</bean>
    Repository
    Code:
    @Repository("contactRepository")
    public class ContactRepository {
    
    	@Autowired	
    	private SessionFactory sessionFactory;	
    		
    	public void persist(Contact contact){
    		sessionFactory.getCurrentSession().save(contact);
    	}
    	
    }
    Service
    Code:
    @Transactional(readOnly=true)
    @Service("contactService")
    public class ContactService {
    
    	@Autowired	
    	private ContactRepository contactRepository;
    		
    	@Transactional(propagation=Propagation.REQUIRED, readOnly=false, rollbackFor={RuntimeException.class})
    	public void persist(Contact contact) throws Exception {
    
    		contatoRepository.persist(contact);
                    throw new RuntimeException;  //The rollback does not work
    		
    	}
    	
    }
    Thank you for help!

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

    Default

    I suggest that you first fix your configuration. You are using hibernate but using the wrong transactionamanger. So basically you aren't in control of your transactions. Use the HibenateTransactionManager.
    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
    Jan 2012
    Location
    Brazil, Pernambuco - PE
    Posts
    3

    Default

    Hi Marten

    Thanks for the help. Substitution did, but the problem continues ...

    Code:
    	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory"/>
    	</bean>

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

    Default

    I suggest a forum search as questions like these have been answered numerous times before. If rollback isn't happening you have either something wrong in your code (like catching exceptions) or you are using a database (or tables) that don't support transactions (like MyISAM tables in MySQL).

    Also rollback is default for runtime exceptions so not sure why you want that included in your rollbackFor configuration.
    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

  5. #5
    Join Date
    Jan 2012
    Location
    Brazil, Pernambuco - PE
    Posts
    3

    Default

    It worked!

    Thank you, was right in the table was MyISAM.
    Changed to InnoDB and it worked perfectly.


Posting Permissions

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