Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* OpenSessionInViewListener.java
  
  {{IS_NOTE
  	Purpose:
  		
  	Description:
  		
  	History:
  		Tue Sep  5 10:11:55     2006, Created by henrichen
 }}IS_NOTE
 
 Copyright (C) 2006 Potix Corporation. All Rights Reserved.
 
 {{IS_RIGHT
 	This program is distributed under GPL Version 2.0 in the hope that
 	it will be useful, but WITHOUT ANY WARRANTY.
 }}IS_RIGHT
 */
 package org.zkoss.zkplus.hibernate;
 
 
 import  org.hibernate.StaleObjectStateException;
 
 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>
 

Author(s):
henrichen
 
 	private static final Log log = Log.lookup(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.
 						handleStaleObjectStateException(exec, (StaleObjectStateException)ex);
 					} 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 exection to clean up.
ex the StaleObjectStateException being thrown (and not handled) during the execution
 			
 	protected void handleStaleObjectStateException(Execution exec, StaleObjectStateException ex) {
		.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 exection 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 exection 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);
				HibernateUtil.currentSession().getTransaction().rollback();
			}
catch (Throwable rbEx) {
			.error("Could not rollback transaction after exception! Original Exception:\n"+exrbEx);
		}
	}
New to GrepCode? Check out our FAQ X