C3P0 Connection Pool Getting java.sql.SQLException: No suitable driver 异常
C3P0 Connection Pool Getting java.sql.SQLException: No suitable driver exception
我在我的 Web 应用程序中使用 c3p0
连接池,当我 运行 这个应用程序时我得到 java.sql.SQLException: No suitable driver
异常,我使用 Datasource.java
class 用于创建连接池,Utils.java
用于从 datasouce.properties
文件读取属性。
完全执行是(我得到 10 次)为什么会出现异常。我正在为 oracle 驱动程序使用 odbc6.jar
文件,请帮助我
2015-09-07 16:53:32 INFO AbstractPoolBackedDataSource:527 - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 10, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> z8kflt9b1agzamcthrvgf|1d84bc10, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> null, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> z8kflt9b1agzamcthrvgf|1d84bc10, idleConnectionTestPeriod -> 0, initialPoolSize -> 10, jdbcUrl -> jdbc:oracle:thin:@localhost:1521:LOCALDB, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 200, maxStatements -> 2100, maxStatementsPerConnection -> 0, minPoolSize -> 10, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
2015-09-07 16:54:02 WARN BasicResourcePool:1911 - com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@295bc68c -- 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:264)
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.C3P0PooledConnectionPoolPooledConnectionResourcePoolManager.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.access0(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)
2015-09-07 16:54:02 WARN BasicResourcePool:1911 - com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@78da5318 -- 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:
JSP 文件:-
<%
Datasource ds = Datasource.getInstance();
Connection connection = ds.getConnection();
try{
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("select * from app_userlist");
while(rs.next()){
System.out.println(rs.getInt(1));
}
}catch(Exception e){
}
%>
Datasource.java
public class Datasource {
private Datasource() throws IOException, SQLException {
// load datasource properties
props = Utils.readProperties("datasource.properties");
cpds = new ComboPooledDataSource();
cpds.setJdbcUrl(props.getProperty("jdbcUrl"));
cpds.setUser(props.getProperty("username"));
cpds.setPassword(props.getProperty("password"));
cpds.setInitialPoolSize(new Integer((String) props.getProperty("initialPoolSize")));
cpds.setAcquireIncrement(new Integer((String) props.getProperty("acquireIncrement")));
cpds.setMaxPoolSize(new Integer((String) props.getProperty("maxPoolSize")));
cpds.setMinPoolSize(new Integer((String) props.getProperty("minPoolSize")));
cpds.setMaxStatements(new Integer((String) props.getProperty("maxStatements")));
Connection testConnection = null;
Statement testStatement = null;
// test connectivity and initialize pool
try {
testConnection = cpds.getConnection();
testStatement = testConnection.createStatement();
testStatement.executeQuery("select 1+1 from DUAL");
} catch (SQLException e) {
throw e;
} finally {
testStatement.close();
testConnection.close();
}
}
public static Datasource getInstance() throws IOException, SQLException {
if (datasource == null) {
datasource = new Datasource();
return datasource;
} else {
return datasource;
}
}
public Connection getConnection() throws SQLException {
return this.cpds.getConnection();
} }
Utils.java
public class Utils {
static public Properties readProperties(String filename) throws IOException{
Properties props = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream stream = loader.getResourceAsStream(filename);
props.load(stream);
return props;
}
}
datasource.properties:-
driverClass =oracle.jdbc.OracleDriver
jdbcUrl =jdbc:oracle:thin:@localhost:1521:userdb
username =user
password =xxxx
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
maxPoolSize="50"
minPoolSize="15"
acquireIncrement="3"
acquireRetryAttempts = "0"
acquireRetryDelay = "3000"
breakAfterAcquireFailure = "false"
maxConnectionAge = "60"
maxIdleTime = "30"
maxIdleTimeExcessConnections = "10"
idleConnectionTestPeriod = "15"
testConnectionOnCheckout = "true"
preferredTestQuery = "SELECT 1"
debugUnreturnedConnectionStackTraces = "true"
autoCommitOnClose="true"
java.sql.SQLException: No suitable driver found
此异常可能有 2 个原因:
- 根本没有加载 JDBC 驱动程序。
- URL 不匹配任何加载的 JDBC 驱动程序。
DRIVER CLASS: oracle.jdbc.driver.OracleDriver
DRIVER 位置: 只需指定包含 Oracle Thin driver 的 jar 或 zip 文件的位置。
JDBC URL 格式:
jdbc:oracle:thin:@//:/服务名称
或者
jdbc:oracle:thin:@::
注意: Oracle thin driver 需要 JDBC URL 中数据库的 SID 或 ServiceName 而不是数据库别名。有关更多详细信息,请参阅您的 Oracle 文档。
JDBC URL 示例:
jdbc:oracle:thin:@//localhost:1521/XE
jdbc:oracle:thin:@neptune.acme.com:1521:T10A
jdbc:oracle:thin:@127.0.0.1:1521:T10A
我在我的 Web 应用程序中使用 c3p0
连接池,当我 运行 这个应用程序时我得到 java.sql.SQLException: No suitable driver
异常,我使用 Datasource.java
class 用于创建连接池,Utils.java
用于从 datasouce.properties
文件读取属性。
完全执行是(我得到 10 次)为什么会出现异常。我正在为 oracle 驱动程序使用 odbc6.jar
文件,请帮助我
2015-09-07 16:53:32 INFO AbstractPoolBackedDataSource:527 - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 10, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> z8kflt9b1agzamcthrvgf|1d84bc10, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> null, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> z8kflt9b1agzamcthrvgf|1d84bc10, idleConnectionTestPeriod -> 0, initialPoolSize -> 10, jdbcUrl -> jdbc:oracle:thin:@localhost:1521:LOCALDB, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 200, maxStatements -> 2100, maxStatementsPerConnection -> 0, minPoolSize -> 10, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
2015-09-07 16:54:02 WARN BasicResourcePool:1911 - com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@295bc68c -- 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:264)
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.C3P0PooledConnectionPoolPooledConnectionResourcePoolManager.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.access0(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)
2015-09-07 16:54:02 WARN BasicResourcePool:1911 - com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@78da5318 -- 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:
JSP 文件:-
<%
Datasource ds = Datasource.getInstance();
Connection connection = ds.getConnection();
try{
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("select * from app_userlist");
while(rs.next()){
System.out.println(rs.getInt(1));
}
}catch(Exception e){
}
%>
Datasource.java
public class Datasource {
private Datasource() throws IOException, SQLException {
// load datasource properties
props = Utils.readProperties("datasource.properties");
cpds = new ComboPooledDataSource();
cpds.setJdbcUrl(props.getProperty("jdbcUrl"));
cpds.setUser(props.getProperty("username"));
cpds.setPassword(props.getProperty("password"));
cpds.setInitialPoolSize(new Integer((String) props.getProperty("initialPoolSize")));
cpds.setAcquireIncrement(new Integer((String) props.getProperty("acquireIncrement")));
cpds.setMaxPoolSize(new Integer((String) props.getProperty("maxPoolSize")));
cpds.setMinPoolSize(new Integer((String) props.getProperty("minPoolSize")));
cpds.setMaxStatements(new Integer((String) props.getProperty("maxStatements")));
Connection testConnection = null;
Statement testStatement = null;
// test connectivity and initialize pool
try {
testConnection = cpds.getConnection();
testStatement = testConnection.createStatement();
testStatement.executeQuery("select 1+1 from DUAL");
} catch (SQLException e) {
throw e;
} finally {
testStatement.close();
testConnection.close();
}
}
public static Datasource getInstance() throws IOException, SQLException {
if (datasource == null) {
datasource = new Datasource();
return datasource;
} else {
return datasource;
}
}
public Connection getConnection() throws SQLException {
return this.cpds.getConnection();
} }
Utils.java
public class Utils {
static public Properties readProperties(String filename) throws IOException{
Properties props = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream stream = loader.getResourceAsStream(filename);
props.load(stream);
return props;
}
}
datasource.properties:-
driverClass =oracle.jdbc.OracleDriver
jdbcUrl =jdbc:oracle:thin:@localhost:1521:userdb
username =user
password =xxxx
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
maxPoolSize="50"
minPoolSize="15"
acquireIncrement="3"
acquireRetryAttempts = "0"
acquireRetryDelay = "3000"
breakAfterAcquireFailure = "false"
maxConnectionAge = "60"
maxIdleTime = "30"
maxIdleTimeExcessConnections = "10"
idleConnectionTestPeriod = "15"
testConnectionOnCheckout = "true"
preferredTestQuery = "SELECT 1"
debugUnreturnedConnectionStackTraces = "true"
autoCommitOnClose="true"
java.sql.SQLException: No suitable driver found
此异常可能有 2 个原因:
- 根本没有加载 JDBC 驱动程序。
- URL 不匹配任何加载的 JDBC 驱动程序。
DRIVER CLASS: oracle.jdbc.driver.OracleDriver
DRIVER 位置: 只需指定包含 Oracle Thin driver 的 jar 或 zip 文件的位置。
JDBC URL 格式: jdbc:oracle:thin:@//:/服务名称 或者 jdbc:oracle:thin:@::
注意: Oracle thin driver 需要 JDBC URL 中数据库的 SID 或 ServiceName 而不是数据库别名。有关更多详细信息,请参阅您的 Oracle 文档。
JDBC URL 示例:
jdbc:oracle:thin:@//localhost:1521/XE jdbc:oracle:thin:@neptune.acme.com:1521:T10A
jdbc:oracle:thin:@127.0.0.1:1521:T10A