Announcement Announcement Module
Collapse
No announcement yet.
@Transactional on non-interface methods Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Transactional on non-interface methods

    Hi,
    We are using Spring with iBATIS.

    Requirement:
    We have two entities with parent, child relationship. We have an interface and implementation to create these entities. Following is the code:

    Interface:
    public interface SampleExecutor {

    public void execute(String parentDescription, String childDescription) throws Exception;

    }

    Implementation:
    public class SampleExecutorImpl implements SampleExecutor {
    public void execute(String parentDescription, String childDescription) throws Exception {
    Long parentId = createParent(parentDescription);

    createChild(childDescription, parentId);
    }

    public void createParent(String parentDescription) {
    // Create the parent entity
    }

    @Transactional(rollbackFor = Exception.class)
    public void createChild(String childDescription, Long parentId) {
    // Create the child entity
    // Step 1
    // Step 2
    }
    }

    transactions.xml:
    <beans>
    <bean
    class="org.springframework.aop.framework.autoproxy .DefaultAdvisorAutoProxyCreator">
    <property name="proxyTargetClass">
    <value>true</value>
    </property>
    </bean>

    <bean id="transactionInterceptor"
    class="org.springframework.transaction.interceptor .TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributeSource">
    <bean
    class="org.springframework.transaction.annotation. AnnotationTransactionAttributeSource" />
    </property>
    </bean>

    <bean
    class="org.springframework.transaction.interceptor .TransactionAttributeSourceAdvisor">
    <constructor-arg index="0" ref="transactionInterceptor" />
    </bean>
    </beans>

    And say, we have a requirement to create the parent entity irrespective of whether the creation of child succeds or fails. And any exception in creation of child entity should rollback all the steps in 'createChild' method.

    For the requirements, stated above, when I use @Transactional only for createChild method, I expected it to rollback the child entity. But interestingly, it doesnt rollback.

    Then, I thought, it could be because of unavailability of the createChild method in the interface and a call from the external component is on execute. So, I started using CGLib. But still it didnt work.


    Can anyone help? Let me know if you need more details.

    Thanks,
    Javvaji Ramesh

  • #2
    Have a read of this chapter.
    When calling a proxied method from inside a method in the same class, the call is made to the unproxied method.

    Comment


    • #3
      Hi Andrei,
      I understood how it works. But, can you suggest any workaround to fulfil the requirement?

      Thanks,
      Javvaji Ramesh

      Comment


      • #4
        I'd move execute() method to another level.

        Comment

        Working...
        X