当前位置: 动力学知识库 > 问答 > 编程问答 >

java - DBCP2 can't get a connection

问题描述:

My java web application connects Oracle with DBCP2, using tomcat and nginx. It works fine at first, but after some time, all request threads block at the function getConnection which gets a connect from the pool, and the application crashes at the end.

Here is my properties:

driverClassName=oracle.jdbc.driver.OracleDriver

url=jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = xxxx)(PORT = 1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = xxx)(PORT = 1521))(load_balance=yes)(failover=yes))(connect_data=(service_name= CTYRAC01)))

username=xxxx

password=xxxx

initialSize=50

maxTotal =1500

maxIdle=300

minIdle=50

maxWaitMillis=10000

validationQuery=SELECT COUNT(*) FROM DUAL

validationInterval="30000"

testWhileIdle=true

testOnBorrow=true

timeBetweenEvictionRunsMillis=5000

minEvictableIdleTimeMillis=150000

removeAbandonedOnMaintenance=true

removeAbandonedOnBorrow=true

removeAbandonedTimeout=60

connectionProperties=true

useUnicode=true

characterEncoding=gbk

defaultAutoCommit=true

defaultReadOnly=

defaultTransactionIsolation=READ_COMMITTED

and here is my JdbcUtils:

public final class JdbcUtils{

private static Object obj_getConn = new Object();

private static DataSource MainDataSource = null;//Դ

private JdbcUtils() {

}

static {

try {

Class.forName("oracle.jdbc.driver.OracleDriver");

Properties prop = new Properties();

InputStream is = JdbcUtils.class.getClassLoader()

.getResourceAsStream("dbcpconfig.properties");

prop.load(is);

MainDataSource = BasicDataSourceFactory.createDataSource(prop);

} catch (Exception e) {

System.out.println("connect failed:" +e.getMessage());

throw new ExceptionInInitializerError(e);

}

}

public static DataSource getDataSource() {

return MainDataSource;

}

//all threads blocks here

public static synchronized Connection getConnection() throws SQLException, SQLTimeoutException{

return MainDataSource.getConnection();

}

public static void shutDownDataSource() throws Exception{

if(MainDataSource!=null){

((BasicDataSource)MainDataSource).close();

}

}

public static void free(ResultSet rs, Statement st, Connection conn) {

try {

if (rs != null)

rs.close();

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

if (st != null)

st.close();

} catch (SQLException e) {

e.printStackTrace();

} finally {

if (conn != null)

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

}

It doesn't happen all the time, but if it blocks, all the request threads will block at the function getConnection. I can't find out the problem, is anything wrong with my configs or codes? Or the problem with network?

Any help? It has troubled me a lot of time...

分享给朋友:
您可能感兴趣的文章:
随机阅读: