Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Local, dao's and web patterns with Tomcat ...

 
Julio Compadre
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like to know what is the best practice in order to use hibernate in a web app with Tomcat as the servlet container.

For what I have read the best solution is to implement a pattern like the one suggested in Christian Bauer book using Thread Local to "store" the Session and this allows for a nice layer separation between actions and the DAO's.
(or, to be precise: no need to pass session to the dao's and get your code "un-layered").

Also for what I understood this allow for a very important Session per request strategy.

Am I being correct on this?

Is there a better way them this to implement Hibernate with Tomcat?

Regards,
Julio
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It kind of depends on your problem domain. Are you performing CRUD type operations? Reading large amounts of data? Do you need Transactions? There are some suggested patterns for solving various problem domains but you'll need to define what that problem domain is first
 
Julio Compadre
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for your reply.
The Web app is, I would say, relatively simple.
A site with two types of back-office for users to see their accounts, some trading forms with very specific functionality like file exchange on each listing.
I would say it would have some 15 forms, and yes, they must have CRUD operations.
What attracted me to the ThreadLocal pattern is the ability to have the Struts actions with more clear code separation from the DAO's. These also take care of some of the exception handling.
Of course many authors insist that DAO's should be entirely independent of the transaction management ...
The other way to make this work would be to pass the session object to the DAO's and the action would take care of the all exception handling and all Session closing ...
(By the way I use Tomcat 5.5 is there any problem with tomcat 5.5 using this pattern ... because of the ThreadLocal ... ).

Regards,
Julio
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disclaimer: I'm not a Struts developer

Originally posted by Julio Compadre:

Of course many authors insist that DAO's should be entirely independent of the transaction management ...



Well in terms of a DAO not needing to know about Txn Management I totally agree. DAOs should be usable with or without Txns.

Originally posted by Julio Compadre:

The other way to make this work would be to pass the session object to the DAO's and the action would take care of the all exception handling and all Session closing ...



That is an option yes (especially if you are not comfortable with the Thread Local pattern), I guess an advantage of this is that you are doing everything very explicitly which from a maintenance point of view can be beneficial. One of the disadvantages are that you'll be writing a lot more repeated plumbing code.

Originally posted by Julio Compadre:

(By the way I use Tomcat 5.5 is there any problem with tomcat 5.5 using this pattern ... because of the ThreadLocal ... ).



There should be no problem, I think the ThreadLocal pattern is a reasonable choice in your circumstance.

You might also want to search for 'Jakarta Struts Cookbook' on Google Books, you'll see that it also discusses this Thread Local pattern as well as some others.

Hope that helps!
[ October 01, 2008: Message edited by: Martijn Verburg ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic