【发布时间】:2017-10-24 12:00:39
【问题描述】:
我正在使用 hibernate 进行 maven 应用程序开发。
第一次加载应用程序时,会话工厂需要很长时间才能加载(大约 3-4 分钟),但随后的会话工厂创建会立即发生。有人可以帮我弄清楚我应该做哪些改变来减少时间延迟?
这里是休眠实用程序文件:
package com.infosys.common.util;
import java.util.logging.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
/*import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;*/
// TODO: Auto-generated Javadoc
/**
* The Class HibernateUtilities.
*/
public class HibernateUtilities {
/** The Constant CONFIGURATION_LOCATION. */
private static final String CONFIGURATION_LOCATION = "com/infosys/common/util/hibernate.cfg.xml";
private static final Logger LOGGER = Logger.getLogger(HibernateUtilities.class.getName());
/** The session factory. */
private static SessionFactory sessionFactory = null;
/** The service registry. */
private static ServiceRegistry serviceRegistry;
/**
* Gets the session factory.
*
* @return the session factory
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* Creates the session factory.
*
* @return the session factory
* @throws Exception
* the exception
*/
public synchronized static SessionFactory createSessionFactory() throws Exception {
if (sessionFactory == null)
{
try {
LOGGER.info("Logger Name: " + LOGGER.getName());
// Step1 : Loading the configuration details from
// hibernate.cfg.xml
Configuration configuration = new Configuration().configure(CONFIGURATION_LOCATION);
configuration.
setProperty("hibernate.temp.use_jdbc_metadata_defaults","false");
// Step2 : Creating ServiceRegistry using the
// StandardServiceRegistryBuilder and Configuration defined in
// Step 1
serviceRegistry = new StandardServiceRegistryBuilder().configure(CONFIGURATION_LOCATION).build();
// Step3 : Creating the SessionFactory using the Configuration
// and serviceRegistry.
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
throw e;
}
}
return sessionFactory;
}
/**
* Close session factory.
*/
public static void closeSessionFactory()
{
if(sessionFactory!=null)
{
sessionFactory.close();
}
}
}
【问题讨论】:
标签: java hibernate maven sessionfactory