jakarta-jcs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Travis Savo <ts...@IFILM.com>
Subject JCSWorker extension for Hibernate
Date Mon, 09 Aug 2004 20:52:12 GMT
Here's an example of how JCSWorker can be extended to reassociate objects
with a Hibernate session. This is absolutely mandatory when using Hibernate
with Remote Cache and lazy loaded collections because without doing this,
lazy loaded collections won't be able to find the session to reload
themselves with.

Where does this type of stuff belong, Aaron? Is there a application specific
package things like this belong in? I have another... a OSCache->JCS bridge,
which has proven quite useful.

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import net.sf.hibernate.LockMode;
import net.sf.hibernate.Session;

import org.apache.jcs.utils.access.JCSWorker;
import org.apache.jcs.utils.access.JCSWorkerHelper;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

public class HibernateJCSWorker extends JCSWorker {
	private static final Logger logger =
Logger.getLogger(HibernateJCSWorker.class);

	public HibernateJCSWorker(String aName) {
		super(aName);
	}
	/**
	 * @deprecated You should be using getResult(Serializable aKey,
JCSWorkerHelper aWorker, Session aSession) throws Exception {
	 */
	public Object getResult(Serializable aKey, JCSWorkerHelper aWorker)
throws Exception{
		return getResult(aKey, null, aWorker, null);
	}
	/**
	 * @deprecated You should be using getResult(Serializable aKey,
String aGroup, JCSWorkerHelper aWorker, Session aSession) throws Exception {
	 */
	public Object getResult(Serializable aKey, String aGroup,
JCSWorkerHelper aWorker) throws Exception{
		return getResult(aKey, aGroup, aWorker, null);
	}
	
	/**
	 * Wrapper on JCSWorker which takes a session to reassociate objects
with.
	 */
	public Object getResult(Serializable aKey, JCSWorkerHelper aWorker,
Session aSession) throws Exception {
		return getResult(aKey, null, aWorker, aSession);
	
	}
	/**
	 * Wrapper on JCSWorker which takes a session to reassociate objects
with.
	 */
	public Object getResult(Serializable aKey, String aGroup,
JCSWorkerHelper aHelper, Session aSession) throws Exception {
		Object result = super.getResult(aKey, aGroup, aHelper);
		if(aSession == null){
			return result;
		}
		try {
			if (result instanceof List) {
				for (Iterator i = ((List)
result).iterator(); i.hasNext();) {
					Object o = i.next();
					reassociate(o, aSession);
				}
			} else {
				reassociate(result, aSession);
			}
		} catch (Exception e) {
			if (logger.isEnabledFor(Priority.INFO)) {
				logger.info("Couldn't reassociate a session
with a " + result);
				logger.info(e);
			}
		}
		return result;
	}

	/**
	 * Reassociates an object with a session so lazy loaded collections
will still work.
	 */
	public void reassociate(Object anObject, Session aSession) throws
Exception {
		if (!aSession.contains(anObject)) {
			aSession.lock(anObject, LockMode.NONE);
		}
	}
}

-Travis Savo <tsavo@ifilm.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-dev-help@jakarta.apache.org


Mime
View raw message