简体   繁体   中英

How to handle log4j2 JDBCAppender exceptions?

I'm using Log4j 2 in my project to write son logs to a database table. These events are identified based on certain MDC key/value pair. My concern is about the unchecked LoggingException this appender can throw from its append method. These database logs are generated inside asynchronous, transactional process and I don't want the main process gets affected by a potential exception raised by the appender. How should I take care of it? Should I move the configuration based appender to a custom appender? Is there any guarantee that the appender won't rollback my business process in case of log event append error?

thks in advance

Assuming you are using JDBCAppender, I depends on two things:

  • Are you sharing the database connection of your application with log4j? By default, you are not. In fact, you would have to try very hard to accomplish this. So, failures on the logging connection will not impact transactions on the business DB connection.
  • If you are sharing a JDBC connection, then it depends on the autoCommit setting of that connection. If autoCommit is true, logging errors will not affect business transactions even on the same connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM