Top Description Inners Fields Constructors Methods
org.springframework.jdbc.datasource

public Class UserCredentialsDataSourceAdapter

extends DelegatingDataSource
Class Inheritance
Known Direct Subclasses
org.springframework.jdbc.datasource.IsolationLevelDataSourceAdapter
Imports
java.sql.Connection, .SQLException, org.springframework.core.NamedThreadLocal, org.springframework.lang.Nullable, org.springframework.util.Assert, .StringUtils

An adapter for a target JDBC javax.sql.DataSource, applying the specified user credentials to every standard getConnection() call, implicitly invoking getConnection(username, password) on the target. All other methods simply delegate to the corresponding methods of the target DataSource.

Can be used to proxy a target JNDI DataSource that does not have user credentials configured. Client code can work with this DataSource as usual, using the standard getConnection() call.

In the following example, client code can simply transparently work with the preconfigured "myDataSource", implicitly accessing "myTargetDataSource" with the specified user credentials.

<bean id="myTargetDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:comp/env/jdbc/myds"/>
</bean>

<bean id="myDataSource" class="org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter">
  <property name="targetDataSource" ref="myTargetDataSource"/>
  <property name="username" value="myusername"/>
  <property name="password" value="mypassword"/>
</bean>

If the "username" is empty, this proxy will simply delegate to the standard getConnection() method of the target DataSource. This can be used to keep a UserCredentialsDataSourceAdapter bean definition just for the option of implicitly passing in user credentials if the particular target DataSource requires it.

Author
Juergen Hoeller
Since
1.0.2
See Also
getConnection

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
UserCredentialsDataSourceAdapter.JdbcUserCredentials

Inner class used as ThreadLocal value.

Field Summary

Modifier and TypeField and Description
private String
private String
private String
private final ThreadLocal<UserCredentialsDataSourceAdapter.JdbcUserCredentials>
private String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected Connection

Returns:

the Connection
doGetConnection
(String
the username to use
username
,
String
the password to use
password
)

This implementation delegates to the getConnection(username, password) method of the target DataSource, passing in the specified user credentials.

public Connection
getConnection()

Overrides org.springframework.jdbc.datasource.DelegatingDataSource.getConnection.

Implements javax.sql.DataSource.getConnection.

Determine whether there are currently thread-bound credentials, using them if available, falling back to the statically specified username and password (i.e. values of the bean properties) otherwise.
public Connection
getConnection(String
the database user on whose behalf the connection is being made
username
,
String
the user's password
password
)

Overrides org.springframework.jdbc.datasource.DelegatingDataSource.getConnection.

Implements javax.sql.DataSource.getConnection.

Simply delegates to doGetConnection(String, String), keeping the given user credentials as-is.
public void
removeCredentialsFromCurrentThread()

Remove any user credentials for this proxy from the current thread.

public void
setCatalog(String catalog)

Specify a database catalog to be applied to each retrieved Connection.

public void
setCredentialsForCurrentThread(String
the username to apply
username
,
String
the password to apply
password
)

Set user credentials for this proxy and the current thread.

public void
setPassword(String password)

Set the default user's password that this adapter should use for retrieving Connections.

public void
setSchema(String schema)

Specify a database schema to be applied to each retrieved Connection.

public void
setUsername(String username)

Set the default username that this adapter should use for retrieving Connections.

Inherited from org.springframework.jdbc.datasource.DelegatingDataSource:
afterPropertiesSetcreateConnectionBuildercreateShardingKeyBuildergetLoginTimeoutgetLogWritergetParentLoggergetTargetDataSourceisWrapperForobtainTargetDataSourcesetLoginTimeoutsetLogWritersetTargetDataSourceunwrap

Field Detail

catalogback to summary
private String catalog
Annotations
@Nullable
passwordback to summary
private String password
Annotations
@Nullable
schemaback to summary
private String schema
Annotations
@Nullable
threadBoundCredentialsback to summary
private final ThreadLocal<UserCredentialsDataSourceAdapter.JdbcUserCredentials> threadBoundCredentials
usernameback to summary
private String username
Annotations
@Nullable

Constructor Detail

UserCredentialsDataSourceAdapterback to summary
public UserCredentialsDataSourceAdapter()

Method Detail

doGetConnectionback to summary
protected Connection doGetConnection(String username, String password) throws SQLException

This implementation delegates to the getConnection(username, password) method of the target DataSource, passing in the specified user credentials. If the specified username is empty, it will simply delegate to the standard getConnection() method of the target DataSource.

Parameters
username:String

the username to use

password:String

the password to use

Returns:Connection

the Connection

See Also
javax.sql.DataSource#getConnection(String, String), javax.sql.DataSource#getConnection()
getConnectionback to summary
public Connection getConnection() throws SQLException

Overrides org.springframework.jdbc.datasource.DelegatingDataSource.getConnection.

Implements javax.sql.DataSource.getConnection.

Determine whether there are currently thread-bound credentials, using them if available, falling back to the statically specified username and password (i.e. values of the bean properties) otherwise.

Delegates to doGetConnection(String, String) with the determined credentials as parameters.

Returns:Connection

Doc from javax.sql.DataSource.getConnection.

a connection to the data source

Annotations
@Override
Exceptions
SQLException:

Doc from javax.sql.DataSource.getConnection.

if a database access error occurs

See Also
doGetConnection
getConnectionback to summary
public Connection getConnection(String username, String password) throws SQLException

Overrides org.springframework.jdbc.datasource.DelegatingDataSource.getConnection.

Implements javax.sql.DataSource.getConnection.

Simply delegates to doGetConnection(String, String), keeping the given user credentials as-is.

Parameters
username:String

Doc from javax.sql.DataSource.getConnection.

the database user on whose behalf the connection is being made

password:String

Doc from javax.sql.DataSource.getConnection.

the user's password

Returns:Connection

Doc from javax.sql.DataSource.getConnection.

a connection to the data source

Annotations
@Override
Exceptions
SQLException:

Doc from javax.sql.DataSource.getConnection.

if a database access error occurs

removeCredentialsFromCurrentThreadback to summary
public void removeCredentialsFromCurrentThread()

Remove any user credentials for this proxy from the current thread. Statically specified user credentials apply again afterwards.

See Also
setCredentialsForCurrentThread
setCatalogback to summary
public void setCatalog(String catalog)

Specify a database catalog to be applied to each retrieved Connection.

Since
4.3.2
See Also
Connection#setCatalog
setCredentialsForCurrentThreadback to summary
public void setCredentialsForCurrentThread(String username, String password)

Set user credentials for this proxy and the current thread. The given username and password will be applied to all subsequent getConnection() calls on this DataSource proxy.

This will override any statically specified user credentials, that is, values of the "username" and "password" bean properties.

Parameters
username:String

the username to apply

password:String

the password to apply

See Also
removeCredentialsFromCurrentThread
setPasswordback to summary
public void setPassword(String password)

Set the default user's password that this adapter should use for retrieving Connections.

Default is no specific password. Note that an explicitly specified username will always override any username/password specified at the DataSource level.

See Also
setUsername, setCredentialsForCurrentThread(String, String), getConnection(String, String)
setSchemaback to summary
public void setSchema(String schema)

Specify a database schema to be applied to each retrieved Connection.

Since
4.3.2
See Also
Connection#setSchema
setUsernameback to summary
public void setUsername(String username)

Set the default username that this adapter should use for retrieving Connections.

Default is no specific user. Note that an explicitly specified username will always override any username/password specified at the DataSource level.

See Also
setPassword, setCredentialsForCurrentThread(String, String), getConnection(String, String)
org.springframework.jdbc.datasource back to summary

private final Class UserCredentialsDataSourceAdapter.JdbcUserCredentials

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter.JdbcUserCredentials

Inner class used as ThreadLocal value.

Field Summary

Modifier and TypeField and Description
public final String
public final String

Constructor Summary

AccessConstructor and Description
public
JdbcUserCredentials(String username, String password)

Method Summary

Modifier and TypeMethod and Description
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

passwordback to summary
public final String password
usernameback to summary
public final String username

Constructor Detail

JdbcUserCredentialsback to summary
public JdbcUserCredentials(String username, String password)

Method Detail

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override