Hibernate启动很慢 [英] Hibernate startup very slow

查看:325
本文介绍了Hibernate启动很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



以下是日志中的一个片段:

  20:06:51,248 DEBUG BasicResourcePool:422  - 递减pending_acquires:0 
20:06:51,248 DEBUG BasicResourcePool:1644 - trace com.mchange.v2.resourcepool。 BasicResourcePool @ 1acaf0ed [managed:3,unused:2,excluded:0](例如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](例如com.mchange.v2.c3p0.impl.NewPooledConnection@5f873eb2)
20:06:51,273 DEBUG JdbcServicesImpl:121 - 数据库 - >
名称:PostgreSQL
版本:9.1.6
major:9
minor:1
20:06:51,274 DEBUG JdbcServicesImpl:127 - 驱动程序 - >
名称:PostgreSQL Native Driver
版本:PostgreSQL 9.2 JDBC4(内部版本1002)
主要版本:9
minor:2
20:06:51,274 DEBUG JdbcServicesImpl:133 - JDBC版本:4.0 #####在此行上悬挂2分钟#####
20:08:14,727 INFO方言:123 - HHH000400:使用方言:org.hibernate.dialect.PostgreSQLDialect
20:08:14,736 INFO LobCreatorBuilder:120 - HHH000424:禁用上下文LOB创建为createClob()方法抛出错误:java.lang.reflect.InvocationTargetException
20:08:14,736 DEBUG GooGooStatementCache:297 - checkinAll(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats - total size:0;签出:0;数字连接数:0;数字键:0
20:08:14,736 DEBUG GooGooStatementCache:297 - checkinAll():com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats - total size:0;签出:0;数字连接数:0; num key:0
20:08:14,883 DEBUG BasicResourcePool:1644 - trace com.mchange.v2.resourcepool.BasicResourcePool@1acaf0ed [managed:3,unused:2,excluded:0](例如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](eg 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 ;签出:0;数字连接数:0;数字键:0



<请注意#comment#。)



我也尝试了一个老的Postgres JDBC驱动程序,没有任何运气。



连接到本地数据库工作得很好。连接立即建立,我可以查询数据库。这个远程数据库是一个Heroku dev实例。我也尝试了与另一个遥控器。同样的结果。



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



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




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

  configuration-3.0.dtd> 
< hibernate-configuration>
< session-factory>
<属性名称=connection.driver_class> org.postgresql.Driver< / property>
<属性名称=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>线程< / property>
< property name =hibernate.c3p0 。获得_increment→3< /性>
< 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>验证< / property>

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

编辑:我试图通过日志和分析找到延迟的原因,但是已经广泛失败它。 (尽管我在这方面还没有那么先进)。最后,我尝试着尝试并失败,并将我的数据库更改为远程MySQL实例,以检查是否有任何差异。原来,这个连接几乎立即建立起来。 解决方案

请参阅 Hibernate慢速获取Postgres连接

hibernate。 temp.use_jdbc_metadata_defaults = false


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


For some reason, the startup of my hibernate application is unbarrably slow. (up to 2 min) I have been thinking that the c3p0 configuration is plain wrong (related question) but studying the logs shows, that there is no activity just after the connection to the server is established. Also, using the built-in polling capabilities of Hibernate shows the same result.

Here is a snippet from the logs:

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

(Please mind the #comment#.)

I also tried an older Postgres JDBC Driver with no luck whatsoever.

Connecting to a local Database works just fine. Connection is established immediately and I can query the database. This remote db is a Heroku dev instance. I tried it with another remote as well. Same outcome.

I'm out of ideas what I can check now to get rid of this annoyance. Any help would be much appreciated.

Maybe my hibernate.cfg.xml is helpful:

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>

EDIT: I tried to find the reason for the delay via logs and profiling but have been widely unsuccessful with it. (I'm not that advanced in this area though.) In the end I did go with try and fail and changed my db for a remote MySQL instance to check if any difference occurs. Turns out, that the connection is established nearly immediately.

解决方案

See Hibernate Slow to Acquire Postgres Connection

hibernate.temp.use_jdbc_metadata_defaults=false

To avoid meta-data reload during SessionFactory creation.

这篇关于Hibernate启动很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