javaee-使用c3p0连接池是出现了空指针异常

问题描述

使用c3p0连接池是出现了空指针异常

连接不上我的mysql数据库,但是我单独测试又可以连接上,测试代码如下:

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/shi";
String name = "root";
String password = "root";
conn = DriverManager.getConnection(url, name, password);
String sql = "select * from t_user";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()) {
String username = rs.getString("name");
System.out.println(username);
}

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

但是使用c3p0的时候就出问题了:
c3p0的配置如下:

<?xml version="1.0" encoding="UTF-8"?>

    <property name="jdbcUrl">jdbc:mysql://localhost:3306/shi?useUnicode=true&amp;characterEncoding=utf8</property>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="user">root</property>
    <property name="password">root</property>
    <!--
    <property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl</property>
    <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
    <property name="user">scott</property>
    <property name="password">tiger</property>
    -->

    <property name="c3p0.testConnectionOnCheckout">true</property>
    <property name="acquireIncrement">3</property>
    <property name="initialPoolSize">10</property>
    <property name="minPoolSize">2</property>
    <property name="maxPoolSize">10</property>
</default-config>

出现的异常如下:

kate
Tom

2014-8-9 9:31:05 com.mchange.v2.log.MLog
信息: MLog clients using java 1.4+ standard logging.
2014-8-9 9:31:06 com.mchange.v2.c3p0.C3P0Registry
信息: Initializing c3p0-0.9.5-pre8 [built 04-April-2014 04:20:15 -0700; debug? true; trace: 10]
2014-8-9 9:31:06 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 2sa1hd931f5tqer1kt6lht|2c766a24, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> null, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceUseNamedDriverClass -> false, identityToken -> 2sa1hd931f5tqer1kt6lht|2c766a24, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> null, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
2014-8-9 9:31:36 com.mchange.v2.resourcepool.BasicResourcePool
警告: com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@6c3b78cf -- 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.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:507)
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:476)
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
at java.sql.DriverManager.getDriver(DriverManager.java:253)
at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:278)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:158)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:161)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:147)
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:1814)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)
2014-8-9 9:31:36 com.mchange.v2.resourcepool.BasicResourcePool
警告: Having failed to acquire a resource, com.mchange.v2.resourcepool.BasicResourcePool@5c1fe88d is interrupting all Threads waiting on a resource to check out. Will try again in response to new client requests.
2014-8-9 9:31:36 com.mchange.v2.resourcepool.BasicResourcePool
警告: com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@53416c84 -- 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.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:507)
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:476)
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
at java.sql.DriverManager.getDriver(DriverManager.java:253)
at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:278)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:158)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:161)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:147)
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:1814)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)
java.sql.SQLException: Connections could not be acquired from the underlying database!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:690)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140)
at jdbcUtils.JdbcUtils.getConnection(JdbcUtils.java:44)
at jdbcUtils.TestUtils.main(TestUtils.java:59)
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1422)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:606)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:526)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutAndMarkConnectionInUse(C3P0PooledConnectionPool.java:756)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:683)
... 3 more
2014-8-9 9:31:36 com.mchange.v2.resourcepool.BasicResourcePool
警告: Having failed to acquire a resource, com.mchange.v2.resourcepool.BasicResourcePool@5c1fe88d is interrupting all Threads waiting on a resource to check out. Will try again in response to new client requests.
2014-8-9 9:31:36 com.mchange.v2.resourcepool.BasicResourcePool
警告: com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@58a2353d -- 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.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:507)
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:476)
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
at java.sql.DriverManager.getDriver(DriverManager.java:253)
at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:278)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:158)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:161)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:147)
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:1814)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)
2014-8-9 9:31:36 com.mchange.v2.resourcepool.BasicResourcePool
警告: Having failed to acquire a resource, com.mchange.v2.resourcepool.BasicResourcePool@5c1fe88d is interrupting all Threads waiting on a resource to check out. Will try again in response to new client requests.

解决方案

请问这个问题是怎么解决的

时间: 2024-12-10 03:07:02

javaee-使用c3p0连接池是出现了空指针异常的相关文章

c3p0连接池中获取的Connection对象的close()方法是真的把连接给关闭了?

问题描述 c3p0连接池中获取的Connection对象的close()方法是真的把连接给关闭了? 自己做写了一个管理数据源的DBManager,构想中从数据源里面获取的Connection使用完之后执行close()方法,然后把Connection对象闲置回连接池中. 但是测试出来的结果好像每次执行close()之后connection就销毁了. 代码如下: DBManager.java package ben.DBUtils; import java.sql.Connection; impo

hibernate + c3p0连接池 出现以下错误,求解答!

问题描述 hibernate + c3p0连接池 出现以下错误,求解答! 5C 05-Apr-2016 10:36:43.186 INFO [C3P0PooledConnectionPoolManager[identityToken->2sbh7t9f1rc58zmz5ikx9|52feca18]-AdminTaskTimer] org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illega

请问有没有人知道c3p0连接池配置文件报错,下面是具体报错内容

问题描述 请问有没有人知道c3p0连接池配置文件报错,下面是具体报错内容 Invalid property 'driverClassName' of bean class [com.mchange.v2.c3p0.ComboPooledDataSource]: Bean property 'driverClassName' is not writable or has an invalid setter method. Does the parameter type of the setter

数据库连接池-c3p0连接池 无需关注statement是否关闭吗

问题描述 c3p0连接池 无需关注statement是否关闭吗 c3p0连接池 ,当connection.close()的时候,把数据连接还回连接池中,那么connection所产生的statement,resultset等对象是怎么关闭的? 解决方案 使用 c3p0 的话,也是 java.sql.Connection,只要是 JDBC 都是这个接口的对象! 使用完后必须 con.close() 掉,使用连接池的话,执行 con.close 并不会关闭与数据库的 TCP 连接,而是将连接还回到池

c3p0连接池-如何快速定位jar 包冲突

问题描述 如何快速定位jar 包冲突 建立c3p0连接池是时,报Mlog clients using java 1.4+ standard logging. 请问这是jar 包冲突造成的么.如何快速定位jar 包冲突造成的问题呢

可以介绍一下c3p0连接池和自己写的向量连接池的区别么?

问题描述 可以介绍一下c3p0连接池和自己写的向量连接池的区别么? 可以介绍一下c3p0连接池和自己写的Vector向量连接池的区别么? 解决方案 一个是系统的 一个是自定义的

hibernate+c3p0连接池,插入数据程序运行一会就卡住

问题描述 程序是一个导入Excel数据的工具,excel中有好几千条数据,但是程序运行一会,每次都是导入24条以后程序就不运行了,程序好像处于挂起状态一样,卡住了,控制台上也不再打印hibernate日志信息,不知道什么原因,有没有会的看一下,谢谢!   hibernate.cfg.xml配置如下:<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernat

c3p0连接池-c3p0 连接不释放 请路过的大神过来看看

问题描述 c3p0 连接不释放 请路过的大神过来看看 配置文件如下 <?xml version="1.0" encoding="UTF-8"?> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://w

MySQL---数据库从入门走向大神系列(十四)-ComboPooledDataSource(C3P0连接池配置)

需要准备的jar包: MySQL的jar包mysql-connector-java-5.1.39-bin目前是5.1.39版本: http://dev.mysql.com/downloads/connector/j/ C3p0的2个包: https://sourceforge.net/projects/c3p0/ c3p0-0.9.1.2.jar和c3p0-sources-0.9.1.2.jar 配置文件:c3p0-config.xml 名字必须为这个! <c3p0-config> <!