java - 将 JDBC 更改为 hibernate 后,应用程序运行速度非常慢

标签 java hibernate jakarta-ee struts2 hibernate-mapping

以前,我在应用程序中使用 JDBC,它运行得非常快,但我将其修改为使用 Hibernate,这使得它太慢,尤其是当它需要打开一个包含下拉框的页面时。与 JDBC 相比,打开此类页面需要更长的时间。

如果我尝试使用外键访问表,则需要更长的时间。

我的服务器是 GlassFish,我正在使用以下版本的 hibernate。

  <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.10.Final</version>
            <type>jar</type>
        </dependency>

问题是为什么它比 JDBC 慢,我是否需要在每个 session.beginTransaction() 之前有以下 lin?

    session = HibernateUtil.getSessionFactory().openSession();

以下图为例,它有一个下拉框,打开页面后需要填充该下拉框。

HibernateUtil.java

package com.myproject.util;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {

    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;

    private static SessionFactory configureSessionFactory() {
        try {
            System.out.println("1");

            Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new 
                ServiceRegistryBuilder()
                  .applySettings(configuration.getProperties())
                  .buildServiceRegistry();
            System.out.println("2");

            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            System.out.println("3");

            return sessionFactory;
        } catch (HibernateException e) {
            System.out.append("** Exception in SessionFactory **");
            e.printStackTrace();
        }
        return sessionFactory;
    }

    public static SessionFactory getSessionFactory() {
        return configureSessionFactory();
    }
}

MyClassModel.java

public class MyClassModel extends HibernateUtil {

    private Session session;

     public Map populatedropdownList() {
        Map map = new HashMap();
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        List<MyListResult> temp = null;
        try{

              temp = retrieveItems();
              System.err.println("size:" + temp.size());
              for(int i=0;i<temp.size();i++){
                  map.put(temp.get(i).getId(),temp.get(i).getName());
              }
        session.getTransaction().commit();
        session.close();
        return map;
    }catch(Exception e){
        e.printStackTrace();
    }
    return map;
   }



private List <MyListResult> retrieveItems(){
              Criteria criteria = session.createCriteria(MyTable.class, "MyTable");
                ProjectionList pl = Projections.projectionList();
                pl.add(Projections.property("MyTable.id").as("id"));
                pl.add(Projections.property("MyTable.name").as("name"));
                criteria.setProjection(pl);

                criteria.setResultTransformer(new 
                        AliasToBeanResultTransformer(MyListResult.class));
                return criteria.list();
    }

MyListResult.java

public class MyListResult implements Serializable {
    private int id;
    private String Name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return Name;
    }

    public void setName(String Name) {
        this.Name = Name;
    }    

}

Hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/MyDatabase
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">
            org.hibernate.cache.NoCacheProvider
        </property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

                <mapping class="com.MyProject.MyTable" />


    </session-factory>

</hibernate-configuration>

控制台如下

INFO: in myform
INFO: 1
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
INFO: HHH000041: Configured SessionFactory: null
INFO: 2
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
INFO: HHH000115: Hibernate connection pool size: 1
INFO: HHH000006: Autocommit mode: false
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL 
[jdbc:mysql://localhost:3306/MyDatabase]
INFO: HHH000046: Connection properties: {user=root, password=****}
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
INFO: HHH000397: Using ASTQueryTranslatorFactory
INFO: HHH000228: Running hbm2ddl schema update
INFO: HHH000102: Fetching database metadata
INFO: HHH000396: Updating schema
INFO: HHH000261: Table found: MyDatabase.MyTable
INFO: HHH000037: Columns: [id, name, age, xx, yy]
INFO: HHH000126: Indexes: [primary]
INFO: HHH000232: Schema update complete
INFO: Hibernate: select this_.id as y0_, this_.name as y1_ from MyTable this_
SEVERE: size:4

最佳答案

通常在应用程序中,您不应该每次需要 session 时都构建 session 工厂。这就是应用程序使用 hibernate 的实际需要。如果您想手动管理 session ,那么您可以编写 HibernateUtil 使其成为单例。首先,在静态初始化 block 中构建 session 工厂

  private static final ThreadLocal<Session> threadLocal = new ThreadLocal<>();
  private static SessionFactory sessionFactory;

  static {
    try {
      sessionFactory = configureSessionFactory();
    } catch (Exception e) {
      System.err.println("%%%% Error Creating SessionFactory %%%%");
      e.printStackTrace();
    }
  }

  private HibernateUtil() {
  }

  public static SessionFactory getSessionFactory() {
    return sessionFactory;
  }

  public static Session getSession() throws HibernateException {
    Session session = threadLocal.get();

    if (session == null || !session.isOpen()) {
      if (sessionFactory == null) {
        rebuildSessionFactory();
      }
      session = (sessionFactory != null) ? sessionFactory.openSession() : null;
      threadLocal.set(session);
    }

    return session;
  }

  public static void rebuildSessionFactory() {
    try {
      sessionFactory = configureSessionFactory();
    } catch (Exception e) {
      System.err.println("%%%% Error Creating SessionFactory %%%%");
      e.printStackTrace();
    }
  }

  public static void closeSession() throws HibernateException {
    Session session = (Session) threadLocal.get();
    threadLocal.set(null);

    if (session != null) {
      session.close();
    }
  }

认为这足以添加到您的代码中来运行您的应用程序。

关于java - 将 JDBC 更改为 hibernate 后,应用程序运行速度非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17205301/

相关文章:

jakarta-ee - 如何在 Java EE Web 应用程序中以编程方式获取当前 GPS 位置

java - CDI-- 有条件安装

java - 在 JMS 中使用 JBoss 进行 JNDI 查找错误

java - 有没有办法在 Hazelcast 中使用自定义执行程序?

java - Spring/Jackson 映射内部 JSON 对象

java - 内部包含具有不同组接口(interface)的 bean 的多个 bean 验证

java - Spring Data/Hibernate - 传播生成的 key

java - Hibernate 使用 transient 对象更新实体

java - 如何在 Eclipse 或任何其他平台中有效地格式化巨大的 XML 文件

java - Hibernate JPA 的独特性