Tomcat/Eclipse: disable HttpSession serialization during context reload

eclipse, hibernate, jsf, tomcat

Solution

You should be able to turn it of by putting:

<Manager pathname="" />

within the Context section in the Tomcat configuration file.

See the Tomcat documentation: Configuration Reference > The Manager Component > Disable Session Persistence

- Version 7.0

- Version 8.0

Problem

I'm getting `InvalidObjectException: Could not find a SessionFactory named: null` after restart of tomcat server. I've found some possible solution: This exception occurs if you try to serialize a disconnected Hibernate Session and deserialize it in a different VM, or, if the classloader has restarted, for example, during hot redeployment in your application server or web container. This is especially visible in Tomcat. In application servers, always use JNDI to store the SessionFactory, as documented. In Tomcat or other web containers, disable HttpSession serialization during context reload. This has side effects, explained in the web container's documentation. How can I do it? Disable HttpSession serialization during context reload? Note: I'm using Tomcat7 in Eclipse. UPDATE: I've tried enable this tag in `context.xml`: `<Manager pathname="" />` and yes, the exception disappears, but I lose persistent sessions, so I have to login again after tomcat server restart. Do I understand session persistence well, if I believe, that session should be kept (and I mustn't create a new one by login) after tomcat restart in e.g. JSF application? UPDATE2 (session manager): ``` @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 ---------------------------------------------------- } ``` And my `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> ``` And my session factory: ``` 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) { ... } } } ```

Original source