2 Replies Latest reply: Feb 24, 2012 5:10 AM by 583687 RSS

    Toplink: Unable to get proper instanceof UnitofWork in SessionEventListener

    583687
      Hello,

      I have created a custom UnitofWork class by extending UnitofWorkImpl class. I am using this class for all transaction management. I have defined session listener. When I am trying to get the UnitofWork from the SessionEvent object in postBeginTransaction method, if is not giving me the instanceof my UnitofWork class using SessionEvent object.

      ------
      public MyUnitofWork extends UnitofWorkImpl {
      public MyUnitofWork( AbstractSession parentSession ) {
      super( parentSession );
      }
      }

      ------
      public void myMethod() {

      AbstractSession absSession = (AbstractSession)getSession();
      MyUnitofWork unitOfWork = new MyUnitofWork (absSession);
      unitOfWork.beginEarlyTransaction();

      }
      ------
      public EventListener implements SessionEventListener {

      public void postBeginTransaction(SessionEvent event) {

      UnitofWork uow;
      if(event.getSession().isUnitOfWork()){
      uow = (UnitofWork )event.getSession();
      } else if (event.getSession().isClientSession() {
      uow = ((ClientSession )event.getSession()).aquireUnitofWork();
      }

      if ( uow instanceof MyUnitofWork ) {
      -- My logic
      System.out.println("MyUnitofWork");
      }
      }

      }

      public void postCommitTransaction(SessionEvent event) {

      if(event.getSession().isUnitOfWork()){
      uow = (UnitofWork )event.getSession();
      } else if (event.getSession().isClientSession() {
      uow = ((ClientSession )event.getSession()).aquireUnitofWork();
      }

      if ( uow instanceof MyUnitofWork ) {
      -- My logic
      System.out.println("MyUnitofWork");
      }
      }
      }

      }
      ------
      My code is something like shown above. But it never go inside the if loop of MyUnitofWork of postBeginTransaction(). But there is very strange behavior in preCommitTransaction() method. In this method my logic executes properly.
      If I call explicit call to postBeginTransaction() method after unitOfWork.beginEarlyTransaction() using EventManager, postBeginTransaction() method logic works properly. In this case SessionEventListener method get called twice.

      unitOfWork.getEventManager().postBeginTransaction();

      Any Idea why this could be happening?
        • 1. Re: Toplink: Unable to get proper instanceof UnitofWork in SessionEventListener
          JamesSutherland
          The issue is in your code,

          if(event.getSession().isUnitOfWork()){
          uow = (UnitofWork )event.getSession();
          } else if (event.getSession().isClientSession() {
          uow = ((ClientSession )event.getSession()).aquireUnitofWork();
          }

          The event's session is the ClientSession, so you are creating a new UnitOfWork.

          The transaction events are always called on the ClientSession, as it controls transactions. If you need the active unit of work, you would need to store it in a property of the ClientSession, or in a ThreadLocal.
          • 2. Re: Toplink: Unable to get proper instanceof UnitofWork in SessionEventListener
            583687
            Thanks for your clarification.

            Instead of keeping the ClientSession object instance or ThreadLocal variable, I choose to call the below method to make it work,

            AbstractSession absSession = (AbstractSession)getSession();
            MyUnitofWork unitOfWork = new MyUnitofWork (absSession);
            unitOfWork.beginEarlyTransaction();
            unitOfWork .getEventManager().postBeginTransaction();

            The impact of this change is, EventSessionListener.postBeginTransaction() method get called 2 times. Below is the code of my Listener

            public EventListener implements SessionEventListener {

            public void postBeginTransaction(SessionEvent event) {
            if (event.getSession().isUnitOfWork() && event.getSession() instanceof MyUnitofWork ) {
            --Logic
            }
            }

            }

            But I am not sure why it work properly in EventSessionListener.postCommitTransaction() method.

            Any way I am able to get what I wanted.