Results 1 to 3 of 3

Thread: Transaction is not rolling back in case of exception

  1. #1

    Default Transaction is not rolling back in case of exception

    I think transaction is not kicking in atall. I can provide other files if required.

    On console it inserts the data and throws the exception but no rollback.

    Boot.java

    Code:
    public class Boot {
    	
    	public static void main(final String[] args) throws Exception {
    
    		ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml", Boot.class);
    	    FooService fooService = (FooService) ctx.getBean("fooService");
    	    
    	    Testemployee testemployee = new Testemployee();
    	    testemployee.setEmployeename("someName");
    	    
    	    fooService.insertFoo (testemployee);
    
    	  }
    }
    context.xml

    Code:
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">	 
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://x.x.x.x:3306/db" />
    		<property name="username" value="un" />
    		<property name="password" value="pwd" />
    	</bean>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource"><ref local="dataSource" /></property>
    		<property name="configLocation">
    			<value>
    				classpath:resources/hibernate/hibernate.cfg.xml
    			</value>
    		</property>
    	</bean>
    
    <bean id="PersistenceFacade" class="com.x.x.persistence.PersistenceFacadeImpl">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    
    <bean id="FacadeLookup" class="com.x.x.service.FacadeLookup">
    		<property name="persistenceFacade" ref="PersistenceFacade" />
    	</bean>
    
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory"></property>
    	</bean>
    
    <bean id="fooService" class="com.x.x.admin.service.FooServiceImpl">
    		<property name="facadeLookup" ref="FacadeLookup"></property>
    	</bean>
    
    	<tx:advice id="txAdvice" transaction-manager="txManager">
    		<tx:attributes>
    			<tx:method name="insertFoo" rollback-for="Exception"/>
    		</tx:attributes>
    	</tx:advice>
    
    <aop:config>
    		<aop:pointcut id="fooServiceOperation" expression="execution(* com.x.x.admin.service.*.*(..))"/>
    		<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
    	</aop:config>
    FooServiceImpl.java

    Code:
    public class FooServiceImpl implements FooService
    {
    FacadeLookup facadeLookup;
    
    public void insertFoo(Testemployee testemployee) throws Exception {
    		
    		facadeLookup.getPersistenceFacade().save(testemployee);
    		
    		throw new Exception();
    		
    	}
    public void setFacadeLookup(FacadeLookup facadeLookup) {
    		this.facadeLookup = facadeLookup;
    	}
    }
    Cross post : http://www.coderanch.com/t/526343/Sp...k-case#2386514
    Last edited by Vishal Pandya; Feb 6th, 2011 at 04:50 PM. Reason: To add cross post link

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

    Default

    Make sure you use transactional tables (InnoDB) instead of non-transactional (MyISAM)...

    Next to that you have some weird way of getting access to something that persists into the database...

    Also please post your hibernate configuration.
    Last edited by Marten Deinum; Feb 7th, 2011 at 01:57 AM.
    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

    Default

    Quote Originally Posted by Marten Deinum View Post
    Make sure you use transactional tables (InnoDB) instead of non-transactional (MyISAM)...
    Indeed, this was the issue. Table type was MyISAM. I changed it to InnoDB and it works. Thanks.

    Quote Originally Posted by Marten Deinum View Post
    Next to that you have some weird way of getting access to something that persists into the database...
    What is weird about this configuration? I have made multiple facades for various tasks like PersistenceFacade for DB operation, Securityfacade for login and this kind of stuff, UtilFacade serves as normal utility classes. I would love to know an expert's opinion?

    Thanks again.

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
  •