tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Leon Rosenberg <struts_u...@anotheria.net>
Subject RE: Tomcat/JVM hangs in session.getAttribute / HashMap.get()
Date Tue, 06 Sep 2005 15:57:25 GMT
seems so:-)

I just wrote an util class, you may use it if you need :-)

HTH Leon

package xxx-omitted-xxx;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;

import org.apache.log4j.Logger;

public class BeanUtil {
	
	private static Logger logger;
	
	public static void setLogger(Logger aLogger){
		logger = aLogger;
	}
	
	public static Logger getLogger(){
		return logger;
	}
	
	public static void addBeanToSessionUnsafe(
			HttpServletRequest request,
			String key,
			Object value) {

		addBean(request, PageContext.SESSION_SCOPE, key, value);
	}

	public static void addBeanToApplication(
		HttpServletRequest request,
		String key,
		Object value) {
		addBean(request, PageContext.APPLICATION_SCOPE, key, value);
	}

	public static void addBeanToRequest(
		HttpServletRequest request,
		String key,
		Object value) {
		addBean(request, PageContext.REQUEST_SCOPE, key, value);
	}

	public static Object getBeanFromSessionUnsafe(
		HttpServletRequest request,
		String key) {
		return getBean(request, PageContext.SESSION_SCOPE, key);
	}

	public static Object getBeanFromApplication(
		HttpServletRequest request,
		String key) {
		return getBean(request, PageContext.APPLICATION_SCOPE, key);
	}

	public static Object getBeanFromRequest(
		HttpServletRequest request,
		String key) {
		return getBean(request, PageContext.REQUEST_SCOPE, key);
	}

	public static void removeBeanFromSessionUnsafe(
		HttpServletRequest request,
		String key) {
		removeBean(request, PageContext.SESSION_SCOPE, key);
	}

	public static void removeBeanFromApplication(
		HttpServletRequest request,
		String key) {
		removeBean(request, PageContext.APPLICATION_SCOPE, key);
	}

	public static void removeBeanFromRequest(
		HttpServletRequest request,
		String key) {
		removeBean(request, PageContext.REQUEST_SCOPE, key);
	}

	public static void addBean(
		HttpServletRequest request,
		int scope,
		String key,
		Object value) {

		switch (scope) {
			case PageContext.APPLICATION_SCOPE :
				if (logger!=null)
					logger.debug("addBean " + key + " to APPLICATION_SCOPE, value=" +
value);
				request.getSession().getServletContext().setAttribute(key, value);
			break;
			case PageContext.SESSION_SCOPE :
				if (logger!=null)
					logger.debug("addBean " + key + " to SESSION_SCOPE, value=" +
value);
				request.getSession().setAttribute(key, value);
				break;
			case PageContext.REQUEST_SCOPE :
				if (logger!=null)
					logger.debug("addBean " + key + " to REQUEST_SCOPE, value=" +
value);
				request.setAttribute(key, value);
				break;

			default :
				throw new RuntimeException("Unknown scope:" + scope);
		}
	}

	public static Object getBean(HttpServletRequest request,	int scope,
String key) {
		switch (scope) {
			case PageContext.APPLICATION_SCOPE :
					return request.getSession().getServletContext().getAttribute(key);
			case PageContext.SESSION_SCOPE :
				return request.getSession().getAttribute(key);
			case PageContext.REQUEST_SCOPE :
				return request.getAttribute(key);
			default :
				throw new RuntimeException("Unknown scope:" + scope);
		}
	}

	public static void removeBean(HttpServletRequest request, int scope,
String key) {
		switch (scope) {
			case PageContext.APPLICATION_SCOPE :
				request.getSession().getServletContext().removeAttribute(key);
				break;

			case PageContext.SESSION_SCOPE :
				request.getSession().removeAttribute(key);
				break;

			case PageContext.REQUEST_SCOPE :
				request.removeAttribute(key);
				break;

			default :
				throw new RuntimeException("Unknown scope:" + scope);
		}
	}

	public static void addBeanToSessionSafe(HttpServletRequest request,
String key, Object value) {
		HttpSession session = request.getSession();
		if (logger!=null)
			logger.debug("addBean " + key + " to SESSION_SCOPE, value=" + value);
		synchronized(session){
			session.setAttribute(key, value);
		}
	}
	
	public static void removeBeanFromSessionSafe(HttpServletRequest
request, String key){
		HttpSession session = request.getSession();
		if (logger!=null)
			logger.debug("removeBean " + key + " from SESSION_SCOPE");
		synchronized(session){
			session.removeAttribute(key);
		}
	}

	public static Object getBeanFromSessionSafe(HttpServletRequest request,
String key) {
		HttpSession session = request.getSession();
		Object value = null;
		synchronized(session){
			value = session.getAttribute(key);
		}

		if (logger!=null)
			logger.debug("getBean " + key + " from SESSION_SCOPE: "+value);
		return value;
	}

	
}


On Tue, 2005-09-06 at 16:41 +0100, Arup Vidyerthy wrote:
> Guys,
> 
> I have been watching this thread with interest.
> 
> Does this mean that all session.setAttribute() and session.getAttribute()
> should always be synchronised (for instance, inside a servlet or struts
> action) provided the same hashmap is accessed by say more than one
> servlet/struts action?
> 
> Kind regards...
> Arup Vidyerthy. 
> 
> -----Original Message-----
> From: Caldarale, Charles R [mailto:Chuck.Caldarale@unisys.com] 
> Sent: 06 September 2005 15:54
> To: Tomcat Users List
> Subject: RE: Tomcat/JVM hangs in session.getAttribute / HashMap.get()
> 
> > From: Leon Rosenberg [mailto:struts_user@anotheria.net]
> > Subject: RE: Tomcat/JVM hangs in session.getAttribute / HashMap.get()
> > 
> > and replace all req.getSession().setAttribute(beanName, beanValue) in 
> > code with the call to this method (same for remove) and I've solved my 
> > problem?
> 
> Unfortunately, you also need to change the places that retrieve attributes
> from the Session, since the hash map is in a state of flux during the
> setAttribute() invocations.
> 
>  - Chuck
> 
> 
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> MATERIAL and is thus for use only by the intended recipient. If you received
> this in error, please contact the sender and delete the e-mail and its
> attachments from all computers.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
> 
> 		
> ___________________________________________________________ 
> To help you stay safe and secure online, we've developed the all new Yahoo! Security
Centre. http://uk.security.yahoo.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message