Persistence.createEntityManager() 在 Hibernate 中非常慢

发布于 2024-10-17 22:12:47 字数 1357 浏览 2 评论 0原文

我的 Eclipse RCP 应用程序遇到了巨大的性能问题,显然是由 Hibernate 引起的。更具体地说,应用程序的启动时间非常长(最多 2 分钟) - 分析和调试表明创建 Hibernate/JPA EntityManagerFactory(在启动时发生)需要非常长的时间。

我对这些设置进行了很多尝试,例如使用文件数据库而不是内存中的数据库以及不使用 hbm2ddl,但没有成功。有人遇到过这样的问题吗?

我正在使用 JDK 1.6.20、Hibernate 3.5 和 HSQLDB 1.8(内存配置)。

下面是我的 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="my_db" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <!-- about 20 classes; skipped for brevity -->
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
            <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:my_db"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        </properties>
    </persistence-unit>    
</persistence>

I'm having huge performance issues with my Eclipse RCP application, apparently caused by Hibernate. To be more specific, the application takes extremely long to start up (up to 2 minutes) - profiling and debugging showed that creating a Hibernate/JPA EntityManagerFactory (which happens at startup) takes extremely long.

I've played around a lot with the settings, such as using a file database rather than in-memory and not using hbm2ddl, but without success. Has anyone ever experienced problems like these?

I'm using JDK 1.6.20, Hibernate 3.5 and HSQLDB 1.8 (in-memory configuration).

Below is my persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="my_db" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <!-- about 20 classes; skipped for brevity -->
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
            <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:my_db"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        </properties>
    </persistence-unit>    
</persistence>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梨涡少年 2024-10-24 22:12:47
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

根据您拥有的实体数量,创建所有表、索引等可能需要一些时间。我会更改为另一个数据库(或非内存数据库),将 create-drop 替换为“create”,运行一次,完全删除该属性(这样它就不会尝试创建已经存在的表)并且再次运行它。 最后一步比您当前的配置更接近 Hibernate 花费的实际时间。

        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

Depending on how many entities you have, it may take some time to create all the tables, indexes and alikes. I'd change to another DB (or non in-memory db), replace the create-drop to "create", run it once, remove the property entirely (so that it doesn't try to create the already existing tables) and run it again. This last step is closer to the actual time spent by Hibernate than your current configuration.

秋心╮凉 2024-10-24 22:12:47

如此长的延迟也可能是网络超时的迹象。根据这个答案,hibernate应该验证persistence.xml针对 xml 模式的嵌入副本。也许是出于某种原因尝试从互联网获取 xsd。您能否获得实体管理器创建卡住的线程转储或更详细的配置文件?

Such a long delay could also be a sign of a network timeout. According to this answer, hibernate should validate the persistence.xml against an embedded copy of the xml schema. Maybe it is for some reason trying to fetch the xsd from the internet. Can you get a thread dump or a more detailed profile where the entity manager creation is stuck?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文