Java Code Examples for org.springframework.jdbc.datasource.ConnectionProxy

The following examples show how to use org.springframework.jdbc.datasource.ConnectionProxy. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
void getConnection_NoReadReplicaAvailableNoTransactionActive_returnsDefaultDataSource()
		throws Exception {

	// Arrange
	DataSource defaultDataSource = mock(DataSource.class);
	Connection connection = mock(Connection.class);

	when(defaultDataSource.getConnection()).thenReturn(connection);

	ReadOnlyRoutingDataSource readOnlyRoutingDataSource = new ReadOnlyRoutingDataSource();
	readOnlyRoutingDataSource.setTargetDataSources(Collections.emptyMap());
	readOnlyRoutingDataSource.setDefaultTargetDataSource(defaultDataSource);
	readOnlyRoutingDataSource.afterPropertiesSet();

	LazyConnectionDataSourceProxy dataSource = new LazyConnectionDataSourceProxy(
			readOnlyRoutingDataSource);

	// Act
	Connection connectionReturned = dataSource.getConnection();

	// Assert
	assertThat(((ConnectionProxy) connectionReturned).getTargetConnection())
			.isSameAs(connection);
}
 
Example 2
Source Project: spring-analysis-note   Source File: JdbcTemplateTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testConnectionCallback() throws Exception {
	String result = this.template.execute(new ConnectionCallback<String>() {
		@Override
		public String doInConnection(Connection con) {
			assertTrue(con instanceof ConnectionProxy);
			assertSame(JdbcTemplateTests.this.connection, ((ConnectionProxy) con).getTargetConnection());
			return "test";
		}
	});
	assertEquals("test", result);
}
 
Example 3
Source Project: java-technology-stack   Source File: JdbcTemplateTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testConnectionCallback() throws Exception {
	String result = this.template.execute(new ConnectionCallback<String>() {
		@Override
		public String doInConnection(Connection con) {
			assertTrue(con instanceof ConnectionProxy);
			assertSame(JdbcTemplateTests.this.connection, ((ConnectionProxy) con).getTargetConnection());
			return "test";
		}
	});
	assertEquals("test", result);
}
 
Example 4
Source Project: spring-boot-mybatis-rw   Source File: RWPlugin.java    License: Apache License 2.0 5 votes vote down vote up
private void routeConnection(String key, Connection conn) {
	ConnectionHold.CURRENT_CONNECTION.set(key);
	// 同一个线程下保证最多只有一个写数据链接和读数据链接
	if (!ConnectionHold.CONNECTION_CONTEXT.get().containsKey(key)) {
		ConnectionProxy conToUse = (ConnectionProxy) conn;
		conn = conToUse.getTargetConnection();
		ConnectionHold.CONNECTION_CONTEXT.get().put(key, conn);
	}
}
 
Example 5
@Test
void getConnection_NoReadReplicaAvailableReadOnlyTransactionActive_returnsDefaultDataSource()
		throws Exception {

	// Arrange
	DataSource defaultDataSource = mock(DataSource.class);
	Connection connection = mock(Connection.class);

	when(defaultDataSource.getConnection()).thenReturn(connection);

	ReadOnlyRoutingDataSource readOnlyRoutingDataSource = new ReadOnlyRoutingDataSource();
	readOnlyRoutingDataSource.setTargetDataSources(Collections.emptyMap());
	readOnlyRoutingDataSource.setDefaultTargetDataSource(defaultDataSource);
	readOnlyRoutingDataSource.afterPropertiesSet();

	LazyConnectionDataSourceProxy dataSource = new LazyConnectionDataSourceProxy(
			readOnlyRoutingDataSource);

	DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
	transactionDefinition.setReadOnly(true);

	TransactionTemplate transactionTemplate = new TransactionTemplate(
			new DataSourceTransactionManager(dataSource), transactionDefinition);

	// Act
	Connection connectionReturned = transactionTemplate.execute(status -> {
		try {
			return ((ConnectionProxy) dataSource.getConnection())
					.getTargetConnection();
		}
		catch (SQLException e) {
			fail(e.getMessage());
		}
		return null;
	});

	// Assert
	assertThat(connectionReturned).isSameAs(connection);
}
 
