Logging in DBCP

前端 未结 4 706
刺人心
刺人心 2021-02-07 13:59

I\'m using Apache Commons DBCP. There is a task to track the inner behavior of the DBCP - number of active and idle connections.

I found out that DBCP lacks any such lo

4条回答
  •  被撕碎了的回忆
    2021-02-07 14:19

    AOP is the way to go for tracking connection usage from the pool. However, its not very straight forward. You need to do the following:

    1. Create a ConnectionWrapper class that wraps (Decorator pattern) Connection and overrride the close() method to additionally log the connection id, thread id and action 'close'
    2. Intercept the getConnection() method of the datasource.
    3. In that method, log the connection id, thread id and action 'open'
    4. In the same method, decorate the original connection and return your ConnectionWrapper instance

    With this setup, you can track both the borrow & return of the connection from/to the pool.

提交回复
热议问题