Skip to content

Commit 44b5e6c

Browse files
committed
org.apache.commons.csv.CSVPrinter.printRecords(ResultSet) now writes one
record at a time using a lock
1 parent 138a0b5 commit 44b5e6c

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
<version>${commons.jmh.version}</version>
6969
<scope>test</scope>
7070
</dependency>
71+
<dependency>
72+
<groupId>com.github.spotbugs</groupId>
73+
<artifactId>spotbugs-annotations</artifactId>
74+
<version>${commons.spotbugs.impl.version}</version>
75+
<optional>true</optional>
76+
</dependency>
7177
</dependencies>
7278
<scm>
7379
<connection>scm:git:http://gitbox.apache.org/repos/asf/commons-csv.git</connection>

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<!-- FIX -->
4545
<action type="fix" issue="CSV-318" dev="ggregory" due-to="Joseph Shraibman, Gary Gregory">CSVPrinter.printRecord(Stream) hangs if given a parallel stream.</action>
4646
<action type="fix" issue="CSV-318" dev="ggregory" due-to="Joseph Shraibman, Gary Gregory">CSVPrinter now uses an internal lock instead of synchronized methods.</action>
47+
<action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.csv.CSVPrinter.printRecords(ResultSet) now writes one record at a time using a lock.</action>
4748
<!-- ADD -->
4849
<!-- UPDATE -->
4950
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-io:commons-io from 2.18.0 to 2.19.0.</action>

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141
import org.apache.commons.io.function.IOStream;
4242

43+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
44+
4345
/**
4446
* Prints values in a {@link CSVFormat CSV format}.
4547
*
@@ -151,14 +153,10 @@ public void close(final boolean flush) throws IOException {
151153
* @throws IOException
152154
* If an I/O error occurs
153155
*/
156+
@SuppressFBWarnings(value = "AT_NONATOMIC_OPERATIONS_ON_SHARED_VARIABLE", justification = "https://github.com/spotbugs/spotbugs/issues/3428")
154157
private void endOfRecord() throws IOException {
155-
lock.lock();
156-
try {
157-
println();
158-
recordCount++;
159-
} finally {
160-
lock.unlock();
161-
}
158+
println();
159+
recordCount++;
162160
}
163161

164162
/**
@@ -494,21 +492,26 @@ public void printRecords(final Object... values) throws IOException {
494492
public void printRecords(final ResultSet resultSet) throws SQLException, IOException {
495493
final int columnCount = resultSet.getMetaData().getColumnCount();
496494
while (resultSet.next() && format.useRow(resultSet.getRow())) {
497-
for (int i = 1; i <= columnCount; i++) {
498-
final Object object = resultSet.getObject(i);
499-
if (object instanceof Clob) {
500-
try (Reader reader = ((Clob) object).getCharacterStream()) {
501-
print(reader);
495+
lock.lock();
496+
try {
497+
for (int i = 1; i <= columnCount; i++) {
498+
final Object object = resultSet.getObject(i);
499+
if (object instanceof Clob) {
500+
try (Reader reader = ((Clob) object).getCharacterStream()) {
501+
print(reader);
502+
}
503+
} else if (object instanceof Blob) {
504+
try (InputStream inputStream = ((Blob) object).getBinaryStream()) {
505+
print(inputStream);
506+
}
507+
} else {
508+
print(object);
502509
}
503-
} else if (object instanceof Blob) {
504-
try (InputStream inputStream = ((Blob) object).getBinaryStream()) {
505-
print(inputStream);
506-
}
507-
} else {
508-
print(object);
509510
}
511+
endOfRecord();
512+
} finally {
513+
lock.unlock();
510514
}
511-
endOfRecord();
512515
}
513516
}
514517

0 commit comments

Comments
 (0)