From: Hiram C. <hch...@jb...> - 2002-11-16 04:50:57
|
First time post... I like hibernate. I want to use it more with the Aspect Framework that I'm build for JBoss. Anyways, here is a way a Jboss aspect could help simplify things: You would use an interceptor that had an invoke method that looked something like: class HibernateInterceptor { ... public Object invoke(AspectInvocation invocation) throws Throwable { HibernateContext oldHC = getHibernateContext(); HibernateContext hc = null; if( oldHC==null || oldHC.sessionFactory != sessionFactory ) { hc = new HibernateContext(); hc.sessionFactory = sessionFactory; hc.session = sessionFactory.openSession(); hc.transaction = hc.session.beginTransaction(); setHibernateContext(hc); } try { Object rc = invocation.invokeNext(); if( hc != null ) hc.transaction.commit(); return rc; } catch ( Throwable e ) { if( hc != null ) hc.transaction.rollback(); throw e; } finally { if( hc != null ) hc.session.close(); setHibernateContext(oldHC); } } .. } class RuleListAction implemetns IRuleListAction { .. public String doDelete() throws Exception { Session session= HibernateInterceptor.getSession(); RuleList mo = (RuleList) session.load( RuleList.class, id ); session.delete(mo); return SUCCESS; } .. } so that if you have DynamicProxy x with the HibernateInterceptor in the invocation chain then you could just do a: IRuleListAction x = (IRuleListAction )AspectFactory(new RuleListAction()); x.doDelete() Regards, Hiram > -----Original Message----- > From: hib...@li... > [mailto:hib...@li...]On Behalf Of Urberg, > John > Sent: Friday, November 15, 2002 8:30 AM > To: 'Jozsa Kristof' > Cc: hib...@li... > Subject: RE: [Hibernate] Using hibernate - best practices > > > >> 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 > hib...@li... > https://lists.sourceforge.net/lists/listinfo/hibernate-devel |