<< Exceptions Explained
InvalidObjectException: Could not find a SessionFactory named: null
Where it can occur: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.
What it means:Hibernate can not reconstruct the disconnected Session that was serialized during deserialization. A Session also has an associated SessionFactory instance that also has to be serialized and deserialized. However, this SessionFactory is only serialized as a "hollow" object and has to be replaced with a real SessionFactory on deserialization. This replacement procedure depends on a) the old classloader (lookup in a Singleton HashMap) or b) on a SessionFactory in JNDI. If both are not present, the deserialization fails with the exception.
How it can be caused:
Serialization/deserialization of a Hibernate Session in different VMsIf a Hibernate Session is disconnected and serialized, then deserialized in a different VM, the original classloader is no longer present. If no JNDI lookup can be made to find the associated SessionFactory, the exception is thrown.
Hot redeployment or context reloadsIf you store a disconnected Hibernate Session in some user session (e.g. for a long running Application Transaction implementation), it might be serialized/deserialized at some point. This is the case if you chose the HttpSession to keep the Hibernate Session and the web container reloads the context. The classloader is replaced during that reload and the deserialization process can not find the SessionFactory afterwards. The same principle applies to hot redeployment in application servers.
How it can be fixed:
- In application servers, always use JNDI to store the SessionFactory, as documented.
- In Tomcat or other web containers, disable HttpSession serialization. This has side effects, explained in the web containers documentation. This is an example for Tomcat (Nick Heudecker):
<Context path="/example" docBase="example.war">
<Manager className="org.apache.catalina.session.PersistentManager"
saveOnRestart="false"/>
<!-- other stuff here -->
</Context>
|