Skip to content

Commit b5dbc45

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 3a82678 commit b5dbc45

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

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

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

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

834835
@Override
835836
protected void writeInternal(final LogEvent event) {
836-
StringReader reader = null;
837+
// Don't close StringReaders because of (1) batching, (2) resources are not allocated, and (3) they'll be GC'd
838+
// away.
839+
// See https://github.com/apache/logging-log4j2/issues/3127 where closing StringReaders too soon can cause
840+
// problems.
837841
try {
838842
if (!this.isRunning() || isClosed(this.connection) || isClosed(this.statement)) {
839843
throw new AppenderLoggingException(
@@ -879,7 +883,7 @@ protected void writeInternal(final LogEvent event) {
879883
if (column.isEventTimestamp()) {
880884
this.statement.setTimestamp(j++, new Timestamp(event.getTimeMillis()));
881885
} else if (column.isClob()) {
882-
reader = new StringReader(column.getLayout().toSerializable(event));
886+
final Reader reader = new StringReader(column.getLayout().toSerializable(event));
883887
if (column.isUnicode()) {
884888
this.statement.setNClob(j++, reader);
885889
} else {
@@ -929,7 +933,6 @@ protected void writeInternal(final LogEvent event) {
929933
} catch (final SQLException e) {
930934
// Ignore
931935
}
932-
Closer.closeSilently(reader);
933936
}
934937
}
935938

0 commit comments

Comments
 (0)