Results 1 to 5 of 5

Thread: Problem with using TransactionProxyFactoryBean

  1. #1
    Join Date
    Oct 2005
    Posts
    3

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    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).

  3. #3
    Join Date
    Oct 2005
    Posts
    3

    Default 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.

  4. #4
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    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.

  5. #5
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    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.

Similar Threads

  1. Replies: 1
    Last Post: Jul 5th, 2005, 03:48 AM
  2. pagination and continuation problem in SWF
    By yfmoan in forum Web Flow
    Replies: 6
    Last Post: Jun 29th, 2005, 03:42 AM
  3. Replies: 0
    Last Post: Feb 16th, 2005, 01:45 PM
  4. Oracle Jdbc invalid url problem
    By jfuchs in forum Data
    Replies: 5
    Last Post: Nov 1st, 2004, 11:33 AM
  5. Lazy Load Problem when Doing UnitTest
    By yoshi in forum Data
    Replies: 7
    Last Post: Sep 29th, 2004, 10:00 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
  •