java 初始化 C3P0 连接池需要 2 分钟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14417692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:12:17  来源:igfitidea点击:

Initializing C3P0 connection pool takes 2 min

javahibernatec3p0

提问by bentrm

I can't wrap my head around why the initialization of a c3p0 connection pool takes 2 min in my Hibernate application.

我无法理解为什么在我的 Hibernate 应用程序中初始化 c3p0 连接池需要 2 分钟。

This is in my Hibernate.cfg.xml:

这是在我的 Hibernate.cfg.xml 中:

<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">1</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."></property>

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

        <property name="hbm2ddl.auto">create</property>
 </session-factory>
</hibernate-configuration>

The connection settings are set in my HibernateUtil file when building the session factory.

在构建会话工厂时,连接设置在我的 HibernateUtil 文件中设置。

The pool is initialize when the first transaction in my tests is openend. Connecting and querying the db works just fine afterwards, it only hangs on the following line for a while before it will start. I formated the output a bit as I assume the problem may be with one of the settings mentioned here.:

当我的测试中的第一个事务是 openend 时,池被初始化。之后连接和查询数据库工作正常,它只会在它开始之前挂在下面的行上一段时间。我对输出进行了一些格式化,因为我认为问题可能与此处提到的设置之一有关。:

INFO: Initializing c3p0 pool... 
com.mchange.v2.c3p0.PoolBackedDataSource@30670080 [
  connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@ecfec4d0 [
    acquireIncrement -> 1,
    acquireRetryAttempts -> 30,
    acquireRetryDelay -> 1000,
    autoCommitOnClose -> false,
    automaticTestTable -> null,
    breakAfterAcquireFailure -> false,
    checkoutTimeout -> 0,
    connectionCustomizerClassName -> null,
    connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester,        
    debugUnreturnedConnectionStackTraces -> false,
    factoryClassLocation -> null,
    forceIgnoreUnresolvedTransactions -> false,
    identityToken -> I-REMOVED-THIS,
    idleConnectionTestPeriod -> 3000,
    initialPoolSize -> 3,
    maxAdministrativeTaskTime -> 0,
    maxConnectionAge -> 0,
    maxIdleTime -> 300,
    maxIdleTimeExcessConnections -> 0,
    maxPoolSize -> 10,
    maxStatements -> 50,
    maxStatementsPerConnection -> 0,
    minPoolSize -> 3,
    nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@b17e5c65 [
      description -> null,
      driverClass -> null,
      factoryClassLocation -> null,
      identityToken -> I-REMOVED-THIS,
      jdbcUrl -> jdbc:postgresql://URL-TO-MY_DB,
      properties -> {user=******, password=******, default_schema=}
    ],
    preferredTestQuery -> null,
    propertyCycle -> 0,
    testConnectionOnCheckin -> false,
    testConnectionOnCheckout -> false,
    unreturnedConnectionTimeout -> 0,
    usesTraditionalReflectiveProxies -> false;
    userOverrides: {}
  ],
  dataSourceName -> null,
  factoryClassLocation -> null,
  identityToken -> I-REMOVED-THIS,
  numHelperThreads -> 3
]

It's the first time I'm using Hibernate and c3p0 and I was expecting it to be much quicker when starting the pool? Is it a misconception of mine?

这是我第一次使用 Hibernate 和 c3p0,我希望在启动池时它会更快?这是我的错觉吗?

It's no difference between using the remote DB nor a local PostgreSQL instance.

使用远程数据库和本地 PostgreSQL 实例没有区别。

(Edit: This is not true. I made a mistake when comparing local and remote db server. Locally, initialization is pretty much immediately, remotely it takes around 2 minutes.)

(编辑:这不是真的。我在比较本地和远程数据库服务器时犯了一个错误。在本地,初始化几乎是立即的,远程需要大约 2 分钟。)

Edit2: Hereis a log of the connection process.

Edit2:是连接过程的日志。

回答by fabriciobc

Set the property Hibernate.temp.use_jdbc_metadata_defaults to false in the configuration of your session factory. This will indicate to Hibernate using metadata dialect instead of the connection, which makes the slow startup. You must also configure an appropriate dialect for your driver.

在会话工厂的配置中将属性 Hibernate.temp.use_jdbc_metadata_defaults 设置为 false。这将指示 Hibernate 使用元数据方言而不是连接,这会导致启动缓慢。您还必须为您的驱动程序配置适当的方言。