my dog learned polymorphism*
The moose likes Servlets and the fly likes SessionCounter in Cluster Weblogic environment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "SessionCounter in Cluster Weblogic environment" Watch "SessionCounter in Cluster Weblogic environment" New topic
Author

SessionCounter in Cluster Weblogic environment

Balaji Munuswamy
Ranch Hand

Joined: Apr 07, 2002
Posts: 37
Hi all,

In our application, we are increment the counter when user login and decrementing the same when user logout. This code is deployed in Weblogic 7.0 Clustered environment. While we doing concurrent users testing, even all the users are logged out we notice the counter is carrying more then zero. I need to know how cluster environment manage the �sessionCount� variable. Or if I use the variable as static, can this problem will solve?


Please check the code below and what is the problem in the code?

public class SessionCounter implements HttpSessionBindingListener {
private static SessionCounter me = new SessionCounter();

private int sessionCount = 0;

private SessionCounter() {
this.sessionCount = 0;
}

public void valueBound(HttpSessionBindingEvent e) {
synchronized(this) {
this.sessionCount++;
}
String message = "valudBound: sessionCount = " + sessionCount;
EMotorolaLogger.logDetail(getClass(), "SessionCounter.valueBound", message);
}

public void valueUnbound(HttpSessionBindingEvent e) {
synchronized(this) {
this.sessionCount--;
}
String message = "valudUnbound: sessionCount = " + sessionCount;
EMotorolaLogger.logDetail(getClass(), "SessionCounter.valueUnbound", message);
}

public int getSessionCount() {
return this.sessionCount;
}

public static SessionCounter getInstance() {
return me;
}
}
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Try using HttpSessionListener instead of HttpSessionBindingListener. Because using session binding listener it will increment the count when any thing will be bound to the session.
Balaji Munuswamy
Ranch Hand

Joined: Apr 07, 2002
Posts: 37
Hi,

Thanks for the response. but It is working fine in my local and development weblogic which is not a cluster environment.

Can you tell me, is this problem because of the counter is not declear as static(But the class is singelton class).

Thank you,
Balaji.M
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
Hi Balaji,

From my understanding of a cluster, it means that there are multiple servers (multiple JVMs) hosting the same application.
You could possibly think along these lines.

- Each server would have a separate singleton instance of SessionCounter.
Since SessionCounter implements HttpSessionBindingListener, I am assuming that when a user logs in, the application puts this singleton object in session and when the user logs out, the application removes it. But there are multiple instances of SessionCounter in a clustered environment. This is where I think the flaw is.

- Weblogic does provided replication of HttpSession state across all servers in the cluster.
SessionCounter would have to be made Serializable for this to happen.

You can find more information on coding JSPs and Servlets for a Weblogic clustered environment.

More on HTTP Session State replication here.
For example:
- Session Data Must Be Serializable
- Use setAttribute to Change Session State

I have one question, what is the key you are using when putting this object in session.

Hope this helps.
Sheldon
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: SessionCounter in Cluster Weblogic environment
 
Similar Threads
Invalidate Question
Can we restrict number of sessions through configuration(web.xml).
synchronized thread???
Session expire problem with HttpSessionListener
ThreadUnsafe