https://issues.apache.org/bugzilla/show_bug.cgi?id=51597
Bug #: 51597
Summary: JDBCAppender not closed due to SQL Exception while
executing an SQL
Product: Log4j
Version: unspecified
Platform: PC
OS/Version: All
Status: NEW
Severity: critical
Priority: P2
Component: Appender
AssignedTo: [email protected]
ReportedBy: [email protected]
Classification: Unclassified
Created attachment 27339
--> https://issues.apache.org/bugzilla/attachment.cgi?id=27339
Log4j-JDBC-Appender-Issue.doc
In JDBCAppender.java there is method execute(String sql). This method is used
to insert the SQL into DB, but in case if anything wrong happen while
inserting, then it throws one SQLException and closing only SQL statement but
connection is not closed, and so Appender thread is still alive.
Now as the execute method is called via flushBuffer() method, this method is
calling execute and then event is added to removes list, but if execute method
had thrown the SQLException then the event is not added to removes list and
removes list is growing continuously. And that is in the buffer tile next call
of System.gc(), so this will make the next threads to go into hung state.
Code Snippet having issue is attached.
A solution to this is to have following implementation:
protected void execute(String sql) throws SQLException {
Connection con = null;
Statement stmt = null;
try {
con = getConnection();
stmt = con.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e) {
throw e;
}finally{
try{
if(null != stmt)
stmt.close();
closeConnection(con);
} catch (SQLException e){
}
}
}
public void flushBuffer() {
//Do the actual logging
removes.ensureCapacity(buffer.size());
for (Iterator i = buffer.iterator(); i.hasNext();) {
try {
LoggingEvent logEvent = (LoggingEvent)i.next();
String sql = getLogStatement(logEvent);
removes.add(logEvent);
execute(sql);
}catch (SQLException e) {
errorHandler.error("Failed to excute sql", e,
ErrorCode.FLUSH_FAILURE);
}
}
// remove from the buffer any events that were reported
buffer.removeAll(removes);
// clear the buffer of reported events
removes.clear();
}
--
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]