Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Spring TransactionTemplate not rolling back

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am using Spring BatchSqlUpdate to write to 2 different tables.  I wish that if the insert to the second table fails then the insert to the first table should also be rolledback. I tried using TransactionTemplate to achieve the same.  However the same doesnt seem to work . Following is the code and my configuration.



<beans xmlns="http://www.springframework.org/schema/beans"; <br /> xmlns:jdbc="http://www.springframework.org/schema/jdbc"; <br /> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; <br /> xsi:schemaLocation="http://www.springframework.org/schema/beans <br /> http://www.springframework.org/schema/beans/spring-beans-3.2.xsd <br /> http://www.springframework.org/schema/jdbc <br /> http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">

       <!-- connect to MySQL database -->
<bean id="dataSource" <br /> class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="Vallabh" />
</bean>

<bean id="transactionManager" <br /> class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

<bean id = "jdbcTemplate" <br />      class = "org.springframework.jdbc.core.JdbcTemplate">
     <property name = "dataSource" ref = "dataSource" />    
  </bean>
 
  <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource"/>
</bean>

  <bean id="floatService" class ="com.vm.dm.vp.TestService">
  <constructor-arg index="0" ref="jdbcTemplate"> </constructor-arg>
  <constructor-arg index="1" ref="transactionManager"> </constructor-arg>
 
  </bean>


</beans>

As explained above the insert to the nos table fails since i have made it fail purposely by giving wrong column name. However still the insert to the price table is not rolled back and is commited. What do i to achieve the intended purpose.
 
Ranch Foreman
Posts: 1898
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you see any exception thrown when you insert data to a column that does not exist?
I am not familiar with Spring framework yet. But I guess the data is successfully committed to the price table in one transaction while the transaction for nos table fails.
 
I think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic