我正在使用hibernate进行maven应用程序开发。
在第一次加载应用程序时,会话工厂需要花费大量时间来加载(大约3-4分钟),但随后的会话工厂创建会立即发生。有人可以帮我弄清楚我应该做些什么改变以减少时滞?
这是hibernate实用程序文件:
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();
}
}
}
答案 0 :(得分:0)
这很正常。 第一次配置hibernate并使用会话工厂(即运行程序时),它需要读取你的hibernate配置,你所有的POJO文件和放大器。您的地图文件,请确保您的地图,POJO和数据库匹配等。 所以第一次运行总是需要时间