【问题标题】:Hibernate startup very slowHibernate 启动很慢
【发布时间】:2013-01-04 22:23:02
【问题描述】:

由于某种原因,我的休眠应用程序的启动速度非常慢。 (最多 2 分钟) 我一直认为 c3p0 配置完全错误(related question),但研究日志显示,在与服务器建立连接后没有任何活动。此外,使用 Hibernate 的内置轮询功能显示相同的结果。

这是日志中的一个 sn-p:

20:06:51,248 DEBUG BasicResourcePool:422 - decremented pending_acquires: 0
20:06:51,248 DEBUG BasicResourcePool:1644 - trace com.mchange.v2.resourcepool.BasicResourcePool@1acaf0ed [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@5f873eb2)
20:06:51,248 DEBUG BasicResourcePool:1644 - trace com.mchange.v2.resourcepool.BasicResourcePool@1acaf0ed [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@5f873eb2)
20:06:51,273 DEBUG JdbcServicesImpl:121 - Database ->
       name : PostgreSQL
    version : 9.1.6
      major : 9
      minor : 1
20:06:51,274 DEBUG JdbcServicesImpl:127 - Driver ->
       name : PostgreSQL Native Driver
    version : PostgreSQL 9.2 JDBC4 (build 1002)
      major : 9
      minor : 2
20:06:51,274 DEBUG JdbcServicesImpl:133 - JDBC version : 4.0 ##### HANGS FOR 2 MINUTES  ON THIS LINE #####
20:08:14,727  INFO Dialect:123 - HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
20:08:14,736  INFO LobCreatorBuilder:120 - HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
20:08:14,736 DEBUG GooGooStatementCache:297 - checkinAll(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 0; checked out: 0; num connections: 0; num keys: 0
20:08:14,736 DEBUG GooGooStatementCache:297 - checkinAll(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 0; checked out: 0; num connections: 0; num keys: 0
20:08:14,883 DEBUG BasicResourcePool:1644 - trace com.mchange.v2.resourcepool.BasicResourcePool@1acaf0ed [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@5f873eb2)
20:08:14,883 DEBUG BasicResourcePool:1644 - trace com.mchange.v2.resourcepool.BasicResourcePool@1acaf0ed [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@5f873eb2)
20:08:14,883 DEBUG GooGooStatementCache:297 - checkinAll(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 0; checked out: 0; num connections: 0; num keys: 0

(请注意#comment#。)

我还尝试了一个较旧的 Postgres JDBC 驱动程序,但没有任何运气。

连接到本地数据库就可以了。立即建立连接,我可以查询数据库。这个远程数据库是一个 Heroku 开发实例。我也用另一个遥控器试了一下。同样的结果。

我不知道现在可以检查什么来摆脱这种烦恼。任何帮助将不胜感激。

也许我的 hibernate.cfg.xml 有帮助:

http://www.hibernate.org/dtd/hibernate-

configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url"/>
        <property name="connection.default_schema"/>
        <property name="connection.username"/>
        <property name="connection.password"/> 

        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
            <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
        <property name="current_session_context_class">thread</property>            
        <property name="hibernate.c3p0.acquire_increment">3</property>
        <property name="hibernate.c3p0.min_size">3</property>
        <property name="hibernate.c3p0.max_size">10</property>
        <property name="hibernate.c3p0.timeout">300</property>
        <property name="hibernate.c3p0.max_statements">50</property>
        <property name="hibernate.c3p0.idle_test_period">3000</property>
        <property name="hibernate.c3p0.acquireRetryDelay">500</property>

        <property name="show_sql">true</property>
        <property name="format_sql">false</property>

        <property name="hbm2ddl.auto">validate</property>

        <mapping class="core.entities.Exam" />
        <mapping class="core.entities.Examination" />
        ...
    </session-factory>
</hibernate-configuration>

编辑:我试图通过日志和分析找出延迟的原因,但一直没有成功。 (虽然我在这方面并不那么先进。)最后,我确实尝试并失败了,并为远程 MySQL 实例更改了我的数据库,以检查是否发生任何差异。事实证明,连接几乎是立即建立的。

【问题讨论】:

  • 试试分析器,否则你只是在猜测它为什么这么慢。
  • 只是预感,但您可能想检查您的网络,也许是 DNS?尝试使用服务器的 IP 而不是服务器名称。听起来有点像是在等待超时。
  • 是的,这听起来像是网络问题

标签: java hibernate postgresql jdbc postgresql-9.1


【解决方案1】:

Hibernate Slow to Acquire Postgres Connection

hibernate.temp.use_jdbc_metadata_defaults=false

为了避免在 SessionFactory 创建期间重新加载元数据。

【讨论】:

  • 这看起来真的很像答案。不幸的是,我现在无法检查。想知道如果我将其标记为答案是否可以将人们指向这个方向。这个问题还是有很多人关注的。
  • 我建议接受这个作为答案,因为它在类似情况下对我有用。
  • 这将我的启动时间从 40 秒缩短到 14 秒。将节省数小时。
  • 在 hibernate.cfg.xml 中:&lt;property name="hibernate.temp.use_jdbc_metadata_defaults"&gt;false&lt;/property&gt;
【解决方案2】:

对于 Postgres,添加应用程序配置:

spring.jpa.database-platform = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false

如果不能确定方言,第一行是必要的

结果

之前:

09:10:19.637 [main] INFO  o.h.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
09:14:17.159 [main] INFO  org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL9Dialect

~4 分钟

之后:

09:40:10.930 [main] INFO  o.h.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
09:40:11.043 [main] INFO  org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect

~1 分钟

【讨论】:

【解决方案3】:

启动缓慢可能是这个配置造成的:

<property name="hbm2ddl.auto">update</property>

这个配置意味着当hibernate启动时,检查实体是否与ddl匹配,并执行'create','update'等操作。这将花费太多时间。

所以解决方案是评论这个配置。然后休眠将在没有验证的情况下启动。

【讨论】:

    【解决方案4】:

    当我将数据库从 10g 更新到 11 时,我遇到了这个问题。 我在代码中发现了以下设置:

        hibernateProperties.setProperty("hibernate.dialect","org.hibernate.dialect.Oracle10gDialect");
    

    不得不改变方言如下:

        hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle11gDialect");
    

    在某一时刻,这不再对我有用,我不得不更改以下内容,以免卡在“HHH000400:使用方言:org.hibernate.dialect.Oracle10gDialect”:

    hibernateProperties.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
    

    此外,我必须注释掉以下内容,以免卡在“HV000001:Hibernate Validator 6.1.5.Final”:

            // hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "validate");
    

    【讨论】:

      【解决方案5】:

      如果它异常缓慢,那么您的应用程序可能有锁,或者一些资源块。在任何情况下,下载 VisualVM(JDK 包括 jconsole,它的简化版本)并检查您的线程在做什么,它们被卡在哪里(threaddump),如果没有给出任何快速答案,请打开分析器。

      【讨论】:

        【解决方案6】:

        您使用的是什么容器? c3p0 应该安装在容器中,例如 Tomcat。如果您正在运行单元测试,请不要使用连接池。如果将其放入 tomcat 中,则使用 Resource 标记执行此操作,然后使用 JNDI 连接到它。最好的方法。

        【讨论】:

          猜你喜欢
          • 2010-12-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-22
          • 1970-01-01
          • 1970-01-01
          • 2012-05-04
          • 2011-01-01
          相关资源
          最近更新 更多