Session factory instance Initialization time


#1

We migrated a Hibernate layer of a Java-Desktop-application from 4 (4.1.19) to Hibernate 5 (5.2.17).
We still use legacy HBM mapping-files. The initialization of the Session factory instance takes now more time (double).

// Initialization-Method - Hibernate 4.1.19 - ~ 5 seconds

public synchronized  SessionFactory getSessionFactory() {
	private SessionFactory sessionFactory = null;

	try {
		Configuration configuration = new Configuration();
		configuration.configure();

		// add custom-Hibernate-Interceptor
		configuration.setInterceptor(new myInterceptor());
				
		// Build a Service-Registry
		ServiceRegistry aRegistry = new ServiceRegistryBuilder()
                        					.applySettings(configuration.getProperties())
											.buildServiceRegistry();
				
		// Build the SessionFactory
		sessionFactory = (SessionFactoryImpl) configuration.buildSessionFactory(aRegistry);

	} 
	catch (Throwable ex) {
		....
	}

	return sessionFactory;		
}

// Initialization-Method - Hibernate 5.2.17 ~10 seconds

private synchronized SessionFactory getSessionFactory() {
	private SessionFactory sessionFactory = null;
	
	try {
         // create configuration 
		 Configuration configuration = new Configuration();

		 // add custom-Hibernate-Interceptor
  	     configuration.setInterceptor(new myInterceptor());
				
		// Build a Service-Registry
		aServiceRegistry = new StandardServiceRegistryBuilder(bootstrapRegistry)
				                                       .applySettings(configuration.getProperties())
				                                       .configure()
				                                       .build();

        // Build the MetadataSources
		MetadataSources aSources = new MetadataSources(aServiceRegistry);
				
		// Build the Metadata
		Metadata metadata = aSources.getMetadataBuilder().build();				
				
		// Build the SessionFactory
		sessionFactory = metadata.getSessionFactoryBuilder().build();
	}
				
	catch (Throwable ex) {
		....
	}
	
	return sessionFactory;
}

#2

Try to use a Profiler to identify the issue. Thanks.


#3

OK. Thanks. We try it out.


#4
 // Build the MetadataSources
MetadataSources aSources = new MetadataSources(aServiceRegistry);
				
// Build the Metadata
Metadata metadata = aSources.getMetadataBuilder().build();

// Build the SessionFactory
sessionFactory = metadata.getSessionFactoryBuilder().build();

The Methods

  • SessionFactoryBuilderImpl.build() [ MetamodelImpl.initialize() + SessionFactoryImpl.applyCfgXmlValues()]

and

  • MetadataBuildImpl.build() [ MappingReference.apply() + MetadataBuildingProcess.build()]

takes more time than

// Build the SessionFactory
sessionFactory = (SessionFactoryImpl) configuration.buildSessionFactory(aRegistry);

from 4.1.19 to build the factory.

Is Metadata necessary to build the factory?


#5

Yes, it is. You need to break down to see what exactly it is that takes more time now, not just the higher level calling methods.