2013-08-02 93 views
3

的persistence.xml如何在JPA/Hibernate應用程序中配置連接池(無Spring)?

<properties> 
     <property name='javax.persistence.jdbc.driver' value='org.postgresql.Driver' /> 
     <property name='javax.persistence.jdbc.url' 
      value='jdbc:postgresql://192.168.19.66:5432/service1' /> 
     <property name='javax.persistence.jdbc.user' value='postgres' /> 
     <property name='javax.persistence.jdbc.password' value='root' /> 
     <property name='hibernate.dialect' value='org.hibernate.dialect.PostgreSQLDialect' /> 
     <property name='hibernate.connection.shutdown' value='true' /> 
     <property name='hibernate.hbm2ddl.auto' value='none' /> 
     <property name='hibernate.show_sql' value='false' /> 
     <property name='hibernate.format_sql' value='false' /> 
        <!-- Connection Pooling --> 
     <property name="hibernate.connection.provider_class" 
      value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" /> 
     <property name="hibernate.c3p0.max_size" value="100" /> 
     <property name="hibernate.c3p0.min_size" value="5" /> 
     <property name="hibernate.c3p0.acquire_increment" value="5" /> 
     <property name="hibernate.c3p0.idle_test_period" value="500" /> 
     <property name="hibernate.c3p0.max_statements" value="50" /> 
     <property name="hibernate.c3p0.timeout" value="10000" /> 
    </properties> 

異常

[C3P0PooledConnectionPoolManager[identityToken->1hge4qw8wh4gndjoawud4|b81be4]-AdminTaskTimer] DEBUG com.mchange.v2.async.ThreadPoolAsynchronousRunner - com[email protected]5d1aef -- Running DeadlockDetector[Exiting. No pending tasks.] 
[C3P0PooledConnectionPoolManager[identityToken->1hge4qw8wh4gndjoawud4|b81be4]-AdminTaskTimer] DEBUG com.mchange.v2.resourcepool.BasicResourcePool - Continuing acquisition series. pending_acquires [5], attempts_remaining: 20 
[C3P0PooledConnectionPoolManager[identityToken->1hge4qw8wh4gndjoawud4|b81be4]-AdminTaskTimer] DEBUG com.mchange.v2.async.ThreadPoolAsynchronousRunner - [email protected]: Adding task to queue -- com[email protected]934847 
[C3P0PooledConnectionPoolManager[identityToken->1hge4qw8wh4gndjoawud4|b81be4]-HelperThread-#0] DEBUG com.mchange.v2.resourcepool.BasicResourcePool - An exception occurred while acquiring a poolable resource. Will retry. 
java.sql.SQLException: No suitable driver 
    at java.sql.DriverManager.getDriver(DriverManager.java:264) 
    at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:240) 
    at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:146) 
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:195) 
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:184) 
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:200) 
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1086) 
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1073) 
    at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:44) 
    at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1810) 
    at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:648) 

獲取例外值java.sql.SQLException:沒有合適的驅動程序

當我查看源代碼時,它正在尋找屬性「hibernate.connection.driver_class」。我已經在「javax.persistence.jdbc.driver」中提到了驅動程序類。

我想我沒有做正確的方式來配置連接池。該應用程序使用hibernate 4.1.11(不含spring),JPA2,Postgresql 9.1。沒有hibernate.cfg.xml。

問題

  • 哪些是休眠/ JPA應用程序配置連接池的方式。
  • 我已經在javax.persistence.jdbc.driver中提供了嗎?如果我提供hibernate.connection.driver_class屬性,連接池和JPA連接創建的連接是相同還是不同?

注意

  • PostgreSQL驅動在類路徑中。
  • 如果我在<!-- Connection Pooling -->之後刪除行,它會運行。但是,如果我在連接池之後添加<!-- Connection Pooling -->行,我得到這個異常。
+0

是PostgreSQL驅動在類路徑後? –

+0

是的。如果我在<! - Connection Pooling - >之後刪除行。它運行。但是,如果我在<! - 連接池 - >連接池之後添加行。我得到這個例外。 – Shashi

+0

設置hibernate.connection.driver_class屬性有什麼問題? –

回答

0

回到過去,您必須使用Class.forName(org.postgresql.Driver.class)結構加載相應的驅動程序類。使用JDBC 4.0驅動程序不再需要 - 但您可以嘗試一下。

或者,嘗試使用-Djdbc.drivers=org.postgresql.Driver開始您的應用程序,它幾乎與Class.forName片段相同,但不要求您對驅動程序類進行硬編碼。希望能幫助到你。

+0

謝謝。這沒有幫助。我已經解決了我在答案中發佈的這個問題。 – Shashi

相關問題