### Question:
ShardingConnection extends AbstractConnectionAdapter { public Connection getConnection(final String dataSourceName, final SQLType sqlType) throws SQLException { Optional<Connection> connection = getCachedConnection(dataSourceName, sqlType); if (connection.isPresent()) { return connection.get(); } Context metricsContext = MetricsContext.start(Joiner.on("-").join("ShardingConnection-getConnection", dataSourceName)); DataSource dataSource = shardingContext.getShardingRule().getDataSourceRule().getDataSource(dataSourceName); Preconditions.checkState(null != dataSource, "Missing the rule of %s in DataSourceRule", dataSourceName); String realDataSourceName; if (dataSource instanceof MasterSlaveDataSource) { dataSource = ((MasterSlaveDataSource) dataSource).getDataSource(sqlType); realDataSourceName = MasterSlaveDataSource.getDataSourceName(dataSourceName, sqlType); } else { realDataSourceName = dataSourceName; } Connection result = dataSource.getConnection(); MetricsContext.stop(metricsContext); connectionMap.put(realDataSourceName, result); replayMethodsInvocation(result); return result; }  Collection<Connection> getConnectionForDDL(final String dataSourceName); Connection getConnection(final String dataSourceName, final SQLType sqlType); void release(final Connection connection); @Override DatabaseMetaData getMetaData(); @Override PreparedStatement prepareStatement(final String sql); @Override PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency); @Override PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability); @Override PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys); @Override PreparedStatement prepareStatement(final String sql, final int[] columnIndexes); @Override PreparedStatement prepareStatement(final String sql, final String[] columnNames); @Override Statement createStatement(); @Override Statement createStatement(final int resultSetType, final int resultSetConcurrency); @Override Statement createStatement(final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability); @Override Collection<Connection> getConnections(); @Override void close();  }### Answer:
@Test public void assertGetConnectionSelectThenUpdate() throws Exception { assertNotSame(connection.getConnection(DS_NAME, SQLType.DQL), connection.getConnection(DS_NAME, SQLType.DML)); }
@Test public void assertGetConnectionUpdateThenSelect() throws Exception { assertSame(connection.getConnection(DS_NAME, SQLType.DML), connection.getConnection(DS_NAME, SQLType.DQL)); }
@Test public void assertGetConnectionBothSelect() throws Exception { assertSame(connection.getConnection(DS_NAME, SQLType.DQL), connection.getConnection(DS_NAME, SQLType.DQL)); }
@Test public void assertGetConnectionBothUpdate() throws Exception { assertSame(connection.getConnection(DS_NAME, SQLType.DML), connection.getConnection(DS_NAME, SQLType.DML)); }
@Test public void assertGetConnectionMixed() throws Exception { Connection slaveConnection = connection.getConnection(DS_NAME, SQLType.DQL); Connection masterConnection = connection.getConnection(DS_NAME, SQLType.DML); assertNotSame(slaveConnection, masterConnection); assertNotSame(slaveConnection, connection.getConnection(DS_NAME, SQLType.DQL)); assertNotSame(slaveConnection, connection.getConnection(DS_NAME, SQLType.DML)); assertSame(masterConnection, connection.getConnection(DS_NAME, SQLType.DQL)); assertSame(masterConnection, connection.getConnection(DS_NAME, SQLType.DML)); }