-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: transactions with service methods in hibernate
PostPosted: Wed Nov 23, 2005 1:04 pm 
Beginner
Beginner

Joined: Wed Nov 23, 2005 12:55 pm
Posts: 23
Hey,

I'm wondering if I'm doing it right. I always build my applications with service layers. So in front I have my struts layer that communicates with my service layer. My service layer communicates with my DAO layer.

Well know, I manage transactions at the service layer. Because that's where my business logic is. Now before I used hibernate I always passed my connection to my interfaces of my DAO. For hibernate this has off course no value.

Now I created a solution using an ITransaction, this has 4 methods, begin,rollback,commit and end. In the hibernate implementation I close my session in the end. This way I don't have any Hibernate references in my service layer.

So in my service layer for every business process I do begin,commit or rollback and end. All in the try,catch,finally of these methods.

Okay I know if I'm gonna mix transactions between hibernate and another ORM or DAO framework it's gonna fail, but this way my service layer is clean of Hibernate references.

I know I could use AOP for this, but most clients I have don't like AOP to much yet, it's to advanced for them.

My actual question is this a good pattern or more like an antipattern?


Top
 Profile  
 
 Post subject: already read demarcation
PostPosted: Wed Nov 23, 2005 1:05 pm 
Beginner
Beginner

Joined: Wed Nov 23, 2005 12:55 pm
Posts: 23
http://forum.hibernate.org/viewtopic.ph ... emarcation

I also wanted to note that I already read the hread, but It doesn't say anything about how to get rid of the references to hibernate in your service layer.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 2:41 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://hibernate.org/42.html
http://hibernate.org/43.html


Top
 Profile  
 
 Post subject: also had read that one :)
PostPosted: Wed Nov 23, 2005 7:45 pm 
Beginner
Beginner

Joined: Wed Nov 23, 2005 12:55 pm
Posts: 23
Hey Christian,

I've already read those 2 also. I search first the documentation, then the forum but not yet a real solution. I was just wondering if there was a solution/pattern where you could let your service handle your transactions without being dependent on hibernate.
ie.: if Hibernate 4 would come out that I only need to alter my DAO and ... but that I don't have to touch my service layer because that's where my business logic is in, and I don't want to alter to much in there?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 9:23 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I'm pretty sure that you should read both pages again. They describe exactly what you want to do.


Top
 Profile  
 
 Post subject: read them again
PostPosted: Thu Nov 24, 2005 3:02 am 
Beginner
Beginner

Joined: Wed Nov 23, 2005 12:55 pm
Posts: 23
Hey Christian,

I've read them again, but I still have the feeling they don't describe what I mean. In the 2 pages I never see a service layer that is not Hibernate aware, all the transaction code that is described in those 2 pages are hibernate wrappers around either JTA or JDBC, right? I just want to make sure that if I needed to switch back to JDBC (so no hibernate reference in the whole project, not single library), I'll be able to reuse all of my interfaces without altering them, only altering the implementation of my DAO layer.

I wanted a further extraction layer dependent on nothing but my own objects, the implementation of this layer on the other hand is off course hibernate aware.

Im pretty sure with this interface
Code:
public interface ITransaction {
   
   public boolean begin() throws DAOException;
   public boolean commit()  throws DAOException;
   public boolean rollback() throws DAOException;
   public void end() throws DAOException;

}

It will work.

So a methodn in one of my services looks like this now

Code:
public CustomerServiceImpl(ICustomerDAO customerDao,int numLoginAttempts){
      this.customerDao = customerDao;
      this.defaultNumLoginAttempts = numLoginAttempts;
   }

public boolean saveCustomer(ICustomer customer) Throws DAOException{
      ITransaction transaction = null;
      boolean retVal = false;
      try {
         transaction = this.customerDao.getTransaction();
         transaction.begin();
         retVal = this.customerDao.saveCustomerAgent(transaction, agent);
         if (retVal) {
            transaction.commit();
         } else {
            transaction.rollback();
         }
      } catch (Exception e) {
         this._logger.error("saveCustomer " + e);
         try {
            transaction.rollback();
         } catch (Exception e2) {
            this._logger.error("saveCustomer " + e2);
         }
                        throw new DAOException(e);
      } finally {
         try {
            transaction.end();
         } catch (Exception e2) {
            this._logger.error("saveCustomer " + e2);
         }
      }
      return retVal;
   }


If one of the proposed solutions one of these pages actually does this could you tell me which one? I've read them all and as far as I can see they all force you to make your service layer hibernate transaction aware.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.