Class UserCredentialsDataSourceAdapter
- All Implemented Interfaces:
- Wrapper,- CommonDataSource,- DataSource,- InitializingBean
- Direct Known Subclasses:
- IsolationLevelDataSourceAdapter
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.
- Since:
- 1.0.2
- Author:
- Juergen Hoeller
- See Also:
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionprotected ConnectiondoGetConnection(String username, String password) This implementation delegates to thegetConnection(username, password)method of the target DataSource, passing in the specified user credentials.Determine whether there are currently thread-bound credentials, using them if available, falling back to the statically specified username and password (i.e.getConnection(String username, String password) Simply delegates todoGetConnection(String, String), keeping the given user credentials as-is.voidRemove any user credentials for this proxy from the current thread.voidsetCatalog(String catalog) Specify a database catalog to be applied to each retrieved Connection.voidsetCredentialsForCurrentThread(String username, String password) Set user credentials for this proxy and the current thread.voidsetPassword(String password) Set the default user's password that this adapter should use for retrieving Connections.voidSpecify a database schema to be applied to each retrieved Connection.voidsetUsername(String username) Set the default username that this adapter should use for retrieving Connections.Methods inherited from class org.springframework.jdbc.datasource.DelegatingDataSourceafterPropertiesSet, createConnectionBuilder, createShardingKeyBuilder, getLoginTimeout, getLogWriter, getParentLogger, getTargetDataSource, isWrapperFor, obtainTargetDataSource, setLoginTimeout, setLogWriter, setTargetDataSource, unwrap
- 
Constructor Details- 
UserCredentialsDataSourceAdapterpublic UserCredentialsDataSourceAdapter()
 
- 
- 
Method Details- 
setUsernameSet 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. 
- 
setPasswordSet 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. 
- 
setCatalogSpecify a database catalog to be applied to each retrieved Connection.- Since:
- 4.3.2
- See Also:
 
- 
setSchemaSpecify a database schema to be applied to each retrieved Connection.- Since:
- 4.3.2
- See Also:
 
- 
setCredentialsForCurrentThreadSet user credentials for this proxy and the current thread. The given username and password will be applied to all subsequentgetConnection()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- the username to apply
- password- the password to apply
- See Also:
 
- 
removeCredentialsFromCurrentThreadpublic void removeCredentialsFromCurrentThread()Remove any user credentials for this proxy from the current thread. Statically specified user credentials apply again afterwards.
- 
getConnectionDetermine 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.- Specified by:
- getConnectionin interface- DataSource
- Overrides:
- getConnectionin class- DelegatingDataSource
- Throws:
- SQLException
- See Also:
 
- 
getConnectionSimply delegates todoGetConnection(String, String), keeping the given user credentials as-is.- Specified by:
- getConnectionin interface- DataSource
- Overrides:
- getConnectionin class- DelegatingDataSource
- Throws:
- SQLException
 
- 
doGetConnectionprotected Connection doGetConnection(@Nullable String username, @Nullable String password) throws SQLException This implementation delegates to thegetConnection(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 standardgetConnection()method of the target DataSource.- Parameters:
- username- the username to use
- password- the password to use
- Returns:
- the Connection
- Throws:
- SQLException
- See Also:
 
 
-