>> The InvoiceRepository would be implemented like this:
>> [code to load invoices]

> And where do you close those opened sessions using this design? Also,
where
> did you find a place to implement transaction handling, including joining
> into existing transactions if possible?

That's the responsibility of the database object.  Code will usually look
something like this:
  
   SomeRepository someRepository = database.getRepository(...);
   AnotherRepository anotherRepository = database.getRepository(...);
   database.startTrans();
   try {
      someRepository.update(object);
      anotherRepository.update(object);
      database.commit();
   } catch (Exception e) {
      database.rollback();
   }

All the code for dealing with sessions and transactions is safely tucked
away in the database implementation.

> These piece of codes are repeating in almost ALL of the Service classes'
> business methods, are just flooding all the logic badly :( 

I'm not sure how you get around the whole transaction thing unless you're in
a J2EE server that does that automatically.  If you want to get rid of the
boilerplate code, you could do something like this:

  public class Database {
    <...>
    public void inTransactionDo(Runnable doRun) {
      Session session = null;
      Transaction tx = null;
      try {
          session = Hibernator.getSession();
          tx = session.beginTransaction();
        doRun.run();
          tx.commit();
      } catch (Exception e) {
          logger.error (<blah/>), e);
          tx.rollback();
      } finally {
           if (session != null) session.close();
     }
     <...>
  } 

Then your business methods would look like this:

  database.inTransactionDo(new Runnable() {
    public void run() {
     <...business logic here...>
    } 
  }

Regards,
John Urberg


-------------------------------------------------------
This sf.net email is sponsored by: To learn the basics of securing 
your web site with SSL, click here to get a FREE TRIAL of a Thawte 
Server Certificate: http://www.gothawte.com/rd524.html
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to