Announcement Announcement Module
Collapse
No announcement yet.
Declarative transaction on sub-methods? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Declarative transaction on sub-methods?

    Hi,

    I am using declarative transaction management on methods starting with save, delete, etc. Code snippet is as follows:


    <bean id="txTemplate" abstract="true" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager" ref="txManager" />
    <property name="transactionAttributes">
    <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    ....

    <bean id="appFacade" parent="txTemplate">
    <property name="target">
    <bean id="appFacadeImpl" class="test.facade.AppFacadeImpl" />
    </property>
    </bean>

    As per the above code, transaction will be invoked for methods starting with save and delete in AppFacadeImpl.

    Suppose, in AppFacadeImpl I have a dummy method create* which calls the save* method inside it. save* method executes few database operations.

    If I try to invoke the create* method of AppFacadeImpl instead of save* method, I am getting an exception 'Session is closed' when i try to fetch a data from the database. Will declarative transaction won't start a transaction for sub methods?

  • #2
    Short answer - no. Longer answer here.

    Comment


    • #3
      I am not using AOP stuff in my code. Its simple declarative transaction management from the application context xml file. Any inputs would be of great help!

      Comment


      • #4
        Originally posted by ArumugamD View Post
        I am not using AOP stuff in my code. Its simple declarative transaction management from the application context xml file. Any inputs would be of great help!
        Declarative transaction management uses AOP proxies. The proxy intercepts the method calls to save* and delete*, and opens the transaction. The proxy only works if you call save* or delete* from outside the object, never if you make the call internally.

        Comment


        • #5
          Originally posted by Rober2D2 View Post
          Declarative transaction management uses AOP proxies. The proxy intercepts the method calls to save* and delete*, and opens the transaction. The proxy only works if you call save* or delete* from outside the object, never if you make the call internally.
          Pretty clear. Thanks a lot.

          Comment


          • #6
            Originally posted by Rober2D2 View Post
            Declarative transaction management uses AOP proxies. The proxy intercepts the method calls to save* and delete*, and opens the transaction. The proxy only works if you call save* or delete* from outside the object, never if you make the call internally.
            also a great help for me,thanks a lot!

            Comment


            • #7
              Actually, it is possible to wrap self-invocations with transactions. But I think it is only possible when using annotations. As the documentation states:

              Note: In proxy mode (which is the default), only 'external' method calls coming in through the proxy will be intercepted. This means that 'self-invocation', i.e. a method within the target object calling some other method of the target object, won't lead to an actual transaction at runtime even if the invoked method is marked with @Transactional!

              Consider the use of AspectJ mode (see below) if you expect self-invocations to be wrapped with transactions as well. In this case, there won't be a proxy in the first place; instead, the target class will be 'weaved' (i.e. its byte code will be modified) in order to turn @Transactional into runtime behavior on any kind of method.


              I am not sure if it is possible to mix declarative-based and annotation-based transaction management. If it is, and annotations overrides declarative-based, you could mix them to solve your problem.

              Regards,
              Vitor.

              Comment

              Working...
              X