Results 1 to 4 of 4

Thread: object committed in service layer - no call to dao?

  1. #1
    Join Date
    Sep 2004
    Posts
    2

    Default object committed in service layer - no call to dao?

    Hello All,

    I am a little confused as to why my objects are being saved from the service layer, without a call to my dao layer.

    In a method in my service layer I receive my object that I am going to update by:
    Code:
    EntityGroup group = (EntityGroup) dao.getEntityGroup();
    Then I make some changes to the group object.
    At the end of the method, I make sure that the object is "safe" for saving. IE, the user did not remove things from the group that should be there, basically a validation. If the validation fails, I throw a custom Exception.

    *Note: I found out about the transactionAttribue using something like
    Code:
    PROPAGATION_REQUIRED,-CustomException
    which works great for rolling back the transaction, but lets say we don't use this*

    However, even without this, I am not calling my dao.saveEntityGroup() method, so I would not think that the group object would be saved. Why is this object being flushed without a specific call to my dao layer?

    I hope I explained this well enough.

    Thanks in advance!

    Chris

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    Sounds like you're using a persistence tool like Hibernate or a JDO implementation that supports automatic dirty checking. You don't need to tell it your objects are dirty and they need saving: it knows which objects are dirty and saves them automatically. Why don't you want this behaviour? Or do you just want to understand why it's happening?
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Sep 2004
    Posts
    2

    Default Hibernate

    Did I really write all that and not mention that I am using hibernate?
    Sheesh..

    So, yes exactly, I guess I wasn't aware that hibernate would do this. I would love to know why this is happening. Does this only happen if I change the object within the service layer (within a Spring transaction)? How/where do I configure this functionality?

    Thanks again,

    Chris

  4. #4
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    Hibernate will track changes to any object that is obtained from and still attached to a live Session, and write it out when that Session is closed or the transaction is done.

    This is basically the same way JDO would work too.

    If you do not want the object saved, you are going to have to cause the transaction to roll back. As you figuredout, this means registering custom exception types, or relying on the default treatment of RuntimeException derived exceptions to cause rollback.

    If you really want to use programmatic means to set the current tx to rollback-only, ala EJB, if you are using TransactionInterceptor (ie using the declarative transaction proxies, not TransactionTemplate), you can use:

    Code:
    TransactionInterceptor.currentTransactionStatus().setRollbackOnly()
    while an approach that will work for both cases is:

    Code:
     public static void setCurrentTransactionRollbackOnly(PlatformTransactionManager ptm) throws
    TransactionException {
      TransactionDefinition definition = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY);
      TransactionStatus status = ptm.getTransaction(definition);
      ptm.rollback(status);
     }
    where ptm is the PlatformTransactionManafer instance you are using, i.e. JTATransactionManager or HibernateTransactionManager.

Similar Threads

  1. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  2. User information in the service layer
    By luxaeterna in forum Architecture
    Replies: 5
    Last Post: Feb 24th, 2005, 11:14 PM
  3. Replies: 0
    Last Post: Jan 6th, 2005, 08:19 AM
  4. Creating a proper service layer?
    By infectedrhythms in forum Architecture
    Replies: 5
    Last Post: Jan 5th, 2005, 06:32 PM
  5. Replies: 2
    Last Post: Sep 29th, 2004, 01:38 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
  •