Announcement Announcement Module
Collapse
No announcement yet.
Problem with using TransactionProxyFactoryBean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with using TransactionProxyFactoryBean

    Hi,
    I am trying to use TransactionProxyFacotryBean to implement declarative transaction management, however I noticed that my inserts get executed and committed as soon as the jdbc statement is executed via the jdbc template. I am newbie and am not sure what I am doing wrong. Even when my target class throws an exception the data still shows up in the database.
    Thanks,
    Sohil
    The configuration file is below..

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName">
    <value>oracle.jdbc.driver.OracleDriver</value>
    </property>
    <property name="url">
    <value>jdbc:oracle:thin:@SERVER:1521:INSTANCE</value>
    </property>
    <property name="username"><value>USERNAME</value></property>
    <property name="password"><value>PASSWORD</value></property>
    </bean>

    <bean id="txdataSource" class="org.springframework.jdbc.datasource.Transac tionAwareDataSourceProxy">
    <constructor-arg><ref bean="dataSource"/></constructor-arg>
    </bean>

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

    <!-- proxy calls to the manager to automatically manage the transaction -->
    <!-- TODO extend and and add property for commit strategy -->
    <bean id="sampleManagerTransactionProxy" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="proxyTargetClass"><value>true</value></property>
    <property name="transactionManager">
    <ref bean="transactionManager"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="*">PROPAGATION_REQUIRED,-TestException</prop>
    </props>
    </property>
    <property name="target"><ref local="sampleMainManager"/></property>
    </bean>

    <!-- main manager responsible for staging and processing -->
    <bean id="sampleMainManager" class="manager.SampleMainManager">
    <property name="ds"><ref local="txdataSource"/></property>
    </bean>

    and the java code does the following. I first tried supplying the datasource directly to the samplemainmanager and then using the transactionAwareDataSourceProxy but neither works. I execute the process method on the SampleMainManager after obtaining an instance from the ApplicationContext.

    public class SampleMainManager
    {
    TransactionAwareDataSourceProxy ds;
    //DataSource ds;

    public void setDs (TransactionAwareDataSourceProxy ds)
    {
    this.ds = ds;
    }


    public void process () throws Exception
    {
    try{
    if(ds != null){
    JdbcTemplate jt = new JdbcTemplate();
    jt.setDataSource(ds);
    jt.execute("insert into temp (id) values(2)");


    }else{
    System.out.println("datasource is null");
    }
    }catch(Exception e){
    e.printStackTrace();
    throw e;
    }
    }

    }

  • #2
    I execute the process method on the SampleMainManager after obtaining an instance from the ApplicationContext.
    Try extracting the bean sampleManagerTransactionProxy and trying the same thing (you shouldn't need the txdataSource).

    Comment


    • #3
      It works!!

      Thanks, I never realised from the documentation that I should be using the proxied bean and not the original. Is that the way to go since I didnt see anything pointing to that in the documentation.

      Comment


      • #4
        Is that the way to go since I didnt see anything pointing to that in the documentation.
        Yes. That's the way Spring's AOP works. It is able to advise/intercept your proxied object by means of the proxy.

        Comment


        • #5
          I never realised from the documentation that I should be using the proxied bean and not the original
          The following reference docs. sections may be useful:
          Spring AOP: Aspect Oriented Programming with Spring
          Declarative transaction management

          Also the samples with the distribution will make this clear.

          Comment

          Working...
          X