-->
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.  [ 3 posts ] 
Author Message
 Post subject: Another DAO pattern discussion, no springs
PostPosted: Fri Nov 07, 2003 11:19 am 
Newbie

Joined: Thu Oct 09, 2003 11:11 am
Posts: 5
I have been looking at the forum posts and working on developing a DAO pattern that works for me. Here are the requirements I have for what is a good DAO implementation:

1) The Data Access Layer (DAOs) must completely hide the EIS being used. If there are multiple datasources, hide this, and hide the technology used to load the POJOs (castor / toplink / hiberntae / etc).

2) EJB Session Beans should use CMT.

3) The DAL implementation should support being used in a fast lane implementation for the web tier (by pass EJBs) and should support easy Junit tests for the DAL.

Here is what I came up with:

class DAOFactory{
//singleton
private DAOFactory(){}
//getaDAO
public static PersonDAO getPersonDAO() throws DataAccessException{
return new HibernatePersonDAO(getHibernateSession());
}

public static Session getHibernateSession(){
//jndi lookup
}
}

Note that I do not work with the UserTransaction and set any thread local objects. We will see how this is handled in a second.

public class HibernatePersonDAO extends BaseHibernateDAO implements PersonDAO{
public HibernatePersonDAO(Session session){
super(session);
}

public Person savePerson(Person p) throws DataUpdateException{
try{
super.save(p);
super.flush();
}catch(HibernateException he){
throw new DataUpdateException(he);
}
}

//called when someone is finished with this DAO
public boolean close(){
try{
session.close();
}...
}
}

The reason for the close on the DAO was that I had no good way that I would know when to close the session(s) when in an EJB method.

e.g.

abstract class SomeSessionFacadeBean extends BaseSessionBean{

...
public Person updatePerson(Person p){
PersonDAO pdao = null;
try{
pdao = DAOFactory.getPersonDAO();
}catch(DataAccessException dae){
}catch(DataUpdateException due){
} finally{
pdao.close();
}
}

Now, where this is especially good for me is I can bypass the DAOFactory all together when writing Junit Tests for my HibernateDAOs, or (using the "fast lane" in web apps for that matter)..

public void setup()throws Exception{
theSession = getConfiguration().getSessionFactory().getSesseion();
Transaction tx = theSession.begin();
pDAO = new HibernatePersonDAO(theSession);
}
...
public void tearDown() throws Exception{
tx.rollback();
}

This is starting to work very nicely for me. Since DAO methods always flush - and the Sessions are all in the same transaction - they can see uncommited data from other sessions in the same transaction. I hope that having the extra sessions in a transaction will not be that expensive. For the most part - this rarely exceeds two or three DAOs / sessions for most use cases anyway.

(Notice that an especially complex DAO method might be able to create nested DAOs with the same session).

If worst comes to worse - I will code my own HibernateJCA Session Factory and thinly wrap Sessions. This is the correct place to put Thread Local instances of the Session any way - not in a Base EJB as many around have been talking about. I think this is wrong because it does not seem to support remote UserTransactions - which I use when unit testing the SessionFacade classes.

Post your comments, ideas, evaluations.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 07, 2003 1:54 pm 
Beginner
Beginner

Joined: Mon Sep 29, 2003 3:10 pm
Posts: 36
It looks like you've put some thought into this. One quick, somewhat unrelated, comment. if you put your code snippits within [code][/code] blocks, they'll be infinitely more readable.

-Watter


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 08, 2003 6:54 am 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
check out this post, lots of helpfull info.

http://forum.hibernate.org/viewtopic.ph ... hlight=dao


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.