服务器运行几天后就会出现连不了数据库,不知道具体的问题所在。
applicationContext.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass"><value>oracle.jdbc.OracleDriver</value></property>
        <property name="jdbcUrl"><value>jdbc:oracle:thin:@192.68.69.1:1521:orcl</value></property>
        <property name="user"><value>admin</value></property>
        <!-- Make sure <value> tags are on same line - if they're not, authentication will fail -->
        <property name="password"><value>admin</value></property>
        <!--连接池中保留的最小连接数。-->
        <property name="minPoolSize">
            <value>5</value>
        </property>
 
 <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize">
            <value>30</value>
        </property>
 
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize">
            <value>10</value>
        </property>
 
 <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime">
            <value>60</value>
        </property>
 
 <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement">
            <value>5</value>
        </property>
 
 <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
  属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
  如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
        <property name="maxStatements">
            <value>0</value>
        </property>
 
 <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
        <property name="idleConnectionTestPeriod">
            <value>60</value>
        </property>
 
 <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
        <property name="acquireRetryAttempts">
            <value>30</value>
        </property>
 
 <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
  保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
  获取连接失败后该数据源将申明已断开并永久关闭。Default: false-->
        <property name="breakAfterAcquireFailure">
            <value>false</value>
        </property>
        
        <property name="acquireRetryDelay">
     <value>100</value>
</property>
 
 <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的
  时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable
  等方法来提升连接测试的性能。Default: false -->
        <property name="testConnectionOnCheckout">
            <value>false</value>
        </property>
    </bean>    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource"><ref local="dataSource"/></property>
        <property name="mappingResources">
            <list>
<value>com/xtbg/web/domain/TExchangeProj.hbm.xml</value>
             <value>com/xtbg/web/domain/TBaseProj.hbm.xml</value>
             ....等等
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
         <prop key="hibernate.hbm2ddl.auto">none</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>           
            </props>
        </property>
    </bean>
 
<!-- transaction config -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>

<!-- 系统管理模块配置 -->
    <bean id="departmentDAO" class="com.xtbg.web.dao.DepartmentDAO" >
     <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="employeeDAO" class="com.xtbg.web.dao.EmployeeDAO" >
     <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    
    <bean id="powerDAO" class="com.xtbg.web.dao.PowerDAO" >
     <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="roleDAO" class="com.xtbg.web.dao.RoleDAO" >
     <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="rolePowerDAO" class="com.xtbg.web.dao.RolePowerDAO" >
     <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <!-- 信息办模块配置 -->
    <bean id="baseProjDAO" class="com.xtbg.web.dao.BaseProjDAO" >
     <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="projMemberDAO" class="com.xtbg.web.dao.ProjMemberDAO" >
     <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="projAttachmentDAO" class="com.xtbg.web.dao.ProjAttachmentDAO" >
     <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="sbsqxxDAO" class="com.xtbg.web.dao.SbsqxxDAO" >
     <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="sbslxxDAO" class="com.xtbg.web.dao.SbslxxDAO" >
     <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="sbspxxDAO" class="com.xtbg.web.dao.SbspxxDAO" >
     <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="projCodeDAO" class="com.xtbg.web.dao.ProjCodeDAO" >
     <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="exchangeProjDAO" class="com.xtbg.web.dao.ExchangeProjDAO" >
     <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean id="zjysghDAO" class="com.xtbg.web.dao.ZjysghDAO" >
     <property name="sessionFactory" ref="sessionFactory" />
    </bean> <!-- service配置 -->
    <bean id="systemService" class="com.xtbg.web.service.impl.SystemService">
        <property name="departmentDAO" ref="departmentDAO"/>
        <property name="employeeDAO" ref="employeeDAO"/>
        <property name="powerDAO" ref="powerDAO"/>
        <property name="roleDAO" ref="roleDAO"/>
        <property name="rolePowerDAO" ref="rolePowerDAO"/>
    </bean>
    
    <!-- AOP事务 -->
    <aop:config>
        <aop:pointcut id="serviceOperation" expression="execution(* com.xtbg.web.service.impl.*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
     <tx:method name="save*" />
     <tx:method name="delete*" />
     <tx:method name="search*" />
     <tx:method name="submit*" />
     <tx:method name="download*" />
</tx:attributes>
    </tx:advice>