Example 6
@Test
void getConnection_ReadReplicaAvailableReadOnlyTransactionActive_returnsReadReplicaDataSource()
		throws Exception {

	// Arrange
	DataSource defaultDataSource = mock(DataSource.class);
	Connection connection = mock(Connection.class);

	DataSource readOnlyDataSource = mock(DataSource.class);
	Connection readOnlyConnection = mock(Connection.class);

	when(readOnlyDataSource.getConnection()).thenReturn(readOnlyConnection);
	when(defaultDataSource.getConnection()).thenReturn(connection);

	ReadOnlyRoutingDataSource readOnlyRoutingDataSource = new ReadOnlyRoutingDataSource();
	readOnlyRoutingDataSource.setTargetDataSources(
			Collections.singletonMap("read1", readOnlyDataSource));
	readOnlyRoutingDataSource.setDefaultTargetDataSource(defaultDataSource);
	readOnlyRoutingDataSource.afterPropertiesSet();

	LazyConnectionDataSourceProxy dataSource = new LazyConnectionDataSourceProxy(
			readOnlyRoutingDataSource);

	DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
	transactionDefinition.setReadOnly(true);

	TransactionTemplate transactionTemplate = new TransactionTemplate(
			new DataSourceTransactionManager(dataSource), transactionDefinition);

	// Act
	Connection connectionReturned = transactionTemplate.execute(status -> {
		try {
			return ((ConnectionProxy) dataSource.getConnection())
					.getTargetConnection();
		}
		catch (SQLException e) {
			fail(e.getMessage());
		}
		return null;
	});

	// Assert
	assertThat(connectionReturned).isSameAs(readOnlyConnection);
}
 
Example 7
@Test
void getConnection_ReadReplicaAvailableWriteTransactionActive_returnsDefaultDataSource()
		throws Exception {

	// Arrange
	DataSource defaultDataSource = mock(DataSource.class);
	Connection connection = mock(Connection.class);

	DataSource readOnlyDataSource = mock(DataSource.class);
	Connection readOnlyConnection = mock(Connection.class);

	when(readOnlyDataSource.getConnection()).thenReturn(readOnlyConnection);
	when(defaultDataSource.getConnection()).thenReturn(connection);

	ReadOnlyRoutingDataSource readOnlyRoutingDataSource = new ReadOnlyRoutingDataSource();
	readOnlyRoutingDataSource.setTargetDataSources(
			Collections.singletonMap("read1", readOnlyDataSource));
	readOnlyRoutingDataSource.setDefaultTargetDataSource(defaultDataSource);
	readOnlyRoutingDataSource.afterPropertiesSet();

	LazyConnectionDataSourceProxy dataSource = new LazyConnectionDataSourceProxy(
			readOnlyRoutingDataSource);

	DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
	transactionDefinition.setReadOnly(false);

	TransactionTemplate transactionTemplate = new TransactionTemplate(
			new DataSourceTransactionManager(dataSource), transactionDefinition);

	// Act
	Connection connectionReturned = transactionTemplate.execute(status -> {
		try {
			return ((ConnectionProxy) dataSource.getConnection())
					.getTargetConnection();
		}
		catch (SQLException e) {
			fail(e.getMessage());
		}
		return null;
	});

	// Assert
	assertThat(connectionReturned).isSameAs(connection);
}
 
Example 8
Source Project: spring-analysis-note   Source File: JdbcTemplate.java    License: MIT License 3 votes vote down vote up
/**
 * Create a close-suppressing proxy for the given JDBC Connection.
 * Called by the {@code execute} method.
 * <p>The proxy also prepares returned JDBC Statements, applying
 * statement settings such as fetch size, max rows, and query timeout.
 * @param con the JDBC Connection to create a proxy for
 * @return the Connection proxy
 * @see java.sql.Connection#close()
 * @see #execute(ConnectionCallback)
 * @see #applyStatementSettings
 */
protected Connection createConnectionProxy(Connection con) {
	return (Connection) Proxy.newProxyInstance(
			ConnectionProxy.class.getClassLoader(),
			new Class<?>[] {ConnectionProxy.class},
			new CloseSuppressingInvocationHandler(con));
}
 
Example 9
Source Project: java-technology-stack   Source File: JdbcTemplate.java    License: MIT License 3 votes vote down vote up
/**
 * Create a close-suppressing proxy for the given JDBC Connection.
 * Called by the {@code execute} method.
 * <p>The proxy also prepares returned JDBC Statements, applying
 * statement settings such as fetch size, max rows, and query timeout.
 * @param con the JDBC Connection to create a proxy for
 * @return the Connection proxy
 * @see java.sql.Connection#close()
 * @see #execute(ConnectionCallback)
 * @see #applyStatementSettings
 */
