File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "SessionCounter in Cluster Weblogic environment" Watch "SessionCounter in Cluster Weblogic environment" New topic

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) {
String message = "valudBound: sessionCount = " + sessionCount;
EMotorolaLogger.logDetail(getClass(), "SessionCounter.valueBound", message);

public void valueUnbound(HttpSessionBindingEvent e) {
synchronized(this) {
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

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,
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.
I agree. Here's the link:
subject: SessionCounter in Cluster Weblogic environment
jQuery in Action, 3rd edition