Best approach for returning connection objects to HikariCP pool

后端 未结 1 1191
一向
一向 2021-02-07 02:10

I am trying to use HikariCP connection pool. I was able to get it to work and get a connection that I could use. I am not sure what is the best approach for returning the connec

1条回答
  •  闹比i
    闹比i (楼主)
    2021-02-07 03:12

    As with most connection pools, Hikari doesn't give you an actual JDBC Connection when you ask for one. What it does instead is give you a proxy that implements the Connection interface. In the case of Hikari - it's a ConnectionProxy object.

    This proxy serves a few purposes, the main of which is - take the control of opening/closing connections and statements away from you and into the connection pool. This happens automagically and you should be using your connections as usual. This includes closing them after use.

    If you look at the source code for Hikari, at the ConnectionProxy class in particular, you will see that the close() method is very different from the standard one. The code reads as:

    Mark the connection as closed, do cleanup, reset underlying connection state and params.

    Hence, simply calling close() will just clean and return the connection to the pool.

    0 讨论(0)
提交回复
热议问题