Announcement Announcement Module
Collapse
No announcement yet.
Transaction doesnt rollback JdbcTemplate MySql Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transaction doesnt rollback JdbcTemplate MySql

    Hi,

    I am testing my transaction mechanism. Even though there is a runtimeException, transaction doesnt rollback.
    At the end of catch statement it commits.

    I use MySql(InnoDb) ,Spring 2.55 ,JDBCTemplate, JDK1.6

    Any help would be great. Thanks.

    Code:
    public class MediationServiceImpl implements MediationService {
       
     private MediationDao mediationDao;
    
     public static void main(String ards[]) {
    		ClassPathXmlApplicationContext appCon = new ClassPathXmlApplicationContext(
    				"spring-config.xml");
    		MediationService service = (MediationService) appCon
    				.getBean("mediationService");
    		
    		service.transactionTest();
    	}
    
    	@Override
    	public void transactionTest() {
    		List<MediationModel> mediaList = new ArrayList<MediationModel>();
    		try {
    			this.mediationDao.insertIntoMediationTable();
    			
    			throw new RuntimeException();
    
    		} catch (RuntimeException e) {
    			e.printStackTrace();
    			mediaList = this.mediationDao.getMatchedMediationData();
    		}
    	}



    Code:
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close" >-->
            <!-- http://commons.apache.org/dbcp/api-1.2.2/org/apache/commons/dbcp/BasicDataSource.html-->
            <property name="driverClassName" value="${dataSource.driverClassName}"/>
            <property name="url" value="${dataSource.url}"/>
            <property name="username" value="${dataSource.username}"/>
            <property name="password" value="a"/>
    <!--        <property name="validationQuery" value="${dataSource.validationQuery}"/>-->
            <property name="defaultAutoCommit" value="false" />
        </bean>
       
    	<bean id="transactionManager"		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource"
    			ref="dataSource" />
    	</bean>
    
        <bean id="jdbcTemplate"
    		class="org.springframework.jdbc.core.JdbcTemplate">
    		<property name="dataSource" ref="dataSource" />
    	</bean>
        
        <bean id="mediationDao" class="dao.impl.MediationDaoImpl">
            <property name="jdbcTemplate" ref="jdbcTemplate" />
        </bean>
        
    
        <bean name="mediationService"   class="service.Impl.MediationServiceImpl">
            <property name="mediationDao" ref="mediationDao"/>
        </bean>
    
    	<tx:advice 	id="txAdvice"
    				transaction-manager="transactionManager">
    		<tx:attributes>
    			<tx:method 	name="transactionTest"  propagation="REQUIRED"/>
    			<tx:method 	name="*" read-only="true" />
    		</tx:attributes>
    	</tx:advice>
    	
    	<aop:config>
    		<aop:pointcut
    			id="serviceOperation"
    			expression="execution(* service.*.*(..))" />
    		<aop:advisor
    			advice-ref="txAdvice"
    			pointcut-ref="serviceOperation" />
    	</aop:config>
    Code:
    19:19:36,157 DEBUG DefaultListableBeanFactory:214 - Returning cached instance of singleton bean 'txAdvice'
    19:19:36,157 DEBUG DefaultListableBeanFactory:214 - Returning cached instance of singleton bean 'org.springframework.aop.config.internalAutoProxyCreator'
    19:19:36,157 DEBUG DefaultListableBeanFactory:214 - Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
    19:19:36,157 DEBUG ClassPathXmlApplicationContext:273 - Publishing event in context [org.springframework.context.support.ClassPathXmlApplicationContext@e83912]: org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.ClassPathXmlApplicationContext@e83912: display name [org.springframework.context.support.ClassPathXmlApplicationContext@e83912]; startup date [Thu Jan 27 19:19:35 EET 2011]; root of context hierarchy]
    19:19:36,157 DEBUG DefaultListableBeanFactory:214 - Returning cached instance of singleton bean 'mediationService'
    19:19:36,173 DEBUG DataSourceTransactionManager:346 - Using transaction object [org.springframework.jdbc.datasource.DataSourceTransactionManager$DataSourceTransactionObject@6210fb]
    19:19:36,173 DEBUG DataSourceTransactionManager:374 - Creating new transaction with name [service.MediationService.transactionTest]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
    19:19:36,392 DEBUG DataSourceTransactionManager:202 - Acquired Connection [jdbc:mysql://localhost:3306/pmfcdr, UserName=root@localhost, MySQL-AB JDBC Driver] for JDBC transaction
    19:19:36,392 DEBUG TransactionSynchronizationManager:186 - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@e35bb7] for key [org.apache.commons.dbcp.BasicDataSource@77eaf8] to thread [main]
    19:19:36,392 DEBUG TransactionSynchronizationManager:261 - Initializing transaction synchronization
    19:19:36,392 DEBUG JdbcTemplate:639 - Executing prepared SQL query
    19:19:36,392 DEBUG JdbcTemplate:574 - Executing prepared SQL statement
    19:19:36,392 DEBUG TransactionSynchronizationManager:142 - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@e35bb7] for key [org.apache.commons.dbcp.BasicDataSource@77eaf8] bound to thread [main]
    19:19:36,407 DEBUG TransactionSynchronizationManager:142 - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@e35bb7] for key [org.apache.commons.dbcp.BasicDataSource@77eaf8] bound to thread [main]
    19:19:36,438 DEBUG TransactionSynchronizationManager:142 - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@e35bb7] for key [org.apache.commons.dbcp.BasicDataSource@77eaf8] bound to thread [main]
    19:19:36,438 DEBUG JdbcTemplate:507 - Executing SQL update [INSERT INTO mediation VALUES (NULL, '201010261413338', UNIX_TIMESTAMP('201010261413338'), '5353134442', 'B0302125498331', 444, 'SSS', 'IN', 'OUT', 2, '201010261416338')]
    19:19:36,438 DEBUG TransactionSynchronizationManager:142 - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@e35bb7] for key [org.apache.commons.dbcp.BasicDataSource@77eaf8] bound to thread [main]
    19:19:36,438 DEBUG TransactionSynchronizationManager:142 - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@e35bb7] for key [org.apache.commons.dbcp.BasicDataSource@77eaf8] bound to thread [main]
    19:19:36,438 DEBUG JdbcTemplate:514 - SQL update affected 1 rows
    19:19:36,438 DEBUG TransactionSynchronizationManager:142 - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@e35bb7] for key [org.apache.commons.dbcp.BasicDataSource@77eaf8] bound to thread [main]
    java.lang.RuntimeException
    	at service.Impl.MediationServiceImpl.transactionTest(MediationServiceImpl.java:45)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    	at $Proxy0.transactionTest(Unknown Source)
    	at service.Impl.MediationServiceImpl.main(MediationServiceImpl.java:31)
    19:19:36,438 DEBUG JdbcTemplate:639 - Executing prepared SQL query
    19:19:36,438 DEBUG JdbcTemplate:574 - Executing prepared SQL statement
    19:19:36,438 DEBUG TransactionSynchronizationManager:142 - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@e35bb7] for key [org.apache.commons.dbcp.BasicDataSource@77eaf8] bound to thread [main]
    19:19:36,438 DEBUG TransactionSynchronizationManager:142 - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@e35bb7] for key [org.apache.commons.dbcp.BasicDataSource@77eaf8] bound to thread [main]
    19:19:36,438 DEBUG TransactionSynchronizationManager:142 - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@e35bb7] for key [org.apache.commons.dbcp.BasicDataSource@77eaf8] bound to thread [main]
    19:19:36,438 DEBUG DataSourceTransactionManager:880 - Triggering beforeCommit synchronization
    19:19:36,438 DEBUG DataSourceTransactionManager:893 - Triggering beforeCompletion synchronization
    19:19:36,438 DEBUG DataSourceTransactionManager:707 - Initiating transaction commit
    19:19:36,438 DEBUG DataSourceTransactionManager:259 - Committing JDBC transaction on Connection [jdbc:mysql://localhost:3306/pmfcdr, UserName=root@localhost, MySQL-AB JDBC Driver]
    19:19:36,485 DEBUG DataSourceTransactionManager:906 - Triggering afterCommit synchronization
    19:19:36,485 DEBUG DataSourceTransactionManager:922 - Triggering afterCompletion synchronization
    19:19:36,485 DEBUG TransactionSynchronizationManager:315 - Clearing transaction synchronization
    19:19:36,485 DEBUG TransactionSynchronizationManager:232 - Removed value [org.springframework.jdbc.datasource.ConnectionHolder@e35bb7] for key [org.apache.commons.dbcp.BasicDataSource@77eaf8] from thread [main]
    19:19:36,485 DEBUG DataSourceTransactionManager:314 - Releasing JDBC Connection [jdbc:mysql://localhost:3306/pmfcdr, UserName=root@localhost, MySQL-AB JDBC Driver] after transaction
    19:19:36,485 DEBUG DataSourceUtils:312 - Returning JDBC Connection to DataSource

  • #2
    Hi,

    I tried anotation driven approach but results were same.

    Then programmaticly created a savePoint and after exception rolled back to savepoint. Log was same but transaction rolled back succesfully.

    Comment


    • #3
      I suggest a forum search as this question has been answered numerous times before.

      1) You are catching the xception yourself, the transaction support never sees it so everything is ok thus commit
      2) You are using MySQL make sure you use InnoDB tables, other table types are non-transactional.

      Comment


      • #4
        But it is a unchecked exception.

        Comment


        • #5
          As I stated, you are catching the exception yourself the transaction support doesn't see it and hence for the tx support everything is ok... If the tx support doesn't see the exception, which it doesn't because you swallow it, it assumes everything is fine..

          Comment


          • #6
            I think i got the point. Thank you.

            Comment

            Working...
            X