Question about org.apache.commons.dbcp.BasicDataSource

I fixed some error related to the way we used BasicDataSource, and although I understand part of it, I still got no answer :)

Problem: The application could not automatically connect to the database after db failed.

The application uses the org.apache.commons.dbcp.BasicDataSource class as a pool of TCP connections to connect JDBC to Oracle db.

Fix: After some research, I found that in BasicDataSource, testOnBorrow and testOnreturn were not set. I provided a validation request to verify the connections. This fixed the problem.

The maximum number of connections in the pool is not set to 1

My understanding: The connection pool will transfer the connection to the application. I think the MAGICALLY app returned a bad collection to the pool when it worked. Now, since the pool does not know if it is a bad connection, it will transmit the same connection to the application the next time it is needed so that the application does not automatically connect to db.

Now, after the fix .. whenever a bad connection is returned to the connection pool, it will be dropped and will not be used again due to the fix I made above.

, BasicDataSource , , , con.close..BasicDataSource , , discardigg ..

: , MAGICALLY [ , con.close , ]. , BasicDataSource , ?. - ?

, , ?

+3
2

, , Google, , . BasicDataSource DBCP: http://commons.apache.org/proper/commons-dbcp/configuration.html

" " " BaseDataSource, ?" ()...

org.apache.commons.dbcp.BasicDataSource , - . , , , -, ( !). DataSource , .

BasicDataSource , "removeAbandoned", - "removeAbandonedTimeout", .

"removeAbandoned" - , , . "false".

"removeAbandonedTimeout" - int, , , . - 300 ( 5 ).

+4

, , "", , "in-use" ( ).

. BasicDataSource # setRemoveAbandoned (boolean) BasicDataSource # setRemoveAbandonedTimeout (int )

, , , finally, :

Connection conn = getConnection();
try {
    ... // perform work
} finally {
    conn.close();
}

, Apache DBUtils.

0

Source: https://habr.com/ru/post/1773338/


All Articles