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

  • TransactionProxyFactoryBean

    Hi,

    I'm using Spring's TransactionProxyFactoryBean to manage a Hibernate DAO in the manner suggested in the reference documentation


    Code:
    <bean id="myDAO" class="foo.bar.MyDAOImpl">
      <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    
    <bean id="proxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    
      <property name="transactionManager"><ref local="transactionManager"/></property>
      <property name="target"><ref local="myDAO"/></property>
      <property name="transactionAttributes">
        <props>
          <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>	
        </props>
      </property>
    </bean>

    My question is simply this: when I want to use the DAO object to access some data, presumably it is the "proxy" bean which I should retrieve (from the application context)?

    If I retrieve the myDAO bean instead, then presumably the operations will not be wrapped in transactions?

    Thanks in Advance,
    DM

  • #2
    That is correct.

    And that is also why I always (before Spring 2.0 AOP features) tended to define my targets as inner beans. This way we can only retrieve the transactional ones.

    Code:
    <bean id="proxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <property name="transactionManager"><ref local="transactionManager"/></property>
      <property name="target">
        <bean class="foo.bar.MyDAOImpl">
            <property name="sessionFactory"><ref local="sessionFactory"/></property>
        </bean>
      </property>
      <property name="transactionAttributes">
        <props>
          <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>	
        </props>
      </property>
    </bean>

    Comment


    • #3
      You are correct in your assessment of the proxied bean behavior versus the non-proxied bean behavior.

      The proxied bean will wrap the configured method calls in a transaction and then forward the calls to the DAO. The proxied bean will simply foward the non-configured method calls to the DAO.

      If you simply retrieve the non-proxied DAO, then no Spring-managed transactions will be utilized.

      Comment


      • #4
        Thanks very much for your replies! I have a couple of follow-up questions:

        Assuming you have the "transactionAttributes" property of the proxy configured like this:

        Code:
        <property name="transactionAttributes">
            <props>
                <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>	
                <prop key="update*">PROPAGATION_REQUIRED</prop>	
            </props>
        </property>
        Say I have a DAO method:

        Code:
        void updateCustomer(long id, String newName) {
        
          Customer customer = loadCustomer(id);
          customer.setName(newName);
        
          // getHibernateTemplate.update(customer);
        }
        1. Presumably changes to the Customer will be committed because this method matches "update*", even though the first thing it does is call a method matching "load*" ?

        2. (i) Is it necessary to include the line that is commented-out in order to ensure that the modification is persisted, or will it happen "automatically" without this?

        (ii) More generally, assuming the "transactionAttributes" are specified properly, is it ever necessary to use the HibernateTemplate when updating objects?

        Thanks Again,
        DM
        Last edited by domurtag; Oct 17th, 2006, 10:34 AM.

        Comment


        • #5
          1. Presumably changes to the Customer will be committed because this method matches "update*", even though the first thing it does is call a method matching "load*" ?
          Yes. Infact load* will not be interecepted when it is being called from within updateXXX.

          2. (i) Is it necessary to include the line that is commented-out in order to ensure that the modification is persisted, or will it happen "automatically" without this?
          Thats a hibernate question. Hibernate usually syncs the changes in the object model to the DB if the object was retrieved from within the same session.

          (ii) More generally, assuming the "transactionAttributes" are specified properly, is it ever necessary to use the HibernateTemplate when updating objects?
          Yes assuming you retrieved the objects in the same session and modified them, you would not have to call hibernate template's update. But hibernate template is useful when you are detaching and reattaching objects (a workaround for lack of long running transactions spanning multiple requests).

          Comment

          Working...
          X