开发者

Reconnect to DB within log4j

If I have a JDBCAppender configured to throw log messages to MySQL and, while my system is up I restart the 开发者_高级运维database is it reconnect to DB?


I have had this use case occur over this past weekend. My database is hosted by Amazon AWS. It rolled over my log database and all of the instances logging to that database via the Log4j JDBC Appender stopped logging. I bounced one of the apps and it resumed logging.

So the answer to this question, through experience, appears to be No.

If the database goes down and comes back online, the JDBC appender does not reconnect automatically.

edit

JDBCAppender getConnection might be overridden to fix.


JDBCAppender in log4j 1.2.15 has following code

protected Connection getConnection() throws SQLException {
      if (!DriverManager.getDrivers().hasMoreElements())
         setDriver("sun.jdbc.odbc.JdbcOdbcDriver");

      if (connection == null) {
        connection = DriverManager.getConnection(databaseURL, databaseUser,
                    databasePassword);
      }

      return connection;
  }

so if connection is not null, but is broken (needs reconnect) log4j will return broken connection to its logic, and executing statement which does logging to db will fail.

Not a workaround, but a proper solution is to replace log4j with logback: see related answer: Log to a database using log4j

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