问答

hibernate运行很慢?查一张只有几条记录的表都要一俩分钟。

作者:admin 2021-08-04 我要评论

问题描述 hibernate运行很慢?查一张只有几条记录的表都要一俩分钟。 虽然结果能出,但是在这个界面停留有一俩分钟,就5条数据。 问题出现的环境背景及自己尝试...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

问题描述

hibernate运行很慢?查一张只有几条记录的表都要一俩分钟。
虽然结果能出,但是在这个界面停留有一俩分钟,就5条数据。
image.png
image.png

问题出现的环境背景及自己尝试过哪些方法

环境:eclipse 2020.3、oracle 12c、java 8
jar包:image.png

相关代码

学生类:
package entity;

import java.util.Date;

public class Student {
    private String stuNo;

    public String getStuNo() {
        return stuNo;
    }

    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    private String stuName;
    private String password;
    private String address;
    private Date birthday;

    @Override
    public String toString() {
        return "Student [stuNo=" + stuNo + ", stuName=" + stuName + ", password=" + password + ", address=" + address
                + ", birthday=" + birthday + "]";
    }

    public Student(String stuNo, String stuName, String password, String address, Date birthday) {
        super();
        this.stuNo = stuNo;
        this.stuName = stuName;
        this.password = password;
        this.address = address;
        this.birthday = birthday;
    }

    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }

}
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>
        <property name="connection.username">c##zqf</property>
        <property name="connection.password">zqf123</property>
        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
        
        <!-- 每个数据库都有对应的Dialect以匹配平台特征 -->
        <property name="dialect">org.hibernate.dialect.Oracle9iDialect</property>
        
         <!--数据库连接池 C3P0 添加C3P0依赖-->
        <property name="c3p0.max_size">2</property>
        <property name="c3p0.min_size">2</property>
        <property name="c3p0.timeout">5000</property>
        <property name="c3p0.max_statements">100</property>
        <property name="c3p0.idle_test_period">3000</property>
        <property name="c3p0.acquire_increment">2</property>
        <property name="c3p0.validate">false</property>
        
         <!--显示Hibernate自动生成的SQL-->
        <property name="show_sql">true</property>
        
        <property name="current_session_context_class">thread</property>
        
        <mapping resource="entity/Student.hbm.xml"/>    
    </session-factory>
</hibernate-configuration>
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 2020-5-12 15:48:35 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="entity.Student" table="STUDENT">
        <id name="stuNo" type="java.lang.String">
            <column name="STUNO" />
            <generator class="assigned" />
        </id>
        <property name="stuName" type="java.lang.String">
            <column name="STUDENTNAME" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="PASSWD" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="ADDRESS" />
        </property>
        <property name="birthday" type="java.util.Date">
            <column name="BIRTHDAY" />
        </property>
    </class>
</hibernate-mapping>
测试代码:
package test;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import entity.Student;

