代码如下:
- Java code
public final class DatabaseUtils{ // private final static String JDBC_URL = "jdbc:oracle:thin:@localhost:1521:orcl"; private final static String JDBC_URL = "dbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = vip-db1)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = vip-db2)(PORT = 1521))(LOAD_BALANCE = yes)(failover = yes)(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = db) ) )"; private final static String JDBC_USER = "xxx"; private final static String JDBC_PASSWORD = "xxx"; private final static String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver"; // private static DataSource dataSource; private static ComboPooledDataSource cpds; static { try { cpds = new ComboPooledDataSource(); cpds.setDriverClass(JDBC_DRIVER); cpds.setJdbcUrl(JDBC_URL); cpds.setUser(JDBC_USER); cpds.setPassword(JDBC_PASSWORD); cpds.setMinPoolSize(5); cpds.setAcquireIncrement(5); cpds.setMaxPoolSize(20); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } /** * @return 网络连接 * @throws SQLException */ public static Connection getConnection() throws SQLException { return cpds.getConnection(); }}
错误如下:
- XML code
190015 [Task-Thread-for-com.mchange.v2.async.ThreadPerTaskAsynchronousRunner@62e8ef4c] WARN com.mchange.v2.resourcepool.BasicResourcePool - com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@5eb10190 -- 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:223) at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134) 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.ThreadPerTaskAsynchronousRunner$TaskThread.run(ThreadPerTaskAsynchronousRunner.java:255)
oracle的jar包在classpath里的,如果不解集群,使用下面的url,测试通过
private final static String JDBC_URL = "jdbc:oracle:thin:@localhost:1521:orcl";
------解决方案--------------------
------解决方案--------------------
private final static String JDBC_URL = "dbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = vip-db1)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = vip-db2)(PORT = 1521))(LOAD_BALANCE = yes)(failover = yes)(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = db) ) )";