protected Connection createConnectionProxy(Connection con) {
	return (Connection) Proxy.newProxyInstance(
			ConnectionProxy.class.getClassLoader(),
			new Class<?>[] {ConnectionProxy.class},
			new CloseSuppressingInvocationHandler(con));
}
 
Example 10
Source Project: lams   Source File: JdbcTemplate.java    License: GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a close-suppressing proxy for the given JDBC Connection.
 * Called by the {@code execute} method.
 * <p>The proxy also prepares returned JDBC Statements, applying
 * statement settings such as fetch size, max rows, and query timeout.
 * @param con the JDBC Connection to create a proxy for
 * @return the Connection proxy
 * @see java.sql.Connection#close()
 * @see #execute(ConnectionCallback)
 * @see #applyStatementSettings
 */
protected Connection createConnectionProxy(Connection con) {
	return (Connection) Proxy.newProxyInstance(
			ConnectionProxy.class.getClassLoader(),
			new Class<?>[] {ConnectionProxy.class},
			new CloseSuppressingInvocationHandler(con));
}
 
Example 11
Source Project: spring4-understanding   Source File: JdbcTemplate.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Create a close-suppressing proxy for the given JDBC Connection.
 * Called by the {@code execute} method.
 * <p>The proxy also prepares returned JDBC Statements, applying
 * statement settings such as fetch size, max rows, and query timeout.
 * @param con the JDBC Connection to create a proxy for
 * @return the Connection proxy
 * @see java.sql.Connection#close()
 * @see #execute(ConnectionCallback)
 * @see #applyStatementSettings
 */
protected Connection createConnectionProxy(Connection con) {
	return (Connection) Proxy.newProxyInstance(
			ConnectionProxy.class.getClassLoader(),
			new Class<?>[] {ConnectionProxy.class},
			new CloseSuppressingInvocationHandler(con));
}
 
Example 12
Source Project: effectivejava   Source File: JdbcTemplate.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Create a close-suppressing proxy for the given JDBC Connection.
 * Called by the {@code execute} method.
 * <p>The proxy also prepares returned JDBC Statements, applying
 * statement settings such as fetch size, max rows, and query timeout.
 * @param con the JDBC Connection to create a proxy for
 * @return the Connection proxy
 * @see java.sql.Connection#close()
 * @see #execute(ConnectionCallback)
 * @see #applyStatementSettings
 */
protected Connection createConnectionProxy(Connection con) {
	return (Connection) Proxy.newProxyInstance(
			ConnectionProxy.class.getClassLoader(),
			new Class<?>[] {ConnectionProxy.class},
			new CloseSuppressingInvocationHandler(con));
}
 
Example 13
Source Project: spring-boot-mybatis-rw   Source File: DataSourceProxy.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Return a Connection handle that lazily fetches an actual JDBC Connection
 * when asked for a Statement (or PreparedStatement or CallableStatement).
 * <p>
 * The returned Connection handle implements the ConnectionProxy interface,
 * allowing to retrieve the underlying target Connection.
 * 
 * @return a lazy Connection handle
 * @see ConnectionProxy#getTargetConnection()
 */
@Override
public Connection getConnection() throws SQLException {
	return (Connection) Proxy.newProxyInstance(ConnectionProxy.class.getClassLoader(),
			new Class<?>[] { ConnectionProxy.class }, new LazyConnectionInvocationHandler());
}
 
Example 14
Source Project: spring-boot-mybatis-rw   Source File: DataSourceProxy.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Return a Connection handle that lazily fetches an actual JDBC Connection
 * when asked for a Statement (or PreparedStatement or CallableStatement).
 * <p>
 * The returned Connection handle implements the ConnectionProxy interface,
 * allowing to retrieve the underlying target Connection.
 * 
 * @param username
 *            the per-Connection username
 * @param password
 *            the per-Connection password
 * @return a lazy Connection handle
 * @see ConnectionProxy#getTargetConnection()
 */
@Override
public Connection getConnection(String username, String password) throws SQLException {
	return (Connection) Proxy.newProxyInstance(ConnectionProxy.class.getClassLoader(),
			new Class<?>[] { ConnectionProxy.class }, new LazyConnectionInvocationHandler(username, password));
}