public class Test2 {
    public static void main(String[] args) {
        // 1.获取Configuration对象
        Configuration configuration = null;
        // 2.sessionFactory对象
        SessionFactory sessionFactory = null;
        // 3.获取session对象
        Session session = null;
        // 4.获取事务对象Transaction
        Transaction tx = null;

        try {
            // 1.获取Configuration对象
            configuration = new Configuration().configure();
            // 2.sessionFactory对象
            sessionFactory = configuration.buildSessionFactory();
            // 3.获取session对象
            session = sessionFactory.openSession();
            // 4.获取事务对象Transaction
            tx = session.beginTransaction();

            String hql = "from Student";
            Query qurey = session.createQuery(hql);

            List<Student> list = session.createQuery(hql).list();
            for (Student student : list) {
                System.out.println(student.getStuName());
            }

            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
}

2020/5/26更新:

加了运行时间,其他地方没改

Query qurey = session.createQuery(hql);
List<Student> list = session.createQuery(hql).list();
end = System.currentTimeMillis();
System.out.println("start time:" + start + "; end time:" + end + "; Run Time:" + (end - start) + "(ms)");

结果更奇怪了:才262ms啊
image.png

5/26再次更新:

测试文件改成:
package test;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import entity.Student;

public class Test2 {
    public static void main(String[] args) {
        // 1.获取Configuration对象
        Configuration configuration = null;
        // 2.sessionFactory对象
        SessionFactory sessionFactory = null;
        // 3.获取session对象
        Session session = null;
        // 4.获取事务对象Transaction
        Transaction tx = null;

        try {
            long[] start = new long[10];
            long[] end = new long[10];
            start[6] = System.currentTimeMillis();
            start[0] = System.currentTimeMillis();

            // 1.获取Configuration对象
            System.out.println();
            configuration = new Configuration().configure();
            end[0] = System.currentTimeMillis();
            System.out.println("语句:configuration = new Configuration().configure();");
            System.out.println(
                    "start time:" + start[0] + "; end time:" + end[0] + "; Run Time:" + (end[0] - start[0]) + "(ms)");

            // 2.sessionFactory对象
            System.out.println();
            start[1] = System.currentTimeMillis();
            sessionFactory = configuration.buildSessionFactory();
            end[1] = System.currentTimeMillis();
            System.out.println("语句:sessionFactory = configuration.buildSessionFactory();");
            System.out.println(
                    "start time:" + start[1] + "; end time:" + end[1] + "; Run Time:" + (end[1] - start[1]) + "(ms)");

            // 3.获取session对象
            System.out.println();
            start[2] = System.currentTimeMillis();
            session = sessionFactory.openSession();
            end[2] = System.currentTimeMillis();
            System.out.println("语句:session = sessionFactory.openSession();");
            System.out.println(
                    "start time:" + start[2] + "; end time:" + end[2] + "; Run Time:" + (end[2] - start[2]) + "(ms)");

            // 4.获取事务对象Transaction
            System.out.println();
            start[3] = System.currentTimeMillis();
            tx = session.beginTransaction();
            end[3] = System.currentTimeMillis();
            System.out.println("语句:tx = session.beginTransaction();");
            System.out.println(
                    "start time:" + start[3] + "; end time:" + end[3] + "; Run Time:" + (end[3] - start[3]) + "(ms)");

            String hql = "from Student";
            System.out.println();

            start[4] = System.currentTimeMillis();
            Query qurey = session.createQuery(hql);
            end[4] = System.currentTimeMillis();
            System.out.println("语句:Query qurey = session.createQuery(hql);");
            System.out.println(
                    "start time:" + start[4] + "; end time:" + end[4] + "; Run Time:" + (end[4] - start[4]) + "(ms)");

            System.out.println();
            start[5] = System.currentTimeMillis();
            List<Student> list = session.createQuery(hql).list();
            end[5] = System.currentTimeMillis();
            System.out.println("语句:Query qurey = session.createQuery(hql);");
            System.out.println(
                    "start time:" + start[5] + "; end time:" + end[5] + "; Run Time:" + (end[5] - start[5]) + "(ms)");

            System.out.println();
            end[6] = System.currentTimeMillis();
            System.out.println("以上语句运行总时间:");
            System.out.println("Run Time:" + (end[6] - start[6]) + "(ms)");
            System.out.println();
            for (Student student : list) {
                System.out.println(student.getStuName());
            }

            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
}

运行结果:


五月 26, 2020 9:45:17 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.11.Final}
语句:configuration = new Configuration().configure();
start time:1590457516601; end time:1590457517467; Run Time:866(ms)

五月 26, 2020 9:45:17 上午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
五月 26, 2020 9:45:18 上午 org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator instantiateC3p0Provider
WARN: HHH000022: c3p0 properties were encountered, but the c3p0 provider class was not found on the classpath; these properties are going to be ignored.
五月 26, 2020 9:45:18 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
五月 26, 2020 9:45:18 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [oracle.jdbc.OracleDriver] at URL [jdbc:oracle:thin:@127.0.0.1:1521:orcl]
五月 26, 2020 9:45:18 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=c##zqf, password=****}
五月 26, 2020 9:45:18 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
五月 26, 2020 9:45:18 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
五月 26, 2020 9:45:20 上午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle9iDialect
语句:sessionFactory = configuration.buildSessionFactory();
start time:1590457517467; end time:1590457588530; Run Time:71063(ms)

语句:session = sessionFactory.openSession();
start time:1590457588531; end time:1590457588623; Run Time:92(ms)

语句:tx = session.beginTransaction();
start time:1590457588623; end time:1590457588627; Run Time:4(ms)

语句:Query qurey = session.createQuery(hql);
start time:1590457588628; end time:1590457588775; Run Time:147(ms)

Hibernate: select student0_.STUNO as stuno1_0_, student0_.STUDENTNAME as studentname2_0_, student0_.PASSWD as passwd3_0_, student0_.ADDRESS as address4_0_, student0_.BIRTHDAY as birthday5_0_ from STUDENT student0_
语句:Query qurey = session.createQuery(hql);
start time:1590457588775; end time:1590457588837; Run Time:62(ms)

以上语句运行总时间:
Run Time:72236(ms)

张三
kryon
Tom
Jack
JackMa

image.png
image.png

###
Query qurey = session.createQuery(hql);
List<Student> list = session.createQuery(hql).list();

这两句上下打印下时间,统计下到底执行了多久

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • hibernate运行很慢?查一张只有几条记

    hibernate运行很慢?查一张只有几条记

  • nextjs9 加载 antd4 国际化语言包报错

    nextjs9 加载 antd4 国际化语言包报错

  • vue  css预设器配置项 无效如何处理

    vue css预设器配置项 无效如何处理

  • django调用sqlite:只读取现有的数据库

    django调用sqlite:只读取现有的数据库

腾讯云代理商
海外云服务器