Results 1 to 7 of 7

Thread: Declarative transaction on sub-methods?

  1. #1
    Join Date
    Oct 2008
    Posts
    5

    Default 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. #2
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    Short answer - no. Longer answer here.

  3. #3
    Join Date
    Oct 2008
    Posts
    5

    Default

    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!

  4. #4
    Join Date
    Dec 2007
    Posts
    130

    Default

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

  5. #5
    Join Date
    Oct 2008
    Posts
    5

    Default

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

  6. #6
    Join Date
    Dec 2008
    Posts
    1

    Smile

    Quote 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!

  7. #7
    Join Date
    Dec 2008
    Posts
    15

    Default

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •