java - 使用 JPA hibernate ,连接时间非常慢

标签 java hibernate jpa persistence.xml hibernate.cfg.xml

我有一个项目,我想在其中使用 Hibernate 进行数据库访问。在应用程序中,使用了 JPA api。

persistence.xml文件是这个

<persistence 
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
    <persistence-unit name="hibernate.test"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.ejb.cfgfile" value="/hibernate/hibernate.cfg.xml"/>
        </properties>
    </persistence-unit>
</persistence>

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="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
  <property name="hibernate.connection.password">password</property>
  <property name="hibernate.connection.url">jdbc:oracle:thin:@//server:1521/DBNAME</property>
  <property name="hibernate.connection.username">username</property>
  <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
  <!-- <property name="hibernate.hbm2ddl.auto">verify</property>  -->
  <property name="hibernate.default_catalog">SYS_SOMETHING</property>
  <property name="hibernate.default_schema">SYS_SOMETHING</property>
 </session-factory>
</hibernate-configuration>

问题在于此设置,entityManagerFactory = Persistence.createEntityManagerFactory("hibernate.test");

调用大约需要 25 秒才能完成。

如果我将配置直接移动到 persistence.xml 文件,连接将在 1 秒内完成。 Oracle 和 MySQL 数据库都会出现此错误。

我可以在日志文件中看到延迟,这段时间没有发生任何其他事情

525 [pool-2-thread-1] INFO org.hibernate.cfg.Environment  - HHH000021: Bytecode provider name : javassist
21668 [pool-2-thread-1] DEBUG org.hibernate.service.internal.JaxbProcessor  - cfg.xml document did not define namespaces; wrapping in custom event reader to introduce namespace information

完整日志:http://pastebin.com/4NjPpFPe

如果不使用cfg.xml,这个延迟只有200ms左右。

在此期间,进程内存没有变化,cpu 使用率为 0%,根据 sysinternals Process Monitor,交互为 0。

我想保留此设置,因为 hibernate.cfg.xml 也用于逆向工程和 hibernate 工具。

使用的工具: java8 hibernate -4.3.8 ojdbc7 jpa-2.1

感谢您提前提出任何建议。

更新:

找到解决方案,现在我很开心!

hibernate 框架(或 xml 解析器,我不确定)会等待 http 请求。

要解决这个问题,更换

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<!DOCTYPE hibernate-configuration SYSTEM "-//Hibernate/Hibernate Configuration DTD 3.0//EN">

最佳答案

找到解决方案,现在我很开心!

hibernate 框架(或 xml 解析器,我不确定)会等待 http 请求。

要解决这个问题,更换

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<!DOCTYPE hibernate-configuration SYSTEM "hibernate-configuration-3.0.dtd">

编辑:替换了“with”行,这将在 Release模式和逆向工程中使用正确的 dtd 文件(如果不指定实际文件则无法使用 reveng)。

关于java - 使用 JPA hibernate ,连接时间非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29670916/

相关文章:

java - hibernate : Unable to execute schema management to JDBC target

java - 如何使用内连接按页面请求排序

java - Gradle 在编译任务中使用带有依赖项的 jar

java - 不同 DAO 中的 Spring 事务无论如何都不起作用?

java - 在 Spring Data JPA 中使用注释连接两个以上的表

java - 当将 OrderColumn 与两个列表一起使用时,Hibernate 会抛出 "Repeated column in mapping for entity"

java - 怀疑hibernate JPA中的多对多关系

java - 用于验证 Apple Pay 中的商家的 Tomcat java servlet

java - 为什么 Hibernate query.list() 很慢?

java - 在 Java Breakout Game 中为 JPanel 设置背景图片