This is an automated email from the ASF dual-hosted git repository.
jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 3e42218 Refactor ShardingSphereConnection.createArrayOf(), avoid use
first data source only (#12925)
3e42218 is described below
commit 3e422184d01e18a1f38f76d8db98d4800759d958
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Oct 8 11:10:59 2021 +0800
Refactor ShardingSphereConnection.createArrayOf(), avoid use first data
source only (#12925)
---
.../core/connection/ShardingSphereConnection.java | 16 +++++-------
.../connection/ShardingSphereConnectionTest.java | 30 +++++++++++-----------
2 files changed, 22 insertions(+), 24 deletions(-)
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java
index b9e2984..9f4284e 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java
@@ -91,7 +91,7 @@ public final class ShardingSphereConnection extends
AbstractConnectionAdapter im
/**
* Get random physical data source name.
- *
+ *
* @return random physical data source name
*/
public String getRandomPhysicalDataSourceName() {
@@ -189,7 +189,7 @@ public final class ShardingSphereConnection extends
AbstractConnectionAdapter im
public Statement createStorageResource(final Connection connection, final
ConnectionMode connectionMode, final StatementOption option) throws
SQLException {
return connection.createStatement(option.getResultSetType(),
option.getResultSetConcurrency(), option.getResultSetHoldability());
}
-
+
@SuppressWarnings("MagicConstant")
@Override
public PreparedStatement createStorageResource(final String sql, final
List<Object> parameters,
@@ -318,13 +318,6 @@ public final class ShardingSphereConnection extends
AbstractConnectionAdapter im
}
@Override
- public Array createArrayOf(final String typeName, final Object[] elements)
throws SQLException {
- String dataSourceName =
contextManager.getDataSourceMap(schema).keySet().iterator().next();
- Connection connection = getConnection(dataSourceName);
- return connection.createArrayOf(typeName, elements);
- }
-
- @Override
public boolean isReadOnly() {
return readOnly;
}
@@ -359,6 +352,11 @@ public final class ShardingSphereConnection extends
AbstractConnectionAdapter im
}
@Override
+ public Array createArrayOf(final String typeName, final Object[] elements)
throws SQLException {
+ return
getConnection(getRandomPhysicalDataSourceName()).createArrayOf(typeName,
elements);
+ }
+
+ @Override
public boolean isClosed() {
return closed;
}
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java
index 0abe99e..3087efd 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java
@@ -25,7 +25,6 @@ import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.transaction.ConnectionTransaction;
import
org.apache.shardingsphere.transaction.ConnectionTransaction.DistributedTransactionOperationType;
import org.apache.shardingsphere.transaction.TransactionHolder;
-import org.apache.shardingsphere.transaction.core.TransactionType;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import org.junit.After;
@@ -42,6 +41,7 @@ import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
@@ -254,27 +254,27 @@ public final class ShardingSphereConnectionTest {
verify(physicalConnection).setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
}
- @SuppressWarnings("unchecked")
- @SneakyThrows(ReflectiveOperationException.class)
- private Multimap<String, Connection> getCachedConnections(final
ShardingSphereConnection shardingSphereConnection) {
- Field field =
ShardingSphereConnection.class.getDeclaredField("cachedConnections");
- field.setAccessible(true);
- return (Multimap<String, Connection>)
field.get(shardingSphereConnection);
+ @Test
+ public void assertCreateArrayOf() throws SQLException {
+ Connection physicalConnection = mock(Connection.class);
+
when(connection.getContextManager().getDataSourceMap(DefaultSchema.LOGIC_NAME).get("ds").getConnection()).thenReturn(physicalConnection);
+ connection.getConnection("ds");
+ assertNull(connection.createArrayOf("int", null));
+ verify(physicalConnection).createArrayOf("int", null);
}
@Test
public void assertClose() throws SQLException {
connection.close();
assertTrue(connection.isClosed());
- assertTrue(getCachedConnections(connection).isEmpty());
+ assertTrue(getCachedConnections().isEmpty());
}
- @Test
- public void assertCloseShouldNotClearTransactionType() throws SQLException
{
- TransactionTypeHolder.set(TransactionType.XA);
- connection.close();
- assertTrue(connection.isClosed());
- assertTrue(getCachedConnections(connection).isEmpty());
- assertThat(TransactionTypeHolder.get(), is(TransactionType.XA));
+ @SuppressWarnings("unchecked")
+ @SneakyThrows(ReflectiveOperationException.class)
+ private Multimap<String, Connection> getCachedConnections() {
+ Field field =
ShardingSphereConnection.class.getDeclaredField("cachedConnections");
+ field.setAccessible(true);
+ return (Multimap<String, Connection>) field.get(connection);
}
}