Skip to content

Commit 1a21ed8

Browse files
committed
Don't close StringReaders because of (1) batching, (2) resources are not
allocated, and (3) they'll be GC'd away. See #3127 where closing StringReaders too soon can cause problems.
1 parent 82c707f commit 1a21ed8

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JdbcDatabaseManager.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.logging.log4j.core.appender.db.jdbc;
1818

1919
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20+
import java.io.Reader;
2021
import java.io.Serializable;
2122
import java.io.StringReader;
2223
import java.sql.Clob;
@@ -945,7 +946,10 @@ private Object truncate(final String nameKey, Object value) {
945946

946947
@Override
947948
protected void writeInternal(final LogEvent event, final Serializable serializable) {
948-
StringReader reader = null;
949+
// Don't close StringReaders because of (1) batching, (2) resources are not allocated, and (3) they'll be GC'd
950+
// away.
951+
// See https://github.com/apache/logging-log4j2/issues/3127 where closing StringReaders too soon can cause
952+
// problems.
949953
try {
950954
if (!this.isRunning() || isClosed(this.connection) || isClosed(this.statement)) {
951955
throw new AppenderLoggingException(
@@ -990,7 +994,7 @@ protected void writeInternal(final LogEvent event, final Serializable serializab
990994
if (column.isEventTimestamp()) {
991995
this.statement.setTimestamp(j++, new Timestamp(event.getTimeMillis()));
992996
} else if (column.isClob()) {
993-
reader = new StringReader(column.getLayout().toSerializable(event));
997+
final Reader reader = new StringReader(column.getLayout().toSerializable(event));
994998
if (column.isUnicode()) {
995999
this.statement.setNClob(j++, reader);
9961000
} else {
@@ -1040,7 +1044,6 @@ protected void writeInternal(final LogEvent event, final Serializable serializab
10401044
} catch (final SQLException e) {
10411045
// Ignore
10421046
}
1043-
Closer.closeSilently(reader);
10441047
}
10451048
}
10461049

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="changed">
6+
<issue id="3127" link="https://github.com/apache/logging-log4j2/issues/3127"/>
7+
<description format="asciidoc">Don't close StringReaders because of batching.</description>
8+
</entry>

0 commit comments

Comments
 (0)