Skip to content

Commit

Permalink
Do not create a proxy when ResultSet is null
Browse files Browse the repository at this point in the history
`Statement.getResultSet` can return `null`.
In that case, do not create a `ResultSet` proxy.

Resolves #117
  • Loading branch information
ttddyy committed Nov 6, 2024
1 parent baa4421 commit 1274bc8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ protected Object performProxyLogic(Object proxy, Method method, Object[] args, M
final boolean isResultSetReturningMethod = !isGetGeneratedKeysMethod && METHODS_TO_RETURN_RESULTSET.contains(methodName);

final boolean isCreateGeneratedKeysProxy = isGetGeneratedKeysMethod && this.proxyConfig.isGeneratedKeysProxyEnabled();
final boolean isCreateResultSetProxy = isResultSetReturningMethod && this.proxyConfig.isResultSetProxyEnabled();
final boolean isCreateResultSetProxy = (retVal != null && isResultSetReturningMethod && this.proxyConfig.isResultSetProxyEnabled());

// create proxy for returned ResultSet
if (isCreateGeneratedKeysProxy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

import javax.sql.DataSource;
import java.lang.reflect.Proxy;
import java.sql.*;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**
* Dynamic Proxy Class(Jdk Proxy) based {@link net.ttddyy.dsproxy.proxy.JdbcProxyFactory} implementation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,6 @@ public void testEquals() throws Throwable {

@Test
public void proxyResultSet() throws Throwable {

final AtomicReference<Object> listenerReceivedResult = new AtomicReference<Object>();
QueryExecutionListener listener = new NoOpQueryExecutionListener() {
@Override
Expand All @@ -817,13 +816,11 @@ public void afterQuery(ExecutionInfo execInfo, List<QueryInfo> queryInfoList) {
}
};


ResultSetMetaData metaData = mock(ResultSetMetaData.class);

ResultSet resultSet = mock(ResultSet.class);
when(resultSet.getMetaData()).thenReturn(metaData);


Statement stmt = mock(Statement.class);
when(stmt.executeQuery(anyString())).thenReturn(resultSet);
when(stmt.getGeneratedKeys()).thenReturn(resultSet);
Expand Down Expand Up @@ -859,8 +856,18 @@ public void afterQuery(ExecutionInfo execInfo, List<QueryInfo> queryInfoList) {
result = logic.invoke(null, getGeneratedKeysMethod, null);
assertThat(result).isInstanceOf(ResultSet.class);
assertFalse(Proxy.isProxyClass(result.getClass()));
}

@Test
public void resultSetIsNull() throws Throwable {
Statement stmt = mock(Statement.class);
when(stmt.getResultSet()).thenReturn(null);
Method getResultSetMethod = Statement.class.getMethod("getResultSet");

StatementProxyLogic logic = getProxyLogic(stmt, null, null, true, false);
Object result = logic.invoke(null, getResultSetMethod, null);

assertThat(result).isNull();
}

@Test
Expand Down

0 comments on commit 1274bc8

Please sign in to comment.