Announcement Announcement Module
Collapse
No announcement yet.
Spring Declarative Transaction - Roll back not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Declarative Transaction - Roll back not working

    I have followed the documentation from Spring and have setup the declarative tx using Spring AOP, but the rollback doesn't work. I have put in the code, application context and the log file. Rollback doesn't work.
    Any help is appreciated. Thanks in advance.

    http://static.springsource.org/sprin...ansaction.html


    Java Code
    --------------
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED, rollbackFor = AdjExecutionRuntimeException.class)
    public void processAdjustment(Connection conn) throws Exception {

    //DOMConfigurator.configure("log4j.xml");
    int affectedRows = 0;

    PreparedStatement stmt = null;
    try {

    String sql = "UPDATE SCHEMA_NAME.TABLE_NAME " +
    "SET ABC_AMT = 98765 " +
    "WHERE " +
    "XYZ IN (12587256, 12283522, 11619250) AND PQR > 2400 AND " +
    "PRIM_KEY = 201201";

    stmt = conn.prepareStatement(sql);

    affectedRows = stmt.executeUpdate();

    throw new AdjExecutionRuntimeException("force fail");

    } finally {
    // have the code to close all the connections.
    }
    }



    Spring Context.xml
    ------------------
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schem...ng-jee-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.xxx.xx.test"/>

    <bean id="updateProcessor" class="com.xxx.xx.test.UpdateProcessor">
    </bean>

    <bean id="abcDS"
    class="org.springframework.jdbc.datasource.DriverM anagerDataSource"
    scope="singleton" >
    <property name="driverClassName" value="com.teradata.jdbc.TeraDriver" />
    <property name="url" value="jdbc:teradata://abc.xyz.com/database=XXXX1111,TMODE=ANSI" />
    <property name="username" value="" />
    <property name="password" value="" />
    </bean>

    <!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="txManager" />


    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
    <property name="dataSource" ref="abcDS" />
    </bean>

    </beans>


    log output
    ----------
    2012-04-09 10:56:36,366 DEBUG [main] - Initializing transaction synchronization
    2012-04-09 10:56:36,366 DEBUG [main] - Getting transaction for [com.x.y.z.UpdateProcessor.processAdjustment]
    2012-04-09 10:56:36,772 DEBUG [main] - Completing transaction for [com.x.y.z.UpdateProcessor.processAdjustment] after exception: com.a.b.AdjExecutionRuntimeException: force fail
    2012-04-09 10:56:36,772 DEBUG [main] - Applying rules to determine whether transaction should rollback on com.a.b.AdjExecutionRuntimeException: force fail
    2012-04-09 10:56:36,772 DEBUG [main] - Winning rollback rule is: RollbackRuleAttribute with pattern [com.a.b.AdjExecutionRuntimeException]
    2012-04-09 10:56:36,772 DEBUG [main] - Triggering beforeCompletion synchronization
    2012-04-09 10:56:36,772 DEBUG [main] - Initiating transaction rollback
    2012-04-09 10:56:36,772 DEBUG [main] - Rolling back JDBC transaction on Connection [com.teradata.jdbc.jdbc_3.ifjdbc_4.TeraLocalConnect ion@10fd7f6]
    2012-04-09 10:56:36,819 DEBUG [main] - Triggering afterCompletion synchronization
    2012-04-09 10:56:36,819 DEBUG [main] - Clearing transaction synchronization
    2012-04-09 10:56:36,819 DEBUG [main] - Removed value [org.springframework.jdbc.datasource.ConnectionHold er@5976c2] for key [org.springframework.jdbc.datasource.DriverManagerD ataSource@1038de7] from thread [main]
    2012-04-09 10:56:36,819 DEBUG [main] - Resetting isolation level of JDBC Connection [com.teradata.jdbc.jdbc_3.ifjdbc_4.TeraLocalConnect ion@10fd7f6] to 8
    2012-04-09 10:56:36,897 DEBUG [main] - Releasing JDBC Connection [com.teradata.jdbc.jdbc_3.ifjdbc_4.TeraLocalConnect ion@10fd7f6] after transaction
    2012-04-09 10:56:36,897 DEBUG [main] - Returning JDBC Connection to DataSource

  • #2
    Please add [ code][/code ] tags that way your code remains readable...

    Your current setup will not work. Spring isn't in control of the resources (for some reason you want to pass in the connection yourself, why?!) thus spring does't control the transaction and as such rollback isn't working either.

    Also don't mess around with the connection (closing etc.) yourself spring should handle that for correct management.

    Comment

    Working...
    X