Hibernate配置C3P0连接池报错 5C

Hibernate+MySQL部署出现了java.net.SocketException: Broken pipe (Write failed)这个错误,查百度说是因为MySQL默认连接8小时的问题,所以想采用C3P0,附上Hibernate.xml配置如下:

 <?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>

        <property name="myeclipse.connection.profile">sces</property>
        <property name="connection.url"><![CDATA[jdbc:mysql://localhost:3306/sces?useUnicode=true&characterEncoding=UTF-8]]></property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="connection.password">123456</property>
        <property name="connection.username">root</property>
        <!--c3p0设置-->
        <!--<property name="connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>-->
        <!--<property name="hibernate.c3p0.min_size">5</property>-->
        <!--<property name="hibernate.c3p0.max_size">20</property>-->
        <!--<property name="hibernate.c3p0.max_statements">100</property>-->
        <!--<property name="hibernate.c3p0.timeout">3600</property>-->
        <!--<property name="hibernate.c3p0.idle_test_period">120</property>-->
        <!--<property name="hibernate.c3p0.acquire_increment">2</property>-->

        <mapping resource="entity/DCharacteristic.hbm.xml" />
        <mapping resource="entity/DIops.hbm.xml" />
        <mapping resource="entity/DPool.hbm.xml" />
        <mapping resource="entity/DStatus.hbm.xml" />
        <mapping resource="entity/Device.hbm.xml" />
        <mapping resource="entity/User.hbm.xml" />
        <mapping resource="entity/DDl.hbm.xml" />
        <mapping resource="entity/Systeminfo.hbm.xml" />
        <mapping resource="entity/DCapacity.hbm.xml"/>
        <mapping resource="entity/DErrlog.hbm.xml"/>
        <mapping resource="entity/DRack.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

中间注释掉的为c3p0的部分,现在这种情况下能正常运行,但是打开c3p0后会报错:

 十二月 26, 2017 3:02:49 下午 com.mchange.v2.resourcepool.BasicResourcePool 
警告: com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@58f663e4 -- 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: No suitable driver
    at java.sql.DriverManager.getDriver(DriverManager.java:278)
    at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:285)
    at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:175)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:220)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:206)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:203)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1138)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1125)
    at com.mchange.v2.resourcepool.BasicResourcePool.access$700(BasicResourcePool.java:44)
    at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1870)
    at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)


看起来是找不到驱动,但是明明不开连接池的时候能正常跑的,说明驱动没问题呀,为什么会这样呢

补充:直接在IDE中运行读取数据库的操作可以正常运行,但是把程序放到tomcat中就会
报这个错误,MySQL的驱动在tomcat的bin和lib中都放了,求解!!!!!

4个回答

org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider按照这个路径去找下类,是否存在这个类,也许是没有添加这个类,也许是版本不一致造成的,总之要找一下这个类,我估计大多数出问题都是版本不一致的原因

qq238472837
qq238472837 这个类是有的,输的时候都是快捷完成自动写的
大约 2 年之前 回复

不加c3p0可以正常连数据库吗

Qiang89416
有趣的 回复qq238472837: 这种错误感觉应该是mysql的连接地址或者driver写的有问题,或者 connector-java-****-bin.jar这个jar的问题
大约 2 年之前 回复
qq238472837
qq238472837 不加可以的
大约 2 年之前 回复


mysql
mysql-connector-java
5.1.40

看看这个好使不

参考这个
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


com.mysql.jdbc.Driver


jdbc:mysql://localhost:3306/miss

root

1111


org.hibernate.dialect.MySQLDialect

true
mysql
<!-- =============== C3P0连接池设定 =================== -->

org.hibernate.connection.C3P0ConnectionProvider

1

100

5

0

2
18000

<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->

3000

true

<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->

2000

<!-- 下面这句很重要,防止8小时自动断开 -->

true




Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问