Skip to content

Commit df3732b

Browse files
committed
Use try-with-resources to manage JDBC Clob in
CSVPrinter.printRecords(ResultSet)
1 parent ab5331c commit df3732b

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@
4444
<!-- ADD -->
4545
<action issue="CSV-308" type="fix" dev="ggregory" due-to="Buddhi De Silva, Gary Gregory">[Javadoc] Add example to CSVFormat#setHeaderComments() #344.</action>
4646
<!-- FIX -->
47-
<action issue="CSV-306" type="fix" dev="ggregory" due-to="Sam Ng, Bruno P. Kinoshita">Replace deprecated method in user guide, update external link #324, #325.</action>
47+
<action type="fix" issue="CSV-306" dev="ggregory" due-to="Sam Ng, Bruno P. Kinoshita">Replace deprecated method in user guide, update external link #324, #325.</action>
4848
<action type="fix" dev="ggregory" due-to="Seth Falco, Bruno P. Kinoshita">Document duplicate header behavior #309.</action>
4949
<action type="fix" dev="ggregory" due-to="jkbkupczyk">Add missing docs #328.</action>
5050
<action type="fix" dev="ggregory" due-to="step-security-bot">[StepSecurity] CI: Harden GitHub Actions #329, #330.</action>
5151
<action type="fix" issue="CSV-147" dev="ggregory" due-to="Steven Peterson, Benedikt Ritter, Gary Gregory, Joerg Schaible, Buddhi De Silva, Elliotte Rusty Harold">Better error message during faulty CSV record read #347.</action>
5252
<action type="fix" issue="CSV-310" dev="ggregory" due-to="Buddhi De Silva">Misleading error message when QuoteMode set to None #352.</action>
5353
<action type="fix" issue="CSV-311" dev="ggregory" due-to="Christian Feuersaenger, Gary Gregory">OutOfMemory for very long rows despite using column value of type Reader.</action>
54+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Use try-with-resources to manage JDBC Clob in CSVPrinter.printRecords(ResultSet).</action>
5455
<!-- UPDATE -->
5556
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-io:commons-io: from 2.11.0 to 2.15.1.</action>
5657
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump commons-parent from 57 to 67.</action>

src/main/java/org/apache/commons/csv/CSVPrinter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.Closeable;
2525
import java.io.Flushable;
2626
import java.io.IOException;
27+
import java.io.Reader;
2728
import java.sql.Clob;
2829
import java.sql.ResultSet;
2930
import java.sql.SQLException;
@@ -414,8 +415,13 @@ public void printRecords(final ResultSet resultSet) throws SQLException, IOExcep
414415
while (resultSet.next()) {
415416
for (int i = 1; i <= columnCount; i++) {
416417
final Object object = resultSet.getObject(i);
417-
// TODO Who manages the Clob? The JDBC driver or must we close it? Is it driver-dependent?
418-
print(object instanceof Clob ? ((Clob) object).getCharacterStream() : object);
418+
if (object instanceof Clob) {
419+
try (Reader reader = ((Clob) object).getCharacterStream()) {
420+
print(reader);
421+
}
422+
} else {
423+
print(object);
424+
}
419425
}
420426
println();
421427
}

0 commit comments

Comments
 (0)