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

  • Hi problem during declarative transaction

    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();
    name =(String) jt.queryForObject(
    "select name from retiredb where Id=1",
    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 id="transactionManager" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
    <property name="dataSource"><ref local="dataSource"/></property>

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

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


  • #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.