/** * Simply delegates to {@link #doGetConnection(String, String)}, * keeping the given user credentials as-is. */ @Override public Connection getConnection(String username, String password) throws SQLException { return doGetConnection(username, password); }
/** * This implementation delegates to the {@code 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 * {@code getConnection()} method of the target DataSource. * @param username the username to use * @param password the password to use * @return the Connection * @see javax.sql.DataSource#getConnection(String, String) * @see javax.sql.DataSource#getConnection() */ protected Connection doGetConnection(@Nullable String username, @Nullable String password) throws SQLException { Assert.state(getTargetDataSource() != null, "'targetDataSource' is required"); if (StringUtils.hasLength(username)) { return getTargetDataSource().getConnection(username, password); } else { return getTargetDataSource().getConnection(); } }
@Test public void testStaticCredentials() throws SQLException { DataSource dataSource = mock(DataSource.class); Connection connection = mock(Connection.class); given(dataSource.getConnection("user", "pw")).willReturn(connection); UserCredentialsDataSourceAdapter adapter = new UserCredentialsDataSourceAdapter(); adapter.setTargetDataSource(dataSource); adapter.setUsername("user"); adapter.setPassword("pw"); assertEquals(connection, adapter.getConnection()); }
@Test public void testThreadBoundCredentials() throws SQLException { DataSource dataSource = mock(DataSource.class); Connection connection = mock(Connection.class); given(dataSource.getConnection("user", "pw")).willReturn(connection); UserCredentialsDataSourceAdapter adapter = new UserCredentialsDataSourceAdapter(); adapter.setTargetDataSource(dataSource); adapter.setCredentialsForCurrentThread("user", "pw"); try { assertEquals(connection, adapter.getConnection()); } finally { adapter.removeCredentialsFromCurrentThread(); } }
@Test public void testNoCredentials() throws SQLException { DataSource dataSource = mock(DataSource.class); Connection connection = mock(Connection.class); given(dataSource.getConnection()).willReturn(connection); UserCredentialsDataSourceAdapter adapter = new UserCredentialsDataSourceAdapter(); adapter.setTargetDataSource(dataSource); assertEquals(connection, adapter.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. * <p>Delegates to {@link #doGetConnection(String, String)} with the * determined credentials as parameters. * @see #doGetConnection */ @Override public Connection getConnection() throws SQLException { JdbcUserCredentials threadCredentials = this.threadBoundCredentials.get(); Connection con = (threadCredentials != null ? doGetConnection(threadCredentials.username, threadCredentials.password) : doGetConnection(this.username, this.password)); if (this.catalog != null) { con.setCatalog(this.catalog); } if (this.schema != null) { con.setSchema(this.schema); } return con; }
/** * This implementation delegates to the {@code 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 * {@code getConnection()} method of the target DataSource. * @param username the username to use * @param password the password to use * @return the Connection * @see javax.sql.DataSource#getConnection(String, String) * @see javax.sql.DataSource#getConnection() */ protected Connection doGetConnection(String username, String password) throws SQLException { Assert.state(getTargetDataSource() != null, "'targetDataSource' is required"); if (StringUtils.hasLength(username)) { return getTargetDataSource().getConnection(username, password); } else { return getTargetDataSource().getConnection(); } }
/** * Applies the current isolation level value and read-only flag * to the returned Connection. * @see #getCurrentIsolationLevel() * @see #getCurrentReadOnlyFlag() */ @Override protected Connection doGetConnection(@Nullable String username, @Nullable String password) throws SQLException { Connection con = super.doGetConnection(username, password); Boolean readOnlyToUse = getCurrentReadOnlyFlag(); if (readOnlyToUse != null) { con.setReadOnly(readOnlyToUse); } Integer isolationLevelToUse = getCurrentIsolationLevel(); if (isolationLevelToUse != null) { con.setTransactionIsolation(isolationLevelToUse); } return con; }
/** * This implementation delegates to the {@code 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 * {@code getConnection()} method of the target DataSource. * @param username the username to use * @param password the password to use * @return the Connection * @see javax.sql.DataSource#getConnection(String, String) * @see javax.sql.DataSource#getConnection() */ protected Connection doGetConnection(@Nullable String username, @Nullable String password) throws SQLException { Assert.state(getTargetDataSource() != null, "'targetDataSource' is required"); if (StringUtils.hasLength(username)) { return getTargetDataSource().getConnection(username, password); } else { return getTargetDataSource().getConnection(); } }
/** * Simply delegates to {@link #doGetConnection(String, String)}, * keeping the given user credentials as-is. */ @Override public Connection getConnection(String username, String password) throws SQLException { return doGetConnection(username, password); }
/** * Simply delegates to {@link #doGetConnection(String, String)}, * keeping the given user credentials as-is. */ @Override public Connection getConnection(String username, String password) throws SQLException { return doGetConnection(username, password); }
/** * 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) else. * <p>Delegates to {@link #doGetConnection(String, String)} with the * determined credentials as parameters. */ @Override public Connection getConnection() throws SQLException { JdbcUserCredentials threadCredentials = this.threadBoundCredentials.get(); if (threadCredentials != null) { return doGetConnection(threadCredentials.username, threadCredentials.password); } else { return doGetConnection(this.username, this.password); } }
/** * 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. * <p>Delegates to {@link #doGetConnection(String, String)} with the * determined credentials as parameters. * @see #doGetConnection */ @Override public Connection getConnection() throws SQLException { JdbcUserCredentials threadCredentials = this.threadBoundCredentials.get(); Connection con = (threadCredentials != null ? doGetConnection(threadCredentials.username, threadCredentials.password) : doGetConnection(this.username, this.password)); if (this.catalog != null) { con.setCatalog(this.catalog); } if (this.schema != null) { con.setSchema(this.schema); } return con; }
/** * Applies the current isolation level value and read-only flag * to the returned Connection. * @see #getCurrentIsolationLevel() * @see #getCurrentReadOnlyFlag() */ @Override protected Connection doGetConnection(@Nullable String username, @Nullable String password) throws SQLException { Connection con = super.doGetConnection(username, password); Boolean readOnlyToUse = getCurrentReadOnlyFlag(); if (readOnlyToUse != null) { con.setReadOnly(readOnlyToUse); } Integer isolationLevelToUse = getCurrentIsolationLevel(); if (isolationLevelToUse != null) { con.setTransactionIsolation(isolationLevelToUse); } return con; }
/** * Applies the current isolation level value and read-only flag * to the returned Connection. * @see #getCurrentIsolationLevel() * @see #getCurrentReadOnlyFlag() */ @Override protected Connection doGetConnection(String username, String password) throws SQLException { Connection con = super.doGetConnection(username, password); Boolean readOnlyToUse = getCurrentReadOnlyFlag(); if (readOnlyToUse != null) { con.setReadOnly(readOnlyToUse); } Integer isolationLevelToUse = getCurrentIsolationLevel(); if (isolationLevelToUse != null) { con.setTransactionIsolation(isolationLevelToUse); } return con; }