Serializing the hibernate sessionfactory & deserialize it to improve startup time

Hi,

I have around 500 entities and taking around 30sec to initialize the Hibernate SessionFactory during service startup. I am just thinking of serializing the SessionFactory to a file during maven build process and just deserialize it during service startup.

I am able to serialize during maven build process but while deserializing, i am getting the following error:

Caused by: org.hibernate.type.SerializationException: could not deserialize
at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:243)
at org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:262)

During maven build time, serializing the session factory object:
Configuration config = new Configuration();
for (String entityName: EntityNameList) {
try {
config.addAnnotatedClass(Class.forName(entityName));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
try (FileOutputStream file = new FileOutputStream(“c:\work\configuration2.ser”);
ObjectOutputStream out = new ObjectOutputStream(file)) {

config.setProperty("hibernate.dialect", "com.oracle.MyOracleDialect");
config.setProperty(AvailableSettings.SESSION_FACTORY_NAME, "mysf")
		.setProperty(AvailableSettings.SESSION_FACTORY_NAME_IS_JNDI, "false"); // default is true
		SessionFactory factory = config.buildSessionFactory();
		String uuid = ((SessionFactoryImplementor) factory).getUuid();
		System.out.println("original uuid::" + uuid);
		out.writeObject(factory);
	} catch (IOException e) {
		e.printStackTrace();
	}

}

Deserialization process:
FileInputStream file = new FileInputStream(“c:\work\configuration2.ser”);
ObjectInputStream in = new ObjectInputStream(file);

SessionFactoryImplementor factory = (SessionFactoryImplementor) SerializationHelper.deserialize(in);

From the hibernate sessionfactory deserialization logic:

private static SessionFactory locateSessionFactoryOnDeserialization(String uuid, String name) throws InvalidObjectException{

final SessionFactory uuidResult = SessionFactoryRegistry.INSTANCE.getSessionFactory( uuid );

if ( uuidResult != null ) {
LOG.debugf( “Resolved SessionFactory by UUID [%s]”, uuid );
return uuidResult;
}

// in case we were deserialized in a different JVM, look for an instance with the same name

// (provided we were given a name)
if ( name != null) {
final SessionFactory namedResult = SessionFactoryRegistry.INSTANCE.getNamedSessionFactory( name );

if( namedResult != null) {
LOG.debugf( “Resolved SessionFactory by name [%s]”, name );
return namedResult;
}
}

throw new InvalidObjectException( “Could not find a SessionFactory [uuid=” + uuid + “,name=” + name + “]” );
}

It seems like while deserializing the sessionfactory it is always trying to get from the local cache.

So, my question is

  1. Is it possible to serialize the hibernate session factory in JVM (maven build process) and use it in a different JVM?
  2. Is my approach correct? Am i missing anything here?
  3. Some forums saying that it is possible when we use JNDI context. Is this correct?

Thank in advance.

1 Like

Any update on this question?