Results 1 to 2 of 2

Thread: Hi problem during declarative transaction

  1. #1
    Join Date
    Aug 2004
    Location
    Bangalore,India
    Posts
    7

    Default 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. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    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.

Similar Threads

  1. Unit testing with JOTM and JtaTransactionManager
    By lalle in forum Architecture
    Replies: 1
    Last Post: Oct 15th, 2005, 09:05 AM
  2. Replies: 0
    Last Post: Jun 6th, 2005, 06:22 AM
  3. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  4. Transaction pb Hibernate/MySQL
    By syluser in forum Data
    Replies: 2
    Last Post: Aug 28th, 2004, 02:40 PM
  5. Replies: 7
    Last Post: Aug 20th, 2004, 08:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •