BackendConnection implements JDBCExecutionConnection { @Override public List<Connection> getConnections(final String dataSourceName, final int connectionSize, final ConnectionMode connectionMode) throws SQLException { return transactionStatus.isInTransaction() ? getConnectionsWithTransaction(dataSourceName, connectionSize, connectionMode) : getConnectionsWithoutTransaction(dataSourceName, connectionSize, connectionMode); } BackendConnection(final TransactionType initialTransactionType); void setCurrentSchema(final String schemaName); @Override List<Connection> getConnections(final String dataSourceName, final int connectionSize, final ConnectionMode connectionMode); @Override Statement createStorageResource(final Connection connection, final ConnectionMode connectionMode, final StatementOption option); @Override PreparedStatement createStorageResource(final String sql, final List<Object> parameters, 
                                                   final Connection connection, final ConnectionMode connectionMode, final StatementOption option); boolean isSerialExecute(); int getConnectionSize(); void add(final Statement statement); void add(final ResultSet resultSet); synchronized Collection<SQLException> closeResultSets(); synchronized Collection<SQLException> closeStatements(); synchronized Collection<SQLException> closeConnections(final boolean forceRollback);  }
@Test public void assertGetConnectionWithMethodInvocation() throws SQLException { backendConnection.getTransactionStatus().setInTransaction(true); when(backendDataSource.getConnections(anyString(), anyString(), eq(2), any())).thenReturn(MockConnectionUtil.mockNewConnections(2)); setMethodInvocation(); List<Connection> actualConnections = backendConnection.getConnections("ds1", 2, ConnectionMode.MEMORY_STRICTLY); verify(backendConnection.getMethodInvocations().iterator().next(), times(2)).invoke(any()); assertThat(actualConnections.size(), is(2)); assertTrue(backendConnection.getTransactionStatus().isInTransaction()); }
@Test public void assertMultiThreadsGetConnection() throws SQLException, InterruptedException { MockConnectionUtil.setCachedConnections(backendConnection, "ds1", 10); when(backendDataSource.getConnections(anyString(), anyString(), eq(2), any())).thenReturn(MockConnectionUtil.mockNewConnections(2)); Thread thread1 = new Thread(this::assertOneThreadResult); Thread thread2 = new Thread(this::assertOneThreadResult); thread1.start(); thread2.start(); thread1.join(); thread2.join(); }
@Test public void assertGetConnectionsWithoutTransactions() throws SQLException { backendConnection.getTransactionStatus().setInTransaction(false); List<Connection> connectionList = MockConnectionUtil.mockNewConnections(1); when(backendDataSource.getConnections(anyString(), anyString(), eq(1), any())).thenReturn(connectionList); List<Connection> fetchedConnections = backendConnection.getConnections("ds1", 1, null); assertThat(fetchedConnections.size(), is(1)); assertTrue(fetchedConnections.contains(connectionList.get(0))); assertConnectionsCached("ds1", connectionList); }