Skip to content

Commit 945281c

Browse files
committed
feat: pass JDBCSampler.maxRows to Statement.setMaxRows so the driver does not fetch extra rows from the database
1 parent aa22e93 commit 945281c

4 files changed

Lines changed: 18 additions & 3 deletions

File tree

src/protocol/jdbc/src/main/java/org/apache/jmeter/protocol/jdbc/AbstractJDBCTestElement.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ protected byte[] execute(Connection conn, SampleResult sample) throws SQLExcepti
167167
if (SELECT.equals(currentQueryType)) {
168168
try (Statement stmt = conn.createStatement()) {
169169
setQueryTimeout(stmt, getIntegerQueryTimeout());
170+
configureMaxRows(stmt);
170171
ResultSet rs = null;
171172
try {
172173
rs = stmt.executeQuery(getQuery());
@@ -198,6 +199,7 @@ protected byte[] execute(Connection conn, SampleResult sample) throws SQLExcepti
198199
} else if (PREPARED_SELECT.equals(currentQueryType)) {
199200
try (PreparedStatement pstmt = getPreparedStatement(conn)) {
200201
setArguments(pstmt);
202+
configureMaxRows(pstmt);
201203
ResultSet rs = null;
202204
try {
203205
rs = pstmt.executeQuery();
@@ -236,7 +238,15 @@ protected byte[] execute(Connection conn, SampleResult sample) throws SQLExcepti
236238
}
237239
}
238240

241+
private void configureMaxRows(Statement stmt) throws SQLException {
242+
int maxRows = getIntegerResultSetMaxRows();
243+
if (maxRows >= 0) {
244+
stmt.setMaxRows(maxRows);
245+
}
246+
}
247+
239248
private String resultSetsToString(PreparedStatement pstmt, boolean result, int[] out) throws SQLException, UnsupportedEncodingException {
249+
configureMaxRows(pstmt);
240250
StringBuilder sb = new StringBuilder();
241251
int updateCount = 0;
242252
boolean currentResult = result;

src/protocol/jdbc/src/main/java/org/apache/jmeter/protocol/jdbc/sampler/JDBCSampler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,17 @@ public SampleResult sample(Entry e) {
7272
Connection conn = null;
7373

7474
try {
75-
if (JOrphanUtils.isBlank(getDataSource())) {
75+
String dataSource = getDataSource();
76+
if (JOrphanUtils.isBlank(dataSource)) {
7677
throw new IllegalArgumentException("Name for DataSoure must not be empty in " + getName());
7778
}
7879

7980
try {
80-
conn = DataSourceElement.getConnection(getDataSource());
81+
conn = DataSourceElement.getConnection(dataSource);
8182
} finally {
8283
res.connectEnd();
8384
}
84-
res.setResponseHeaders(DataSourceElement.getConnectionInfo(getDataSource()));
85+
res.setResponseHeaders(DataSourceElement.getConnectionInfo(dataSource));
8586
res.setResponseData(execute(conn, res));
8687
} catch (SQLException ex) {
8788
final String errCode = Integer.toString(ex.getErrorCode());

src/protocol/jdbc/src/test/kotlin/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class JDBCSamplerTest {
8080
val stmt = mockk<Statement> {
8181
every { executeQuery(any()) } returns rs
8282
justRun { queryTimeout = any() }
83+
justRun { maxRows = any() }
8384
justRun { close() }
8485
}
8586
val conn = mockk<Connection> {
@@ -90,11 +91,13 @@ class JDBCSamplerTest {
9091
}
9192

9293
sut.query = "SELECT"
94+
sut.resultSetMaxRows = "10"
9395
val response = sut.executeForTest(conn, sample)
9496

9597
verifyOrder {
9698
conn.createStatement()
9799
stmt.queryTimeout = 0
100+
stmt.maxRows = 10
98101
stmt.executeQuery(any())
99102
sample.latencyEnd()
100103
// getStringFromResultSet

xdocs/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Summary
8383
<li><issue>6165</issue><pr>6192</pr>The Constant Throughput Timer is throwing a NullPointerException when using variables (vars.get) in "Target Throughput"-field</li>
8484
<li><issue>6162</issue>The Constant Timer is throwing a NullPointerException when using variables (vars.get) in "delay"-field</li>
8585
<li><pr>6193</pr>Log errors happening while JMeter starts the test (previously, errors from TestStateListener.testStarted were not logged)</li>
86+
<li><pr>6216</pr>Pass JDBCSampler.maxRows to Statement.setMaxRows so the driver does not fetch extra rows from the database</li>
8687
</ul>
8788

8889
<ch_section>Non-functional changes</ch_section>

0 commit comments

Comments
 (0)