2

继续解决这种情况,我已经更新了hibernateond sqljdbc4 jars,现在异常已更改(请看下面)。

我正在使用

  • 休眠 v4.1.4 最终版
  • JSF 2.1 (莫哈拉)
  • 我所有的@ManagedBeans implements Serializable

它与 有一些关系@ViewScoped,因为@SessionScopedbean 还可以

我试过的:

  • enable <Manager pathname=""/> in context.xml,是的,异常消失了,但我真的失去了持久会话,所以我必须在 tomcat 服务器重启后再次登录。

  • 在 hibernate.cfg.xml 文件中进行更改(例如添加name="java:hibernate/SessionFactory"<session-factory>标签),但没有成功

  • SESSIONS.ser文件,该文件是在停止tomcat 服务器后创建的。

    • 当我打开ser 文件时,我可以找到类似的内容: .... uuidq ~ xppt $e8906373-f31b-4990-833c-c7680b2a77ce...
    • 如果我再次启动tomcat 服务器,它会报告Could not find a SessionFactory [uuid=8906373-f31b-4990-833c-c7680b2a77ce,name=null]-为什么???会话似乎存在于SESSION.ser文件中......

我的会话管理器:

@SessionScoped
@ManagedBean(name="userManager")
public class UserManager implements Serializable {
  private static final long serialVersionUID = 1L;
  private transient HttpSession session;
  private String username;
  private String password;
  private User user;

  public String login() {
    // here is seraching for user in database (based on username and password)

    if (user != null) {
      FacesContext context = FacesContext.getCurrentInstance();
      session = (HttpSession) context.getExternalContext().getSession(true);
      session.setAttribute("id", user.getId());
      session.setAttribute("username", user.getName());
      ...
      return "success";
    } 
  }

  public String logout() {
    user = null;
    FacesContext context = FacesContext.getCurrentInstance();
    session = (HttpSession) context.getExternalContext().getSession(true);
    ...     
    session.invalidate();
    return "success";
  }
  // getters and setters ----------------------------------------------------
}

我的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC 
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
 <session-factory>
  <property name="connection.url">jdbc:sqlserver://localhost;databaseName=RXP</property>
  <property name="connection.username">user</property>
  <property name="connection.password">password</property>
  <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
  <property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
  <property name="current_session_context_class">thread</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.hbm2ddl.auto">update</property>

  <mapping class="tables.User"/>

 </session-factory>
</hibernate-configuration>

和我的会话工厂

public final class DaoSF implements Serializable {
  private static final long serialVersionUID = 1L;

  private static SessionFactory sessionFactory;
  private static ServiceRegistry serviceRegistry;

  public static SessionFactory getSessionFactory() {
    try {
      Configuration configuration = new Configuration();
      configuration.configure("hibernate.cfg.xml");
      serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
      sessionFactory = configuration.buildSessionFactory(serviceRegistry);
      return sessionFactory;
    }
    catch (HibernateException he) { 
      ...
    }
  }
}

例外

30.6.2012 13:29:07 org.apache.catalina.session.StandardManager doLoad
SEVERE: IOException while loading persisted sessions: java.io.InvalidObjectException: Could not find a SessionFactory [uuid=f9c33312-cf7f-4f65-ae5f-44261705c18e,name=null]
java.io.InvalidObjectException: Could not find a SessionFactory [uuid=f9c33312-cf7f-4f65-ae5f-44261705c18e,name=null]
    at org.hibernate.internal.SessionFactoryImpl.locateSessionFactoryOnDeserialization(SessionFactoryImpl.java:2007)
    at org.hibernate.internal.SessionFactoryImpl.deserialize(SessionFactoryImpl.java:2037)
....

我试图以hibernate.cfg.xml这种方式编辑我的文件:

<session-factory name="java:hibernate/SessionFactory">

并且报告的错误已更改为:

30.6.2012 13:38:23 org.apache.catalina.session.StandardManager startInternal
SEVERE: Exception loading sessions from persistent storage
java.lang.NullPointerException
    at java.util.concurrent.ConcurrentHashMap.get(Unknown Source)
    at org.hibernate.internal.SessionFactoryRegistry.getSessionFactory(SessionFactoryRegistry.java:140)
    at org.hibernate.internal.SessionFactoryRegistry.getNamedSessionFactory(SessionFactoryRegistry.java:135)
    at org.hibernate.internal.SessionFactoryImpl.locateSessionFactoryOnDeserialization(SessionFactoryImpl.java:2000)
    at org.hibernate.internal.SessionFactoryImpl.deserialize(SessionFactoryImpl.java:2037)
....

你没有遇到过这个错误报告吗?

4

2 回答 2

2

您不希望SessionSessionFactoryDaoSF的实例被序列化。它们是暂时的,无法从文件中恢复。你最好首先弄清楚它们在哪里以及为什么被序列化到一个文件中。这就是您需要进行修复的地方。

于 2012-07-01T12:57:16.550 回答
2

另一个功能是更新tomcat/conf/下的context.xml,具体如下:

   <Manager className="org.apache.catalina.session.PersistentManager" saveOnRestart="false"   maxActiveSessions="-1" minIdleSwap="-1" maxIdleSwap="-1" maxIdleBackup="-1">
   <Store className="org.apache.catalina.session.FileStore"/>
   </Manager>
于 2013-01-29T09:05:10.647 回答