Announcement Announcement Module
Collapse
No announcement yet.
@Transactional not picked up Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Transactional not picked up

    Hi,

    The @Transactional annotation does not trigger anything when called from "this' as shown here:

    Code:
    public class MyService {
    
      protected MyDAO myDAO;
    
      public MyDAO getMyDAO() {
        return myDAO;
      }
    
      public void setMyDAO(MyDAO myDAO) {
        this.myDAO = myDAO;
      }
    
      public void doIndirect() {
        // Transaction not initiated
        doDirect();
      }
    
      @Transactional
      public void doDirect() {
        // Transaction gets initiated
        MyEntity myentity = new MyEntity();
        myDAO.insert(myentity);
      }
    
      public static void main(String[] args) {
        String[] configs = new String[] { "classpath:applicationContext.xml" };
    
        BeanFactory ctx = new ClassPathXmlApplicationContext(configs);
        final MyService service = (MyService) ctx.getBean("myService");
        service.doIndirect();
        service.doDirect();
      }
    }
    In one case, it works, but not on the other. Can anyone give me a clue about what I am doing wrong?

    This seems like an AOP issue to me more than a spring data access problem. Let me know if I should transfer to another forum. Also, I could paste the remaining of the related files if needed.

    Update: I simplified the code from its original version.

    --
    Claude
    Last edited by claude; Jul 25th, 2007, 05:36 PM. Reason: Simplified the code to remove

  • #2
    Hm... I guess the answer is that the call to doDirect() from doIndirect() is done using "this", and not the spring proxy around the service, right? If it is the case, could I be able to achieve what I want, i.e. obtaining a new transaction context around the doDirect() even when called from doIndirect?
    Last edited by claude; Jul 25th, 2007, 06:38 PM. Reason: Adapted to the changes in code of the original post

    Comment


    • #3
      found the answer at: http://forum.springframework.org/showthread.php?t=26021

      Comment


      • #4
        The 'problem' is that Spring AOP works with Proxy's instead of actual weaving the code with a transactional context. If you call this.doDirect() (this is implicitely there) from a non-transactional method, you are just calling the method on the object itself, and not on the proxy who is providing the transactional context.

        If you want to call the method on the proxy, you need to retrieve the proxy from the AOP context:
        Code:
        AopContext.getCurrentProxy();
        and than call the method you want (in a transactional context)

        There is a nice blog entry about the Proxy concept in Spring at Debunking myths Proxies impact performance

        Comment


        • #5
          Thanks, and searching for how to use currentProxy, I found that good examples are in fact in the spring docs at
          http://www.springframework.org/docs/...-proxying-intf

          and

          http://www.springframework.org/docs/...proxying-class

          --
          Claude

          Comment

          Working...
          X