Search code examples
javajdbclog4j2

Does the log4j2 JDBC Appender close the connection by itself?


I've been finding that the log4j2 jdbc appender is really slow every single time an error is being pushed to the database. I think the jdbc appender is closing the connection every single time and then making a new connection. If this is the case, is there some way I can have the appender stop closing the connection everytime? I have the connection stored in the database connection class that I made and I close the connection before the program terminates.


Solution

  • The documentation says that the DataSource that you use for the connector should use a connection pool:

    It can be configured to obtain JDBC connections using a JNDI DataSource or a custom factory method.

    ...

    Whichever approach you take, it must be backed by a connection pool. Otherwise, logging performance will suffer greatly.

    So it is up to your implementation, that you didn't share here. It is not log4j2 responsibility to implement a JDBC connection pool.

    It would not be a good idea to try to implement a custom database connection pool. Use one of the existing libraries for that.