Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -329,23 +329,24 @@ private void putIntoVar(final JMeterVariables jmvars, final String name,
private static void putIntoVar(final JMeterVariables jmvars, final String name,
final Clob clob) throws SQLException {
try {
if (clob.length() > MAX_RETAIN_SIZE) {
try (Reader reader = clob.getCharacterStream(0,MAX_RETAIN_SIZE)) {
jmvars.put(
name,
TextStreamsKt.readText(reader)
+ "<result cut off, it is too big>");
}
} else {
try (Reader reader = clob.getCharacterStream()) {
jmvars.put(name, TextStreamsKt.readText(reader));
}
}
jmvars.put(name, clobToString(clob));
} catch (IOException e) {
log.warn("Could not read CLOB into {}", name, e);
}
}

private static String clobToString(Clob clob) throws SQLException, IOException {
if (clob.length() > MAX_RETAIN_SIZE) {
// Positions in a CLOB start at 1
try (Reader reader = clob.getCharacterStream(1, MAX_RETAIN_SIZE)) {
return TextStreamsKt.readText(reader) + "<result cut off, it is too big>";
}
}
try (Reader reader = clob.getCharacterStream()) {
return TextStreamsKt.readText(reader);
}
}

private void putIntoVar(final JMeterVariables jmvars, final String name,
final Blob blob) throws SQLException {
if (RS_STORE_AS_OBJECT.equals(resultSetHandler)) {
Expand Down Expand Up @@ -571,6 +572,12 @@ private static int processRow(ResultSet rs, ResultSetMetaData meta, StringBuilde
}
if (o instanceof byte[] bytes) {
o = new String(bytes, ENCODING);
} else if (o instanceof Clob clob) {
try {
o = clobToString(clob);
} catch (IOException e) {
log.warn("Could not read CLOB of column {}", meta.getColumnLabel(i), e);
}
}
sb.append(o);
if (i==numColumns){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import org.apache.jmeter.config.gui.SimpleConfigGui
import org.apache.jmeter.protocol.jdbc.executeForTest
import org.apache.jmeter.samplers.SampleResult
import org.apache.jmeter.testelement.TestElementSchema
import org.apache.jmeter.threads.JMeterContextService
import org.apache.jmeter.threads.JMeterVariables
import org.junit.jupiter.api.Assertions.assertArrayEquals
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
Expand All @@ -36,11 +38,13 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import java.nio.charset.StandardCharsets
import java.sql.Connection
import java.sql.ResultSet
import java.sql.ResultSetMetaData
import java.sql.SQLException
import java.sql.Statement
import javax.sql.rowset.serial.SerialClob

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class JDBCSamplerTest {
Expand Down Expand Up @@ -147,6 +151,52 @@ class JDBCSamplerTest {
verify(exactly = 0) { stmt.maxRows = any() }
}

@Test
fun `SELECT returns CLOB columns as text`() {
val body = """{"name":"jmeter"}"""
val response = executeSelectReturning(SerialClob(body.toCharArray()))

assertEquals("BODY\n$body\n", response, "response")
assertEquals(body, JMeterContextService.getContext().variables["body_1"], "variable body_1")
}

@Test
fun `SELECT cuts off CLOB columns larger than the retain size`() {
val retainSize = 64 * 1024
val response = executeSelectReturning(SerialClob("x".repeat(retainSize + 1).toCharArray()))

assertEquals("BODY\n${"x".repeat(retainSize)}<result cut off, it is too big>\n", response)
}

private fun executeSelectReturning(value: Any): String {
JMeterContextService.getContext().variables = JMeterVariables()
val meta = mockk<ResultSetMetaData> {
every { columnCount } returns 1
every { getColumnLabel(1) } returns "BODY"
}
val rs = mockk<ResultSet> {
every { metaData } returns meta
every { next() } returnsMany listOf(true, false)
every { getObject(1) } returns value
justRun { close() }
}
val stmt = mockk<Statement> {
every { executeQuery(any()) } returns rs
justRun { queryTimeout = any() }
justRun { close() }
}
val conn = mockk<Connection> {
every { createStatement() } returns stmt
}
val sample = mockk<SampleResult> {
justRun { latencyEnd() }
}

sut.query = "SELECT"
sut.variableNames = "body"
return String(sut.executeForTest(conn, sample), StandardCharsets.UTF_8)
}

@Test
fun `Catches SQLException during Connection closing`() {
val mockConnection = mockk<Connection> {
Expand Down
1 change: 1 addition & 0 deletions xdocs/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ Summary
<h3>JDBC Sampler</h3>
<ul>
<li><pr>6861</pr><issue>6345</issue>Separate the JDBC statement's maximum row limit from the client-side <code>Limit ResultSet</code> option, so users can limit rows read by JMeter without allowing the JDBC driver or database to optimize away query work.</li>
<li><issue>6106</issue>Return the text of <code>CLOB</code> columns in the response and the variables of the JDBC Request, instead of a string like <code>oracle.sql.CLOB@1b2c3d</code>. CLOBs larger than <code>jdbcsampler.max_retain_result_size</code> are cut off, and reading such a CLOB from a callable statement no longer fails, as it used to start at position 0 instead of 1.</li>
</ul>

<!-- =================== Thanks =================== -->
Expand Down