Java sessionfactory创建第一次加载需要很多时间

Java sessionfactory创建第一次加载需要很多时间,java,hibernate,maven,sessionfactory,Java,Hibernate,Maven,Sessionfactory,我正在使用hibernate开发maven应用程序 在第一次加载应用程序时,会话工厂需要花费大量时间加载(大约3-4分钟),但随后的会话工厂创建是即时进行的。有人能帮我弄清楚我应该做些什么改变来减少时间延迟吗 以下是hibernate实用程序文件: package com.infosys.common.util; import java.util.logging.Logger; import org.hibernate.SessionFactory; import org.hibernate

我正在使用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();
        }
    }
}
这是很正常的。 第一次配置hibernate并使用会话工厂时(即运行程序时),它需要读取hibernate配置、所有POJO文件和映射文件,确保映射、POJO和数据库匹配等。 所以第一次跑步总是需要时间,这很正常。 第一次配置hibernate并使用会话工厂时(即运行程序时),它需要读取hibernate配置、所有POJO文件和映射文件,确保映射、POJO和数据库匹配等。
因此,第一次运行总是需要时间

我见过其他程序使用相同的hibernate方法,它花费的时间非常少。我能对代码做些什么更改吗?数据库是否在您的本地网络中?可能是网络连接不良导致启动延迟?!我的团队在我们公司的网络中使用MySQL workbench,我见过其他程序使用相同的hibernate方法,这花费的时间非常少。我能对代码做些什么更改吗?数据库是否在您的本地网络中?可能是网络连接不良导致启动延迟?!我的团队正在公司网络中使用MySQL workbench