You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2021/05/25 12:38:32 UTC
[shardingsphere] branch master updated: Replace reflection with
lambda in BackendConnection (#10466)
This is an automated email from the ASF dual-hosted git repository.
menghaoran 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 b838c6e Replace reflection with lambda in BackendConnection (#10466)
b838c6e is described below
commit b838c6ea1b684b2d6257c4792111439dbfee86bd
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Tue May 25 20:37:51 2021 +0800
Replace reflection with lambda in BackendConnection (#10466)
---
.../jdbc/connection/BackendConnection.java | 10 +++----
...nvocation.java => ConnectionPostProcessor.java} | 29 +++++-------------
.../jdbc/transaction/LocalTransactionManager.java | 15 +++++-----
.../jdbc/connection/BackendConnectionTest.java | 28 ++++++++---------
.../jdbc/connection/MethodInvocationTest.java | 35 ----------------------
5 files changed, 34 insertions(+), 83 deletions(-)
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnection.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnection.java
index 12a493e..3381863 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnection.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnection.java
@@ -78,7 +78,7 @@ public final class BackendConnection implements ExecutorJDBCManager {
private final Collection<ResultSet> cachedResultSets = new CopyOnWriteArrayList<>();
- private final Collection<MethodInvocation> methodInvocations = new LinkedList<>();
+ private final Collection<ConnectionPostProcessor> connectionPostProcessors = new LinkedList<>();
private final ResourceLock resourceLock = new ResourceLock();
@@ -151,9 +151,9 @@ public final class BackendConnection implements ExecutorJDBCManager {
return result;
}
- private void replayMethodsInvocation(final Object target) {
- for (MethodInvocation each : methodInvocations) {
- each.invoke(target);
+ private void replayMethodsInvocation(final Connection target) {
+ for (ConnectionPostProcessor each : connectionPostProcessors) {
+ each.process(target);
}
}
@@ -285,7 +285,7 @@ public final class BackendConnection implements ExecutorJDBCManager {
}
}
cachedConnections.clear();
- methodInvocations.clear();
+ connectionPostProcessors.clear();
connectionStatus.switchToReleased();
return result;
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/MethodInvocation.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/ConnectionPostProcessor.java
similarity index 63%
rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/MethodInvocation.java
rename to shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/ConnectionPostProcessor.java
index ca3cc40..23d0f31 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/MethodInvocation.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/ConnectionPostProcessor.java
@@ -17,31 +17,18 @@
package org.apache.shardingsphere.proxy.backend.communication.jdbc.connection;
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import lombok.SneakyThrows;
-
-import java.lang.reflect.Method;
+import java.sql.Connection;
/**
- * Reflective method invocation.
+ * Connection post processor.
*/
-@RequiredArgsConstructor
-public final class MethodInvocation {
-
- @Getter
- private final Method method;
-
- @Getter
- private final Object[] arguments;
+@FunctionalInterface
+public interface ConnectionPostProcessor {
/**
- * Invoke method.
- *
- * @param target target object
+ * Process connection.
+ *
+ * @param target target connection
*/
- @SneakyThrows(ReflectiveOperationException.class)
- public void invoke(final Object target) {
- method.invoke(target, arguments);
- }
+ void process(Connection target);
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/transaction/LocalTransactionManager.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/transaction/LocalTransactionManager.java
index 3d84a61..1011256 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/transaction/LocalTransactionManager.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/transaction/LocalTransactionManager.java
@@ -18,9 +18,7 @@
package org.apache.shardingsphere.proxy.backend.communication.jdbc.transaction;
import lombok.RequiredArgsConstructor;
-import lombok.SneakyThrows;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
-import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.MethodInvocation;
import java.sql.Connection;
import java.sql.SQLException;
@@ -37,7 +35,13 @@ public final class LocalTransactionManager implements TransactionManager {
@Override
public void begin() {
- recordMethodInvocation(Connection.class, "setAutoCommit", new Class[]{boolean.class}, new Object[]{false});
+ connection.getConnectionPostProcessors().add(target -> {
+ try {
+ target.setAutoCommit(false);
+ } catch (final SQLException ex) {
+ throw new RuntimeException(ex);
+ }
+ });
}
@Override
@@ -90,9 +94,4 @@ public final class LocalTransactionManager implements TransactionManager {
}
throw ex;
}
-
- @SneakyThrows(ReflectiveOperationException.class)
- private void recordMethodInvocation(final Class<?> targetClass, final String methodName, final Class<?>[] argumentTypes, final Object[] arguments) {
- connection.getMethodInvocations().add(new MethodInvocation(targetClass.getMethod(methodName, argumentTypes), arguments));
- }
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnectionTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnectionTest.java
index b97432d..b7498eb 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnectionTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnectionTest.java
@@ -168,24 +168,24 @@ public final class BackendConnectionTest {
}
@Test
- public void assertGetConnectionWithMethodInvocation() throws SQLException {
+ public void assertGetConnectionWithConnectionPostProcessors() throws SQLException {
backendConnection.getTransactionStatus().setInTransaction(true);
when(backendDataSource.getConnections(anyString(), anyString(), eq(2), any())).thenReturn(MockConnectionUtil.mockNewConnections(2));
- setMethodInvocation();
+ setConnectionPostProcessors();
List<Connection> actualConnections = backendConnection.getConnections("ds1", 2, ConnectionMode.MEMORY_STRICTLY);
- verify(backendConnection.getMethodInvocations().iterator().next(), times(2)).invoke(any());
+ verify(backendConnection.getConnectionPostProcessors().iterator().next(), times(2)).process(any());
assertThat(actualConnections.size(), is(2));
assertTrue(backendConnection.getTransactionStatus().isInTransaction());
}
@SneakyThrows(ReflectiveOperationException.class)
- private void setMethodInvocation() {
- MethodInvocation invocation = mock(MethodInvocation.class);
- Collection<MethodInvocation> methodInvocations = new LinkedList<>();
- methodInvocations.add(invocation);
- Field field = backendConnection.getClass().getDeclaredField("methodInvocations");
+ private void setConnectionPostProcessors() {
+ ConnectionPostProcessor invocation = mock(ConnectionPostProcessor.class);
+ Collection<ConnectionPostProcessor> connectionPostProcessors = new LinkedList<>();
+ connectionPostProcessors.add(invocation);
+ Field field = backendConnection.getClass().getDeclaredField("connectionPostProcessors");
field.setAccessible(true);
- field.set(backendConnection, methodInvocations);
+ field.set(backendConnection, connectionPostProcessors);
}
@Test
@@ -343,7 +343,7 @@ public final class BackendConnectionTest {
backendConnection.closeConnections(false);
verify(connection, times(1)).close();
assertTrue(cachedConnections.isEmpty());
- verifyMethodInvocationsEmpty();
+ verifyConnectionPostProcessorsEmpty();
verify(connectionStatus, times(1)).switchToReleased();
}
@@ -429,10 +429,10 @@ public final class BackendConnectionTest {
@SuppressWarnings("unchecked")
@SneakyThrows(ReflectiveOperationException.class)
- private void verifyMethodInvocationsEmpty() {
- Field field = backendConnection.getClass().getDeclaredField("methodInvocations");
+ private void verifyConnectionPostProcessorsEmpty() {
+ Field field = backendConnection.getClass().getDeclaredField("connectionPostProcessors");
field.setAccessible(true);
- Collection<MethodInvocation> methodInvocations = (Collection<MethodInvocation>) field.get(backendConnection);
- assertTrue(methodInvocations.isEmpty());
+ Collection<ConnectionPostProcessor> connectionPostProcessors = (Collection<ConnectionPostProcessor>) field.get(backendConnection);
+ assertTrue(connectionPostProcessors.isEmpty());
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/MethodInvocationTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/MethodInvocationTest.java
deleted file mode 100644
index ad63f19..0000000
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/MethodInvocationTest.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.proxy.backend.communication.jdbc.connection;
-
-import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.fixture.MethodInvocationFixture;
-import org.junit.Test;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-public final class MethodInvocationTest {
-
- @Test
- public void assertInvoke() throws NoSuchMethodException {
- MethodInvocationFixture methodInvocationFixture = new MethodInvocationFixture();
- MethodInvocation methodInvocation = new MethodInvocation(MethodInvocationFixture.class.getMethod("setId", Integer.class), new Object[]{1});
- methodInvocation.invoke(methodInvocationFixture);
- assertThat(methodInvocationFixture.getId(), is(1));
- }
-}