Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: call service method wrapped in transaction from another one?

  1. #1
    Join Date
    Oct 2004
    Location
    Palmas-TO, Brazil
    Posts
    92

    Default 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. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Oct 2004
    Location
    Palmas-TO, Brazil
    Posts
    92

    Default

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

    Thanks a lot!

    Gilberto

  4. #4
    Join Date
    Oct 2004
    Location
    Palmas-TO, Brazil
    Posts
    92

    Default

    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

  5. #5
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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?
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  6. #6
    Join Date
    Oct 2004
    Location
    Palmas-TO, Brazil
    Posts
    92

    Default

    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

  7. #7
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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).
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  8. #8
    Join Date
    Oct 2004
    Location
    Palmas-TO, Brazil
    Posts
    92

    Default

    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.

  9. #9
    Join Date
    Oct 2004
    Location
    Palmas-TO, Brazil
    Posts
    92

    Default

    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

  10. #10
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

Similar Threads

  1. Unit testing with JOTM and JtaTransactionManager
    By lalle in forum Architecture
    Replies: 1
    Last Post: Oct 15th, 2005, 09:05 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  5. Transaction pb Hibernate/MySQL
    By syluser in forum Data
    Replies: 2
    Last Post: Aug 28th, 2004, 02:40 PM

Posting Permissions

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