org.springframework.jdbc.datasource.ConnectionProxy Java Examples
The following examples show how to use
org.springframework.jdbc.datasource.ConnectionProxy.
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
Source File: ReadOnlyRoutingDataSourceTest.java From spring-cloud-aws with Apache License 2.0 | 6 votes |
@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 File: JdbcTemplateTests.java From spring-analysis-note with MIT License | 5 votes |
@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 File: JdbcTemplateTests.java From java-technology-stack with MIT License | 5 votes |
@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 File: RWPlugin.java From spring-boot-mybatis-rw with Apache License 2.0 | 5 votes |
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
Source File: ReadOnlyRoutingDataSourceTest.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@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
Source File: ReadOnlyRoutingDataSourceTest.java From spring-cloud-aws with Apache License 2.0 | 4 votes |
@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
Source File: ReadOnlyRoutingDataSourceTest.java From spring-cloud-aws with Apache License 2.0 | 4 votes |
@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 File: JdbcTemplate.java From spring-analysis-note with MIT License | 3 votes |
/** * 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 File: JdbcTemplate.java From java-technology-stack with MIT License | 3 votes |
/** * 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 File: JdbcTemplate.java From lams with GNU General Public License v2.0 | 3 votes |
/** * 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 File: JdbcTemplate.java From spring4-understanding with Apache License 2.0 | 3 votes |
/** * 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 File: JdbcTemplate.java From effectivejava with Apache License 2.0 | 3 votes |
/** * 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 File: DataSourceProxy.java From spring-boot-mybatis-rw with Apache License 2.0 | 2 votes |
/** * 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 File: DataSourceProxy.java From spring-boot-mybatis-rw with Apache License 2.0 | 2 votes |
/** * 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)); }