Announcement Announcement Module
Collapse
No announcement yet.
Hi problem during declarative transaction Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hi problem during declarative transaction

    Hi,
    I am finding difficulties when i set my operation in declarative transaction.
    the rollback is not happening even though the perticular method gives exception .can anybody help me to solve this?
    my code look as below

    my dao class where i get exception
    -----------------------------------

    public String myQuery()throws DataAccessException {
    String name = "";


    try {
    jt = new JdbcTemplate();
    jt.setDataSource(dataSource);
    name =(String) jt.queryForObject(
    "select name from retiredb where Id=1",
    java.lang.String.class);
    jt.update("update retiredb set name='test20' where Id=1");
    jt.update("insert into spring2 values ('test2','address2')");
    } catch (Exception ee) {

    System.out.println("inside exception Exception ------" + ee);
    }

    return name;

    }

    my xml files
    -----------------

    <bean id="retirementBusinessObject" class="beans.RetirementBusinessObject">
    <property name="dataAccessObject"><ref local="testSpring"/></property>
    </bean>


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

    <bean id="senderNewTx" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref bean="transactionManager"/>
    </property>
    <property name="target">
    <ref bean="retirementBusinessObject"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
    </props>
    </property>
    </bean>

    is there any problem in my aproach?
    or any additional coding i have to do for the rollback?

    Thanks
    biju

  • #2
    Two points: you should create a JdbcTemplate in your DAO and reuse it. JdbcTemplates are threadsafe. Consider extending the JdbcDaoSupport convenience class, that gives you a JdbcTemplate when given a DataSource by Dependency Injection.

    Secondly, your DAO should simply let DataAccessException propagate or should catch it and rethrow an exception. You're catching and swallowing the exception, which will mean there's no exception to be caught by the transaction infrastructure to prompt rollback.

    You can perform rollback explicitly by TransactionInterceptor.setRollbackOnly(), but it's normally better practice to let an exception cause rollback.

    Comment

    Working...
    X