由于某些原因,我的hibernate应用程序的启动速度非常慢.(最多2分钟)

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

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

(请注意# comments #)

我还try 了一个更老的Postgres JDBC驱动程序,但一点运气都没有.

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

我不知道我现在可以判断什么来摆脱这种烦恼.任何帮助都将不胜感激.

也许是我的冬眠.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>

编辑:我试图通过日志(log)和分析来找出延迟的原因,但普遍没有成功.(不过我在这方面还不太先进.)最后,我使用了try-and-fail,并更改了一个远程MySQL实例的数据库,以判断是否有任何差异.事实证明,这种联系几乎是立即建立起来的.

推荐答案

Hibernate Slow to Acquire Postgres Connection

hibernate.temp.use_jdbc_metadata_defaults=false

避免在SessionFactory创建期间重新加载元数据.

Postgresql相关问答推荐

计算两个子查询之间的差异

在Docker容器内的Postgres,如何通过Promail将JSON登录到Loki?

为什么我的应用程序接收的是空值而不是布尔值?

如何在PostgreSQL 16中设置&Q;VARSIZE&Q;和&Q;SET_VARSIZE&Q;

为什么 Postgres 中的 now() 和 now()::timestamp 对于 CET 和 CEST 时区如此错误?

postgres 不同类型的更新

Postgres 使用不同元素数据类型的订单数据

在 Postgresql 中实现自定义运算符时出错

EF Core和npgsql连接池已被耗尽

在 postgresql 数据库 timestampz 中保留用户偏移量

当我写 SELECT ~1;在 Postgresql 上它给了我 -2 结果.这是什么原因?它一直持续〜4和-5等

如何从 postgresql Select 查询中的 age() 函数中仅获取年份

如何向文本字段添加长度约束

如何在psql中退出查询结果查看器?

NOT LIKE 带有 NULL 值的行为

为什么 rake db:migrate 有时会在 structure.sql 中添加尾随空格?

PostgreSQL 中跨多个表的索引

PostgreSQL 9 在 Windows 上安装:Unable to write inside TEMP environment path.

IF处或附近的 Postgres 语法错误

JOIN (SELECT ... ) ue ON 1=1?