session同步

做过weblogic集群环境的人应该都清楚,要想实现session同步,必须满足两个条件:第一,在weblogic.xml里面增加session同步相关的代码;第二,所有放入session的类都要序列化。

    但是,我终于还是栽了。两个条件明明都满足了,但就是“有些”对象就是不能同步。经过5个小时的奋战,终于让我找到了问题所在。我称之为“第三个条件”。

    我以前使用session是这样的:

Java代码 复制代码  收藏代码
  1. Customer customer = (Customer)session.getAttribute(KEY_CUSTOMER);   
  2. customer.setName("funcreal");  
Customer customer = (Customer)session.getAttribute(KEY_CUSTOMER);
customer.setName("funcreal");

    这样写完以后,在单机环境下,session中的变量customer的name属性就被更改了。然而在集群环境下,仅仅这样做是不能触发session同步机制的。必须要把customer变量在重新放入session中,即:

Java代码 复制代码  收藏代码
  1. session.setAttibute(KEY_CUSTOMER, customer);  
session.setAttibute(KEY_CUSTOMER, customer);

   后来我从weblogic的售后工程师那里也确认了这一点。一点点经验,和大家分享。

 

在weblogic的文档中又翻出了以下内容,要是之前认真看了就好了:

Use setAttribute to Change Session State:
In an HTTP servlet that implements javax.servlet.http.HttpSession, use HttpSession.setAttribute (which replaces the deprecated putValue) to change attributes in a session object. If you set attributes in a session object with setAttribute, the object and its attributes are replicated in a cluster using in-memory replication. If you use other set methods to change objects within a session, WebLogic Server does not replicate those changes. Every time a change is made to an object that is in the session, setAttribute() should be called to update that object across the cluster.

你可能感兴趣的:(职场,休闲,好)