DataSource adapters expose setCatalog/setSchema next to setUsername/setPassword
Issue: SPR-14501
(cherry picked from commit 610bdf1)
This commit is contained in:
parent
8ccfdbbcf9
commit
77f22e9674
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -20,6 +20,7 @@ import java.sql.Connection;
|
|||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.lang.UsesJava7;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
|
|
@ -39,6 +40,10 @@ public abstract class AbstractDriverBasedDataSource extends AbstractDataSource {
|
|||
|
||||
private String password;
|
||||
|
||||
private String catalog;
|
||||
|
||||
private String schema;
|
||||
|
||||
private Properties connectionProperties;
|
||||
|
||||
|
||||
|
|
@ -88,6 +93,40 @@ public abstract class AbstractDriverBasedDataSource extends AbstractDataSource {
|
|||
return this.password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a database catalog to be applied to each Connection.
|
||||
* @since 4.3.2
|
||||
* @see Connection#setCatalog
|
||||
*/
|
||||
public void setCatalog(String catalog) {
|
||||
this.catalog = catalog;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the database catalog to be applied to each Connection, if any.
|
||||
* @since 4.3.2
|
||||
*/
|
||||
public String getCatalog() {
|
||||
return this.catalog;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a database schema to be applied to each Connection.
|
||||
* @since 4.3.2
|
||||
* @see Connection#setSchema
|
||||
*/
|
||||
public void setSchema(String schema) {
|
||||
this.schema = schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the database schema to be applied to each Connection, if any.
|
||||
* @since 4.3.2
|
||||
*/
|
||||
public String getSchema() {
|
||||
return this.schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify arbitrary connection properties as key/value pairs,
|
||||
* to be passed to the Driver.
|
||||
|
|
@ -140,6 +179,7 @@ public abstract class AbstractDriverBasedDataSource extends AbstractDataSource {
|
|||
* @throws SQLException in case of failure
|
||||
* @see java.sql.Driver#connect(String, java.util.Properties)
|
||||
*/
|
||||
@UsesJava7
|
||||
protected Connection getConnectionFromDriver(String username, String password) throws SQLException {
|
||||
Properties mergedProps = new Properties();
|
||||
Properties connProps = getConnectionProperties();
|
||||
|
|
@ -152,7 +192,15 @@ public abstract class AbstractDriverBasedDataSource extends AbstractDataSource {
|
|||
if (password != null) {
|
||||
mergedProps.setProperty("password", password);
|
||||
}
|
||||
return getConnectionFromDriver(mergedProps);
|
||||
|
||||
Connection con = getConnectionFromDriver(mergedProps);
|
||||
if (this.catalog != null) {
|
||||
con.setCatalog(this.catalog);
|
||||
}
|
||||
if (this.schema != null) {
|
||||
con.setSchema(this.schema);
|
||||
}
|
||||
return con;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -20,6 +20,7 @@ import java.sql.Connection;
|
|||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.core.NamedThreadLocal;
|
||||
import org.springframework.lang.UsesJava7;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
|
@ -65,6 +66,10 @@ public class UserCredentialsDataSourceAdapter extends DelegatingDataSource {
|
|||
|
||||
private String password;
|
||||
|
||||
private String catalog;
|
||||
|
||||
private String schema;
|
||||
|
||||
private final ThreadLocal<JdbcUserCredentials> threadBoundCredentials =
|
||||
new NamedThreadLocal<JdbcUserCredentials>("Current JDBC user credentials");
|
||||
|
||||
|
|
@ -93,6 +98,24 @@ public class UserCredentialsDataSourceAdapter extends DelegatingDataSource {
|
|||
this.password = password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a database catalog to be applied to each retrieved Connection.
|
||||
* @since 4.3.2
|
||||
* @see Connection#setCatalog
|
||||
*/
|
||||
public void setCatalog(String catalog) {
|
||||
this.catalog = catalog;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a database schema to be applied to each retrieved Connection.
|
||||
* @since 4.3.2
|
||||
* @see Connection#setSchema
|
||||
*/
|
||||
public void setSchema(String schema) {
|
||||
this.schema = schema;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set user credententials for this proxy and the current thread.
|
||||
|
|
@ -126,14 +149,20 @@ public class UserCredentialsDataSourceAdapter extends DelegatingDataSource {
|
|||
* determined credentials as parameters.
|
||||
*/
|
||||
@Override
|
||||
@UsesJava7
|
||||
public Connection getConnection() throws SQLException {
|
||||
JdbcUserCredentials threadCredentials = this.threadBoundCredentials.get();
|
||||
if (threadCredentials != null) {
|
||||
return doGetConnection(threadCredentials.username, threadCredentials.password);
|
||||
Connection con = (threadCredentials != null ?
|
||||
doGetConnection(threadCredentials.username, threadCredentials.password) :
|
||||
doGetConnection(this.username, this.password));
|
||||
|
||||
if (this.catalog != null) {
|
||||
con.setCatalog(this.catalog);
|
||||
}
|
||||
else {
|
||||
return doGetConnection(this.username, this.password);
|
||||
if (this.schema != null) {
|
||||
con.setSchema(this.schema);
|
||||
}
|
||||
return con;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue