【问题标题】:sessionfactory creation takes a lot of time to load for the first timesessionfactory创建第一次加载需要很多时间
【发布时间】: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


    【解决方案1】:

    这很正常。 第一次配置休眠和使用会话工厂时(即运行程序时),它需要读取休眠配置、所有 POJO 文件和地图文件,确保地图、POJO 和数据库匹配等。 所以第一次运行总是需要时间

    【讨论】:

    • 我见过其他程序使用相同的休眠方法,它花费的时间非常少。我可以在我的代码中做任何更改吗??
    • 数据库在您的本地网络中吗?可能是网络连接不好导致启动延迟?!
    • 我的团队正在我们公司网络中使用 MySQL 工作台
    猜你喜欢
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 2014-08-17
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    相关资源
    最近更新 更多