-->
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.  [ 14 posts ] 
Author Message
 Post subject: Hibernate DAOs Standalone vs. J2EE Environments
PostPosted: Fri Aug 19, 2005 2:47 pm 
Newbie

Joined: Thu Aug 18, 2005 5:39 pm
Posts: 8
Hello all,

So far as I can tell, it appears you need to write two different implementations of a DAO if you intend on using Hibernate. The root of the problem seems to rely on the fact that Hibernate requires "Database transaction demarcation" to work its magic.

Link: http://www.hibernate.org/hib_docs/v3/reference/en/html/transactions.html#transactions-demarcation


The Hibernate docs state that when implementing a DAO in an unmanaged (i.e. standalone) environment you should utilize hibernates internal transaction management system (org.hibernate.transaction.JDBCTransactionFactory).

However what I am unclear about is can I and more to the point, SHOULD I use this transaction demarcation method in a managed environment as well. If so what are the pros and cons between this style and using JTA as the transaction manager/demarcation mechanism.

Thanks in Advance,

Richard Wareing
[/url]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 3:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
In a managed env, just use CMT together with SessionFactory.getCurrentSession()


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 3:40 pm 
Newbie

Joined: Thu Aug 18, 2005 5:39 pm
Posts: 8
gavin wrote:
In a managed env, just use CMT together with SessionFactory.getCurrentSession()


Thanks for the quick reply. Does this mean I would have to do two DAO implementations one for stanalone and one for CMT? I was trying to avoid this :).

Rich...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 3:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Certainly NOT. DAOs do not do transaction/session management!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 4:01 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
What are the chances of getting something like this that doesn't use getCurrentSession() ?

http://cvs.sourceforge.net/viewcvs.py/h ... &view=auto


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 4:11 pm 
Newbie

Joined: Thu Aug 18, 2005 5:39 pm
Posts: 8
gavin wrote:
Certainly NOT. DAOs do not do transaction/session management!


But isn't that the reason you need to implement two DAO implementations? i.e. in a managed (JTA) environment your dao code would look something like this:

public interface StuffDAO {
public Stuff getStuff( String name );
}

public class CMTStuffDAOImpl implements StuffDAO {

public Stuff getStuff( String name ) {
Session sess = factory.getCurrentSession();
// code to get stuff....
} // method

} // class


Where as in unmanaged environment it would look like this:

public class StandAloneStuffDAOImpl implements StuffDAO {

public Stuff getStuff( String name ) {
Session sess = factory.openSession();
Transaction tx = null;
try {
tx = sess.beginTransaction();
// code to get stuff....
tx.commit();
} catch (RuntimeException e) {
if (tx != null) tx.rollback();
throw e; // or display error message
} finally {
sess.close();
} // try
} // method
} // class

Rich...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 20, 2005 8:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Really the DAOs should not even be opening the hibernate sessions themselves. Typically this should be a higher-level function; the DAOs then just look up the session from a "well known place". Think about two DAOs needing to interact within the same logical unit-of-work...

A nice strategy for this is to use proxies for wrapping the things coordinating access to the DAOs. The proxy would handle things like session and transaction management.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 21, 2005 9:38 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
public class StandAloneStuffDAOImpl implements StuffDAO {

public Stuff getStuff( String name ) {
Session sess = currentSession();//a typical use case for thread local
// code to get stuff....

} // method
} // class


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 11:37 am 
Newbie

Joined: Thu Aug 18, 2005 5:39 pm
Posts: 8
steve wrote:
Really the DAOs should not even be opening the hibernate sessions themselves. Typically this should be a higher-level function; the DAOs then just look up the session from a "well known place". Think about two DAOs needing to interact within the same logical unit-of-work...

A nice strategy for this is to use proxies for wrapping the things coordinating access to the DAOs. The proxy would handle things like session and transaction management.


Ok so let me summarize what I've understood so far:

1. CMT Environments - Get the session via the factory.getCurrentSession() method (i.e. let JTA manage things and demarcate the transaction boundaries).

2. Standalone Environments - Use a HibernateUtil/ThreadLocal pattern to externally store the current session in a "well known" place, and have your DAOs retrieve the current session from there or alternately a proxy. This insulates you from the very likely scenario of the "unit of work" not being complete in any particular DAO method.

3. For an application that requires a standalone server in addition to a application server component, you need two DAO implementations: one for the standalone server (implemented ala #2) and one for the application server (implemented ala #1).

Do I have all this right? :) Also, could you point me to any place where I can RTFM on this proxy you speak of?


Thanks in Advance,

Richard...

P.S. As usual you guys have been of great help, thanks for all your comments!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 12:04 pm 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
With Spring you can have one implementation and two xml's. In our application we using this approach - the same code is working in 3-tier and 2-tier versions.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 12:19 pm 
Newbie

Joined: Thu Aug 18, 2005 5:39 pm
Posts: 8
sergeya wrote:
With Spring you can have one implementation and two xml's. In our application we using this approach - the same code is working in 3-tier and 2-tier versions.


Well I'm trying to get away with not using Spring at all in the standalone portion of the project. So I'm not sure that would work :).

It sounds like to really accomplish what I'd like (only one DAO implementation) I'd need to use a JTA based transaction manager and wire it up to Hibernate some how. That way when from inside the Hibernate DAO implementation the factory.getCurrentSession() method is called it should work.

However it sounds like doing this would be alot more grief than it's worth (plus I can't find documentation on this anywhere), so I'm now investigating the suggested "proxy" method, and if I can't figure that out I'll resort to the HibernateUtil strategy.

Richard...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 1:25 pm 
Newbie

Joined: Tue Oct 11, 2005 5:55 am
Posts: 8
hello all,

i am also currently trying to combine christians "Generic DAO pattern with
JDK 5.0" with a standalone application. since the usage of servlet-filters
isn't possible and because i would also like to avoid using spring, the proxy
method mentioned before seems to be the only other possibility.

can anyone give me a quick explanation of how this would be implemented?

that would be a great help!

thanks in advance,
nick


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 1:46 pm 
Hibernate Team
Hibernate Team

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 3:14 pm 
Newbie

Joined: Tue Oct 11, 2005 5:55 am
Posts: 8
thanks for the link christian,

but i already read those pages and still don't quite get it! i know you have
answered so many similar questions bevor and i don't want to annoy you
with this!

i am using hibernate 3.1rc3, so that i can set the "current_session_context_class"
configuration option to "thread" in order to use the ThreadLocal feature.

i do understand, that this creates a new session if none is available, but
how and where do i write the transaction demarcation code as well as the
code for closing the session, if this doesn't belong into the DAOs.

i need something between my application code and the DAOs to decouple the
application from the hibernate framework and the transaction code altogether.
one solution mentioned would be AOP, which i would like to avoid.

the "proxy" method sounds promising! has anybody done this and could give
me (a confused newbie) a quick explanation...

thanks,
nick


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