Thread:
 Container managed JTA Session Service 
 ivelin   20 Dec 2003, 14:20 
 Re: Container managed JTA Session Service 
 Horst Dehmer   16 Feb 2004, 00:06 
 Re: Container managed JTA Session Service 
 alesj   19 Oct 2004, 10:58 
 Re: Container managed JTA Session Service 
 juice   28 Oct 2004, 19:32 
 Re: Container managed JTA Session Service 
 hengels   17 Feb 2004, 10:02 
 Synchronization problem 
 jdragon   17 Feb 2004, 23:29 
 Re: Synchronization problem 
 TransactionException   24 Apr 2007, 04:41 
 Re: Container managed JTA Session Service 
 hengels   18 Mar 2004, 13:29 
 Re: Container managed JTA Session Service 
 hengels   18 Mar 2004, 14:07 
 Re: Container managed JTA Session Service 
 clcantrell   03 Aug 2006, 08:51 

Comment
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
From: ivelin (20 Dec 2003, 14:20) Replies: 9, Views: 43797
Subject: Container managed JTA Session Service
As suggested in an earlier post, I would like to propose the following
convenience service for the JBoss integration:

http://cocoonhive.org/jboss/jtasession.zip 

The initial proposal can be found here:
http://forum.hibernate.org/viewtopic.php?t=925357

An independent but related thread:
http://forum.hibernate.org/viewtopic.php?t=926369

Here is the rationale behind this service:
	This service is applicable when the application uses JTA transactions.
	Either directly, via transaction servlet filter, or EJB.
	http://java.sun.com/products/jta/javadocs-1.0.1/javax/transaction/UserTransaction.html
	http://java.sun.com/blueprints/code/adventure/1.0/src/com/sun/j2ee/blueprints/waf/controller/web/TransactionFilter.java.html
	http://java.sun.com/products/ejb/2.0.html

	The JTAHibernateFactory maintains one HibernateSession per JTA
transaction. 
	It obtains a new session when requested for the first time and binds it
to the underlying
	UserTransaction. If there is no JTA transaction at the time that the
session is requested,
	the factory will throw an exception.
    
	Subsequent requests for a Hibernate session from this factory will
return the existing one.
	Once bound to the UserTransaction, the factory registers a Transaction
Synchronization,
	which ensures that the session is properly closed before the
transaction ends.

	The relationship between each HibernateSession obtain through this factory 
	and the underlying UserTransactions is one to one.
	There is at most one HibernateSession for each UserTransaction and 
	each HibernateSession is related to exactly one UserTransaction.
	
	This eliminates the thread safety problem and allows for a efficient
use of the session cache.
	
	Usage example:
  	public void saveAccount( Account account ) throws HibernateException
 	{
 		Session s = DBHelper.getSession();
 		s.saveOrUpdate(account);
 		// no need to close the session manually
 	}
 	
 	where DBHelper.getHibernateSession() is:
 	
	public static Session getHibernateSession()
	{
		try
		{
			InitialContext ctx = new InitialContext();
			JTASessionFactory jtaf =
(JTASessionFactory)ctx.lookup("java:/hibernate/JTASessionFactory");
			return jtaf.getSession();
		}
		catch(NamingException ne)
		{
			throw new RuntimeException("Failed obtaining hibernate JTA session,
because of: " + ne.getMessage(), ne);
		}
	}
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]