Announcement Announcement Module
Collapse
No announcement yet.
call service method wrapped in transaction from another one? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • call service method wrapped in transaction from another one?

    Hi, all!


    I didn't find any thread with this issue! So, if someone has answered this question, please excuse-me and point me to the right place!


    Here is the question?
    how to call a service method already wrapped in transaction from another one?

    The situation:
    The class MovementStockManger has this method
    public void processMovementStock(MovementStock movementStock) throws WithoutItemToProcessException;.

    And the class MovementSaleManger has this method
    public void processMovementSale(MovementSale movementSale) throws WithoutItemToProcessException;.

    So, inside the last one I do this call:
    Code:
    .
    .
    MovementStock movementStock = new MovementStock ();
    // set required fields
    movementStock .setMovementSaleID(movementSale.getMovementSaleID());
    movementStockManger.processMovementStock(movementStock);
    .
    .
    saveMovementSale (movementSale);
    But I'm getting strange errors in the view:
    Code:
    java.lang.NullPointerException
    	at org.appfuse.service.impl.MovementSaleManagerImpl.processMovementSale(MovementSaleManagerImpl.java:141)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:324)
    	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:296)

    Any idea how to solve this issue?
    Gilberto

  • #2
    I think your problem doesn't have anything to do with transactions. One of your classes is not initialized (at line 141 whatever that is) but taking a wild guess I would say movementSale object in your object is not initialized.

    You probably don't inject any instance if movementSale into MovementSaleManager. Check your configuration and do some logging to find out exactly what object is null.

    Comment


    • #3
      Oh!! :cry: I'm tired! Sometmes we didn't see the obvious!!

      Thanks a lot!

      Gilberto

      Comment


      • #4
        Costin, shouldn't it rollback the process, due to the exception?

        I may case it didn't! The exception was thrown manually this way:
        Code:
        if (movementSale.getMovementSaleItems().isEmpty())
            throw new WithoutItemToProcessException("Sale '" + movementSale.getMovementSaleID() + "' without items to process!");
        Could you explain better this behavior?

        Gilberto

        Comment


        • #5
          If your methods are market as transactional the exception should and will rollback your transaction.
          Unfortunatelly I don't follow you - I see you are throwing an exception manually - what proof do you have that the transaction is not rolled back?
          Do a simple test, write something inside the db and then throw an exception and check the database afterwards.

          ...or am I missing your point?

          Comment


          • #6
            what proof do you have that the transaction is not rolled back?

            After the exception, my movementSale table was changed.

            But after some reading, specially on transaction topic, I understood that I should put the exceptions to be thrown in my service layer, in the spring config file:
            Code:
            <prop key="process*">PROPAGATION_REQUIRED, -Exception</prop>
            And now it is functioning! Just other issues has happened that I would like to understand, like hibernate automatically saving a item that I didn't ask for it!

            Thanks, Costin.

            Gilberto

            Comment


            • #7
              Just other issues has happened that I would like to understand, like hibernate automatically saving a item that I didn't ask for it!
              If you object is attached to the session, if you modify the object the changes will propagate to the db. In this case you should either detach the object (close the session) or control the way the session is flushed - disabling flushing or doing it manually (FlushMode.COMMIT).
              However there are fundamental issues with HB and I recommend to take time and read the official documentation, forums, try the examples and also Hibernate In Action (which explains at great lenght the problems and solutions with O/RM in general not just HB).

              Comment


              • #8
                However there are fundamental issues with HB and I recommend to take time and read the official documentation, forums, try the examples and also Hibernate In Action (which explains at great lenght the problems and solutions with O/RM in general not just HB).
                Yes! I agree with you. I'm just reading it right now!
                One correction, it is not only saving but creating a new object and saving it automatically. The unique way I found to trace this problem was putting a not-null constraint directly on the table. Thus I did catch the expection, but it didn't show who is doing the operation.

                Comment


                • #9
                  After three days and a lot of hibernate DEBUG, I finally found the problem!

                  For some reason this code (below), which I thought it would be good for lookup search, was making hibernate not only saving but creating a new object and saving it automatically.

                  Code:
                    /**
                       * @hibernate.property column="cd_item"
                       */
                      public Integer getCdItem&#40;&#41; &#123; return cdItem; &#125;
                  
                      public void setCdItem&#40;Integer cdItem&#41; &#123; this.cdItem = cdItem;&#125;
                  
                      /**
                       * @hibernate.many-to-one
                       *  insert="false" update="false" cascade="none" column="cd_item"
                       *  outer-join="true"
                       */
                      public Item getItem&#40;&#41; &#123; return this.item;&#125;
                  
                      public void setItem&#40;Item item&#41; &#123; this.item = item; &#125;
                  */
                  hibernate version=2.1.7
                  Is it a bug or as you said ...or am I missing any point?
                  Gilberto

                  Comment


                  • #10
                    I don't see much from your code (you just placed some setters and getters and metadata for xdoclet - no HB code) but from your explanation it seems that you create objects and expect them to be saved.
                    If you are using Spring to do the DI then your objects (used by the setters/getters) are new - using them in a HB session will save(add) them and not update them.
                    Read the HB documentation about how HB considers an object new or not - mainly it's about the id field and versioning/timestamp (if you have any) and depending on your strategy.

                    I recommend you read the HB documentation and log your objects before saving - this way you'll see the properties and figure out what's going on.

                    Comment


                    • #11
                      I don't see much from your code (you just placed some setters and getters and metadata for xdoclet - no HB code)
                      If I remove the cascade="none" hibernate tag it doesn't create that one object.
                      but from your explanation it seems that you create objects and expect them to be saved.
                      No. I just get one existent and try to change one property of it.

                      If you are using Spring to do the DI then your objects (used by the setters/getters) are new - using them in a HB session will save(add) them and not update them.
                      Yes I'm using it! I'm using the very good appfuse-springmvc project.

                      Thanks again for the help.

                      Gilberto

                      Comment

                      Working...
                      X