CodeRider
CodeRider

Reputation: 584

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 we need delegating connection. But I want to the purpose and what extra work is done by delegating connection and not normal connection.

Upvotes: 2

Views: 476

Answers (1)

Mark Rotteveel
Mark Rotteveel

Reputation: 108971

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.

Upvotes: 3

Related Questions