Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* OpenSessionInViewListener.java
  
  	Purpose:
  		
  	Description:
  		
  	History:
  		Tue Sep  5 10:11:55     2006, Created by henrichen
  
 Copyright (C) 2006 Potix Corporation. All Rights Reserved.
 
 {{IS_RIGHT
 	This program is distributed under LGPL Version 2.1 in the hope that
 	it will be useful, but WITHOUT ANY WARRANTY.
 }}IS_RIGHT
 */
 package org.zkoss.zkplus.hibernate;
 
 import java.util.List;
 
Listener to init and cleanup the hibernate session automatically, implement the Hibernate's "Open Session In View" pattern without JTA support. This listener is used with HibernateUtil, or it will not work.

In WEB-INF/zk.xml, add following lines:


  <listener>
  <description>Hibernate "OpenSessionInView" Listener</description>
  <listener-class>org.zkoss.zkplus.hibernate.OpenSessionInViewListener</listener-class>
 </listener>
 

Applicable to Hibernate version 3.2.ga or later

Deprecated:
As of release 6.0.2, please use the official Hibernate's method instead.
Author(s):
henrichen
 
 	private static final Logger log = LoggerFactory.getLogger(OpenSessionInViewListener.class);
 
 	//-- ExecutionInit --//
 	public void init(Execution execExecution parent) {
 		if (parent == null) { //the root execution of a servlet request
 			.debug("Starting a database transaction: "+exec);
 			HibernateUtil.currentSession().beginTransaction();
 		}
 	}
 	
 	//-- ExecutionCleanup --//
 	public void cleanup(Execution execExecution parentList errs) {
 		if (parent == null) { //the root execution of a servlet request
 			try {
 				if (errs == null || errs.isEmpty()) {
 					// Commit and cleanup
 					.debug("Committing the database transaction: "+exec);
 					HibernateUtil.currentSession().getTransaction().commit();
 				} else {
 					final Throwable ex = (Throwableerrs.get(0);
 					if (ex instanceof StaleObjectStateException) {
 						// default implementation does not do any optimistic concurrency 
 						// control; it simply rollback the transaction.
 					} else {
 						// default implementation log the stacktrace and then rollback
 						// the transaction.
 						handleOtherException(execex);
 					}
 				}
 			} finally {
 				HibernateUtil.closeSession(); //always close it
 			}
 		}
 	}

Default StaleObjectStateException handler. This implementation does not implement optimistic concurrency control! It simply rollback the transaction.

Application developer might want to extends this class and override this method to do other things like compensate for any permanent changes during the conversation, and finally restart business conversation. Or maybe give the user of the application a chance to merge some of his work with fresh data... what can be done here depends on the applications design.

Parameters:
exec the execution to clean up.
ex the StaleObjectStateException being thrown (and not handled) during the execution
 			
 		.error("This listener does not implement optimistic concurrency control!");
 		rollback(execex);
	}

Default other exception (other than StaleObjectStateException) handler. This implementation simply rollback the transaction.

Application developer might want to extends this class and override this method to do other things like compensate for any permanent changes during the conversation, and finally restart business conversation... what can be done here depends on the applications design.

Parameters:
exec the execution to clean up.
ex the Throwable other than StaleObjectStateException being thrown (and not handled) during the execution
	protected void handleOtherException(Execution execThrowable ex) {
		// Rollback only
		rollback(execex);
	}

rollback the current session.

Parameters:
exec the execution to clean up.
ex the StaleObjectStateException being thrown (and not handled) during the execution
	private void rollback(Execution execThrowable ex) {
		try {
			if (HibernateUtil.currentSession().getTransaction().isActive()) {
				.debug("Trying to rollback database transaction after exception:"+ex);
			}
catch (Throwable rbEx) {
			.error("Could not rollback transaction after exception! Original Exception:\n"+exrbEx);
		}
	}
New to GrepCode? Check out our FAQ X