我InvalidObjectException: Could not find a SessionFactory named: null
在重新启动tomcat服务器后得到。
我找到了一些可能的解决方案:
如果您尝试序列化断开连接的 Hibernate Session 并在不同的 VM 中反序列化它,或者如果类加载器已重新启动,例如,在应用程序服务器或 Web 容器中的热重新部署期间,则会发生此异常。这在 Tomcat 中尤其明显。在应用程序服务器中,始终使用 JNDI 来存储 SessionFactory,如文档所述。在 Tomcat 或其他 Web 容器中, 在 context reload 期间禁用 HttpSession 序列化。这有副作用,在 web 容器的文档中进行了解释。
我该怎么做?在上下文重新加载期间禁用 HttpSession 序列化?
注意:我在 Eclipse 中使用 Tomcat7。
更新:
我尝试在以下位置启用此标签context.xml
:
<Manager pathname="" />
是的,异常消失了,但是我失去了持久会话,所以我必须在 tomcat 服务器重新启动后再次登录。
我是否很好地理解会话持久性,如果我相信,在例如 JSF 应用程序中的 tomcat 重新启动后应该保留该会话(并且我不能通过登录创建一个新会话)?
UPDATE2(会话管理器):
@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) {
...
}
}
}