DBCP2 Delegating connection

问题: Why we need delegating connection or the innerMostDelegate in DBCP2 for DB connections? I see some of the prepared statements and creating java objects into oracle objects...

问题:

Why we need delegating connection or the innerMostDelegate in DBCP2 for DB connections? I see some of the prepared statements and creating java objects into oracle objects we need delegating connection. But I want to the purpose and what extra work is done by delegating connection and not normal connection.


回答1:

You don't normally "need" it, except when you need access to features that are not defined in the JDBC API, but only available in the driver-specific API.

A connection pool normally returns a logical connection which is a wrapper (or proxy) around a physical connection to the database. This logical connection - usually - only exposes the JDBC API. If you really need access to those driver-specific feature you need to 'unwrap' the logical connection. Given this can easily lead to connection leaks or incorrect handling early closing of the connection, you need to be careful when doing this, and make sure you close the connection that you originally obtained, and not one of the unwrapped connections

In the case of DBCP, it seems to be possible that connections are wrapped multiple times. The getInnerMostDelegate() method will recursively unwrap until it reaches a connection that is not an instance of DelegatingConnection. That will most likely be the physical connection.

So

  1. You don't normally need this if you can do something using the JDBC API.
  2. Only use this if you really need to access the driver-specific API.
  3. If you use this, make sure you close the right connection.
  • 发表于 2018-09-02 03:23
  • 阅读 ( 334 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除