1

我正在使用 Hibernate 4.3.5 我正在尝试连接到数据库(Oracle)并且我尝试使用c3p0-0.9.1.

我设置了以下属性以启用连接池c3p0-0.9.1

  <property  name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>         
  <property name="hibernate.c3p0.min_size">0</property>
  <property name="hibernate.c3p0.max_size">5</property>
  <property name="hibernate.c3p0.timeout">300</property>
  <property name="hibernate.c3p0.max_statements">0</property>
  <property name="hibernate.c3p0.idle_test_period">300</property>

设置这些属性后,将记录以下信息,我无法连接到数据库。

     09:58:39,573  INFO MLog:80 - MLog clients using log4j logging.
     09:58:39,698  INFO C3P0Registry:204 - Initializing c3p0-0.9.1 [built 16-January-2007 14:46:42; debug? true; trace: 10]
     09:58:39,854  INFO AbstractPoolBackedDataSource:462 - Initializing c3p0 pool...  com.mchange.v2.c3p0.PoolBackedDataSource@7eaf7e [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@6e3303f6 [ acquireIncrement -> 3, 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 -> 2xstr79341a29wl20dow|45ef76, idleConnectionTestPeriod -> 300,  initialPoolSize -> 5, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 100, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 5, nestedDataSource ->  com.mchange.v2.c3p0.DriverManagerDataSource@f55211aa [ description -> null, 
     driverClass -> null,  factoryClassLocation -> null, identityToken ->  2xstr79341a29wl20dow|16d0064, jdbcUrl ->  `**Driver URL**`, properties -> {user=******, password=******} ],  preferredTestQuery -> null, propertyCycle -> 0, testConnectionOnCheckin -> false,  testConnectionOnCheckout -> false,   unreturnedConnectionTimeout -> 0,  usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null,     factoryClassLocation -> null, identityToken -> 2xstr79341a29wl20dow|11400c8, numHelperThreads -> 3 ]
         09:58:59,854  WARN ThreadPoolAsynchronousRunner:608 -  com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@13780ae -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
         09:58:59,854  WARN ThreadPoolAsynchronousRunner:624 -    com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@13780ae -- APPARENT   DEADLOCK!!!     
    Complete Status: 
    Managed Threads: 3
    Active Threads: 3
    Active Tasks: 
      com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1e105a1 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0)
      com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@389632 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2)
      com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@10cd3b6 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1)
Pending Tasks: 
      com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@11f5d8c
      com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@36b8db
       Pool thread stack traces:
          Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main]
          java.lang.Thread.sleep(Native Method)
        com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)
    com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
     Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main]
     java.lang.Thread.sleep(Native Method)
     com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)
    com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
        Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main]
        java.lang.Thread.sleep(Native Method)
       com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)
    com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)

在这个可怕的日志记录之后,我得到了以下异常

     09:59:18,135  WARN BasicResourcePool:1841 - com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@10cd3b6 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: 
                java.sql.SQLException: ORA-01017: invalid username/password; logon denied

                       at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
                       at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
                       at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:283)
                       at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:278)
                       at oracle.jdbc.driver.T4CTTIoauthenticate.receiveOauth(T4CTTIoauthenticate.java:785)
                       at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:362)
                       at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
                       at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
                       at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
                        at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
                       at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:135)
                       at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
                       at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
                       at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
                       at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
                       at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
                       at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
                       at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
        HibernateLog --> 09:59:18 WARN  org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL  Error: 0, SQLState: null
       09:59:18,135  WARN SqlExceptionHelper:144 - SQL Error: 0, SQLState: null
       HibernateLog --> 09:59:18 ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Connections could not be acquired from the underlying database!
       09:59:18,135 ERROR SqlExceptionHelper:146 - Connections could not be acquired from the underlying database!
       09:59:18,135 ERROR LoginDAO:111 - GetHostAddrCould not open connection
      org.hibernate.exception.GenericJDBCException: Could not open connection
           at   org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
           at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
           at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
           at  org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:235)
           at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:171)
           at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.connection(StatementPreparerImpl.java:63)
            at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:162)
            at org .hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:186)
            at  org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:160)
           at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1884)
           at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1861)
           at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
           at org.hibernate.loader.Loader.doQuery(Loader.java:909)
           at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
           at org.hibernate.loader.Loader.doList(Loader.java:2553)
           at org.hibernate.loader.Loader.doList(Loader.java:2539)
            at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
           at org.hibernate.loader.Loader.list(Loader.java:2364)
           at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126)
           at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1682)
           at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)

**这里数据库的用户名和密码绝对正确**

在此异常之后,我无法连接到数据库,但在我的日志中,我的应用程序正在尝试连接到数据库,因为我可以在控制台中看到相同类型的日志记录。

以下是我的配置。

        c3p0-0.9.1.jar
        Hibernate-c3p0-4.3.5.Final.jar
        Struts2 -fullhibernatecoreplugin-2.2.2-GA.jar

我正在使用struts2Hibernate 4.3.5。有人可以帮我解决这个问题。我用谷歌搜索中音,但找不到合适且生动的解决方案。

Ans 如果我不使用连接池,我可以连接到数据库并且我的应用程序运行良好c3p0

提前致谢。

编辑:**我正在使用混合属性配置休眠就像我通过 java 程序传递用户名和密码一样。**我愚蠢的猜测是c3p0没有找到由 java 程序设置的用户名和密码,因为我已经尝试使用示例控制台 java 程序进行此连接池,即使我收到 Logon Denied 异常,我也可以在其中连接到 DB

在这里我需要以编程方式设置用户名和密码,有没有办法摆脱这个?.

4

1 回答 1

2

我建议您向 Hibernate 提供自己的 C3P0 数据源,如下所示:

  1. 您以编程方式创建 C3P0 数据源:

    @org.springframework.context.annotation.Configuration
    public class C3P0Configuration {
    
        @Bean
        public ComboPooledDataSource dataSource() {
            ComboPooledDataSource actualDataSource = new ComboPooledDataSource();
            actualDataSource.setJdbcUrl("set-your-url");
            actualDataSource.setUser("set-your-user");
            actualDataSource.setPassword("set-your-user");
            actualDataSource.setMinPoolSize(0);
            actualDataSource.setMaxPoolSize(5);
            return actualDataSource;
        }
    }
    
  2. 您将数据源传递给您的 Hibernate SessionFactory:

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" dependsOn="dataSource">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingResources">
            <list>
            <value>your-hibernate-config.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
            </value>
        </property>
    </bean>
    

    或者如果您也以编程方式构建会话工厂,请确保提供此属性:

    properties.put("hibernate.connection.datasource", dataSource);
    

如果您不使用 Spring,则必须有权访问会话工厂实例化过程,因为您需要在会话工厂之前构建数据源,以便将其作为上述 Hibernate 属性提供。

于 2014-07-07T06:20:02.987 回答