Package org.springframework.jdbc.datasource

# Class DelegatingDataSource

java.lang.Object
org.springframework.jdbc.datasource.DelegatingDataSource

All Implemented Interfaces:
  `Wrapper`, `CommonDataSource`, `DataSource`, `org.springframework.beans.factory.InitializingBean`

Direct Known Subclasses:
  `LazyConnectionDataSourceProxy`, `ShardingKeyDataSourceAdapter`, `TransactionAwareDataSourceProxy`, `UserCredentialsDataSourceAdapter`

public class DelegatingDataSource extends Object implements DataSource, org.springframework.beans.factory.InitializingBean
JDBC `DataSource` implementation that delegates all calls to a given target `DataSource`.

This class is meant to be subclassed, with subclasses overriding only those methods (such as `getConnection()`) that should not simply delegate to the target DataSource.

Since:
  1.1
Author:
  Juergen Hoeller
See Also:
  - `getConnection()`

## Constructor Summary

Constructors
Constructor
Description
`DelegatingDataSource()`
Create a new DelegatingDataSource.
`DelegatingDataSource(DataSource targetDataSource)`
Create a new DelegatingDataSource.

## Method Summary

Modifier and Type
Method
Description
`void`
`afterPropertiesSet()`
`ConnectionBuilder`
`createConnectionBuilder()`
`ShardingKeyBuilder`
`createShardingKeyBuilder()`
`Connection`
`getConnection()`
`Connection`
`getConnection(String username, String password)`
`int`
`getLoginTimeout()`
`PrintWriter`
`getLogWriter()`
`Logger`
`getParentLogger()`
`DataSource`
`getTargetDataSource()`
Return the target DataSource that this DataSource should delegate to.
`boolean`
`isWrapperFor(Class<?> iface)`
`protected DataSource`
`obtainTargetDataSource()`
Obtain the target `DataSource` for actual use (never `null`).
`void`
`setLoginTimeout(int seconds)`
`void`
`setLogWriter(PrintWriter out)`
`void`
`setTargetDataSource(DataSource targetDataSource)`
Set the target DataSource that this DataSource should delegate to.
`<T> T`
`unwrap(Class<T> iface)`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### DelegatingDataSource

public DelegatingDataSource()
Create a new DelegatingDataSource.

See Also:
  - `setTargetDataSource(javax.sql.DataSource)`

### DelegatingDataSource

public DelegatingDataSource(DataSource targetDataSource)
Create a new DelegatingDataSource.

Parameters:
  `targetDataSource` - the target DataSource

## Method Details

### setTargetDataSource

public void setTargetDataSource(@Nullable DataSource targetDataSource)
Set the target DataSource that this DataSource should delegate to.

### getTargetDataSource

@Nullable public DataSource getTargetDataSource()
Return the target DataSource that this DataSource should delegate to.

### obtainTargetDataSource

protected DataSource obtainTargetDataSource()
Obtain the target `DataSource` for actual use (never `null`).

Since:
  5.0

### afterPropertiesSet

public void afterPropertiesSet()

Specified by:
  `afterPropertiesSet` in interface `org.springframework.beans.factory.InitializingBean`

### getConnection

public Connection getConnection() throws SQLException

Specified by:
  `getConnection` in interface `DataSource`
Throws:
  `SQLException`

### getConnection

public Connection getConnection(String username, String password) throws SQLException

Specified by:
  `getConnection` in interface `DataSource`
Throws:
  `SQLException`

### createConnectionBuilder

public ConnectionBuilder createConnectionBuilder() throws SQLException

Specified by:
  `createConnectionBuilder` in interface `DataSource`
Throws:
  `SQLException`

### createShardingKeyBuilder

public ShardingKeyBuilder createShardingKeyBuilder() throws SQLException

Specified by:
  `createShardingKeyBuilder` in interface `CommonDataSource`
Throws:
  `SQLException`

### getLoginTimeout

public int getLoginTimeout() throws SQLException

Specified by:
  `getLoginTimeout` in interface `CommonDataSource`
Specified by:
  `getLoginTimeout` in interface `DataSource`
Throws:
  `SQLException`

### setLoginTimeout

public void setLoginTimeout(int seconds) throws SQLException

Specified by:
  `setLoginTimeout` in interface `CommonDataSource`
Specified by:
  `setLoginTimeout` in interface `DataSource`
Throws:
  `SQLException`

### getLogWriter

public PrintWriter getLogWriter() throws SQLException

Specified by:
  `getLogWriter` in interface `CommonDataSource`
Specified by:
  `getLogWriter` in interface `DataSource`
Throws:
  `SQLException`

### setLogWriter

public void setLogWriter(PrintWriter out) throws SQLException

Specified by:
  `setLogWriter` in interface `CommonDataSource`
Specified by:
  `setLogWriter` in interface `DataSource`
Throws:
  `SQLException`

### getParentLogger

public Logger getParentLogger()

Specified by:
  `getParentLogger` in interface `CommonDataSource`

### unwrap

public <T> T unwrap(Class<T> iface) throws SQLException

Specified by:
  `unwrap` in interface `Wrapper`
Throws:
  `SQLException`

### isWrapperFor

public boolean isWrapperFor(Class<?> iface) throws SQLException

Specified by:
  `isWrapperFor` in interface `Wrapper`
Throws:
  `SQLException`