</beans>

解决方案 »

  1.   

    异常信息如下:
    Hibernate: select projcode0_.PROJ_ID as PROJ1_12_0_, projcode0_.PROJ_CODE as PRO
    J2_12_0_ from T_PROJ_CODE projcode0_ where projcode0_.PROJ_ID=?
    Hibernate: select sbspxx0_.ID as ID10_, sbspxx0_.PROJ_ID as PROJ2_10_, sbspxx0_.
    XMYJ as XMYJ10_, sbspxx0_.SPYJ as SPYJ10_, sbspxx0_.SPRYXM as SPRYXM10_, sbspxx0
    _.JDMC as JDMC10_, sbspxx0_.JDLX as JDLX10_, sbspxx0_.SPRYBH as SPRYBH10_, sbspx
    x0_.JDXH as JDXH10_ from T_SBSPXX sbspxx0_ where sbspxx0_.PROJ_ID=4380 order by
    sbspxx0_.JDXH asc
    WARN [org.hibernate.util.JDBCExceptionReporter]: SQL Error: 0, SQLState: null
    ERROR[org.hibernate.util.JDBCExceptionReporter]: Connections could not be acquir
    ed from the underlying database!
    WARN [org.hibernate.util.JDBCExceptionReporter]: SQL Error: 0, SQLState: null
    ERROR[org.hibernate.util.JDBCExceptionReporter]: Connections could not be acquir
    ed from the underlying database!
    INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]: Loading XM
    L bean definitions from class path resource [org/springframework/jdbc/support/sq
    l-error-codes.xml]
    INFO [org.springframework.jdbc.support.SQLErrorCodesFactory]: SQLErrorCodes load
    ed: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
    WARN [org.springframework.jdbc.support.SQLErrorCodesFactory]: Error while extrac
    ting database product name - falling back to empty error codes
    org.springframework.jdbc.support.MetaDataAccessException: Could not get Connecti
    on for extracting meta data; nested exception is org.springframework.jdbc.Cannot
    GetJdbcConnectionException: Could not get JDBC Connection; nested exception is j
    ava.sql.SQLException: Connections could not be acquired from the underlying data
    base!
           
    Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A Resourc
    ePool could not acquire a resource from its primary factory or source.
            at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicRes
    ourcePool.java:1319)
            at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(
    BasicResourcePool.java:557)
            at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicR
    esourcePool.java:477)
            at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConne
    ction(C3P0PooledConnectionPool.java:525)
            ... 99 more
    WARN [org.hibernate.util.JDBCExceptionReporter]: SQL Error: 0, SQLState: null
    ERROR[org.hibernate.util.JDBCExceptionReporter]: Connections could not be acquir
    ed from the underlying database!
    有碰到过此问题的希望指教!
      

  2.   

    检查你程序中的连接是否都正常关闭
    http://confluence.atlassian.com/display/CONFKB/java.sql.SQLException+Connections+Could+not+be+Acquired+from+the+Underlying+Database!
    http://dev.chinaitzhe.com/java/2008-09/1220565882101882.html
      

  3.   


    <c3p0-config> 
      <default-config> 
    <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> 
    <property name="acquireIncrement">3</property> <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --> 
    <property name="acquireRetryAttempts">30</property> <!--两次连接中间隔时间,单位毫秒。Default: 1000 --> 
    <property name="acquireRetryDelay">1000</property> <!--连接关闭时默认将所有未提交的操作回滚。Default: false --> 
    <property name="autoCommitOnClose">false</property> <!--c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么 
      属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试 
      使用。Default: null--> 
    <property name="automaticTestTable">Test</property> <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 
      保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 
      获取连接失败后该数据源将申明已断开并永久关闭。Default: false--> 
    <property name="breakAfterAcquireFailure">false</property> <!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出 
      SQLException,如设为0则无限期等待。单位毫秒。Default: 0 --> 
    <property name="checkoutTimeout">100</property> <!--通过实现ConnectionTester或QueryConnectionTester的类来测试连接。类名需制定全路径。 
      Default: com.mchange.v2.c3p0.impl.DefaultConnectionTester--> 
    <property name="connectionTesterClassName"></property> <!--指定c3p0 libraries的路径,如果(通常都是这样)在本地即可获得那么无需设置,默认null即可 
      Default: null--> 
    <property name="factoryClassLocation">null</property> <!--Strongly disrecommended. Setting this to true may lead to subtle and bizarre bugs. 
      (文档原文)作者强烈建议不使用的一个属性--> 
    <property name="forceIgnoreUnresolvedTransactions">false</property> <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> 
    <property name="idleConnectionTestPeriod">60</property> <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> 
    <property name="initialPoolSize">3</property> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> 
    <property name="maxIdleTime">60</property> <!--连接池中保留的最大连接数。Default: 15 --> 
    <property name="maxPoolSize">15</property> <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 
      属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 
      如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0--> 
    <property name="maxStatements">100</property> <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0  --> 
    <property name="maxStatementsPerConnection"></property> <!--c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能 
      通过多线程实现多个操作同时被执行。Default: 3--> 
    <property name="numHelperThreads">3</property> <!--当用户调用getConnection()时使root用户成为去获取连接的用户。主要用于连接池连接非c3p0 
      的数据源时。Default: null--> 
    <property name="overrideDefaultUser">root</property> <!--与overrideDefaultUser参数对应使用的一个参数。Default: null--> 
    <property name="overrideDefaultPassword">password</property> <!--密码。Default: null--> 
    <property name="password"></property> <!--定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。注意: 
      测试的表必须在初始数据源的时候就存在。Default: null--> 
    <property name="preferredTestQuery">select id from test where id=1</property> <!--用户修改系统配置参数执行前最多等待300秒。Default: 300 --> 
    <property name="propertyCycle">300</property> <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 
      时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 
      等方法来提升连接测试的性能。Default: false --> 
    <property name="testConnectionOnCheckout">false</property> <!--如果设为true那么在取得连接的同时将校验连接的有效性。Default: false --> 
    <property name="testConnectionOnCheckin">true</property> <!--用户名。Default: null--> 
    <property name="user">root</property> <!--早期的c3p0版本对JDBC接口采用动态反射代理。在早期版本用途广泛的情况下这个参数 
      允许用户恢复到动态反射代理以解决不稳定的故障。最新的非反射代理更快并且已经开始 
      广泛的被使用,所以这个参数未必有用。现在原先的动态反射与新的非反射代理同时受到 
      支持,但今后可能的版本可能不支持动态反射代理。Default: false--> 
    <property name="usesTraditionalReflectiveProxies">false</property>     <property name="automaticTestTable">con_test</property> 
        <property name="checkoutTimeout">30000</property> 
        <property name="idleConnectionTestPeriod">30</property> 
        <property name="initialPoolSize">10</property> 
        <property name="maxIdleTime">30</property> 
        <property name="maxPoolSize">25</property> 
        <property name="minPoolSize">10</property> 
        <property name="maxStatements">0</property> 
        <user-overrides user="swaldman"> 
        </user-overrides> 
      </default-config> 
      <named-config name="dumbTestConfig"> 
        <property name="maxStatements">200</property> 
        <user-overrides user="poop"> 
          <property name="maxStatements">300</property> 
        </user-overrides> 
       </named-config> 
    </c3p0-config> C3P0连接池详细配置
      

  4.   

    问题有变化了,是这样的,你们也来看看!如下:
    环境:
    192.168.0.1是WEB服务器。TOMCAT5.5里面放有两个WEB项目,连接192.168.0.2的ORACLE10G数据库,两个项目使用同一个数据库。
    192.168.0.2是数据库服务器。
    问题:
    WEB服务器运行几天后,就连不上数据库了,两个项目多连不上。而且,在192.168.0.1机器上使用oracle客户端连接也不行。就是192.168.0.1连不上数据库服务器了。这种问题怎么理解呢?
      

  5.   

    在192.168.0.1机器连不上数据库的时候,在其他机器上是可以连接数据库服务器的。
    将192.168.0.1机器的tomcat关闭,再使用客户端软件连接数据库又是可以连的。
      

  6.   

    WEB服务器运行几天后,就连不上数据库了还是你的tomcat的连接池有问题,要不就是你的程序的问题看下日志文件有没有什么异常信息
      

  7.   

    最后仅仅把hibernate的datasource改成了默认的连接池,运行了好久也没有出问题。
    可能是hibernate配置问题,也可能是数据库本身的问题。
    暂时就这样了