我有一个项目,想要使用Hibernate进行数据库访问。在应用程序中,使用了JPA API。
持久性(persistence.xml)文件如下:
hibernate.cfg.xml文件如下:
问题出在这个设置上,即
如果我直接将配置移动到persistence.xml文件中,则连接可以在1秒钟内完成。这个错误发生在Oracle和MySQL数据库中。
我可以在日志文件中看到延迟,此时没有其他活动正在进行。
完整日志:http://pastebin.com/4NjPpFPe 如果不使用cfg.xml,则此延迟仅约为200毫秒。
在此期间,处理器内存不会改变,CPU使用率为0%,根据Sysinternals Process Monitor的记录,交互次数为0。
我想保留这个设置,因为hibernate.cfg.xml用于反向工程和Hibernate工具。
使用的工具: java8 hibernate-4.3.8 ojdbc7 jpa-2.1
提前感谢您的任何建议。
更新:
找到解决方案,现在我很高兴!
Hibernate框架(或XML解析器,我不确定)将等待HTTP请求。
要解决问题,请替换
持久性(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,则此延迟仅约为200毫秒。
在此期间,处理器内存不会改变,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">