public class UserCredentialsDataSourceAdapter extends DelegatingDataSource
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.
getConnection()
Constructor and Description |
---|
UserCredentialsDataSourceAdapter() |
Modifier and Type | Method and Description |
---|---|
protected java.sql.Connection |
doGetConnection(java.lang.String username,
java.lang.String password)
This implementation delegates to the
getConnection(username, password)
method of the target DataSource, passing in the specified user credentials. |
java.sql.Connection |
getConnection()
Determine whether there are currently thread-bound credentials,
using them if available, falling back to the statically specified
username and password (i.e.
|
java.sql.Connection |
getConnection(java.lang.String username,
java.lang.String password)
Simply delegates to
doGetConnection(String, String) ,
keeping the given user credentials as-is. |
void |
removeCredentialsFromCurrentThread()
Remove any user credentials for this proxy from the current thread.
|
void |
setCatalog(java.lang.String catalog)
Specify a database catalog to be applied to each retrieved Connection.
|
void |
setCredentialsForCurrentThread(java.lang.String username,
java.lang.String password)
Set user credententials for this proxy and the current thread.
|
void |
setPassword(java.lang.String password)
Set the default user's password that this adapter should use for retrieving Connections.
|
void |
setSchema(java.lang.String schema)
Specify a database schema to be applied to each retrieved Connection.
|
void |
setUsername(java.lang.String username)
Set the default username that this adapter should use for retrieving Connections.
|
afterPropertiesSet, getLoginTimeout, getLogWriter, getParentLogger, getTargetDataSource, isWrapperFor, obtainTargetDataSource, setLoginTimeout, setLogWriter, setTargetDataSource, unwrap
public void setUsername(java.lang.String username)
Default is no specific user. Note that an explicitly specified username will always override any username/password specified at the DataSource level.
public void setPassword(java.lang.String password)
Default is no specific password. Note that an explicitly specified username will always override any username/password specified at the DataSource level.
public void setCatalog(java.lang.String catalog)
Connection.setCatalog(java.lang.String)
public void setSchema(java.lang.String schema)
Connection.setSchema(java.lang.String)
public void setCredentialsForCurrentThread(java.lang.String username, java.lang.String password)
getConnection()
calls on this DataSource proxy.
This will override any statically specified user credentials, that is, values of the "username" and "password" bean properties.
username
- the username to applypassword
- the password to applyremoveCredentialsFromCurrentThread()
public void removeCredentialsFromCurrentThread()
public java.sql.Connection getConnection() throws java.sql.SQLException
Delegates to doGetConnection(String, String)
with the
determined credentials as parameters.
getConnection
in interface javax.sql.DataSource
getConnection
in class DelegatingDataSource
java.sql.SQLException
public java.sql.Connection getConnection(java.lang.String username, java.lang.String password) throws java.sql.SQLException
doGetConnection(String, String)
,
keeping the given user credentials as-is.getConnection
in interface javax.sql.DataSource
getConnection
in class DelegatingDataSource
java.sql.SQLException
protected java.sql.Connection doGetConnection(@Nullable java.lang.String username, @Nullable java.lang.String password) throws java.sql.SQLException
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.username
- the username to usepassword
- the password to usejava.sql.SQLException
DataSource.getConnection(String, String)
,
DataSource.getConnection()