英文:
Does the log4j2 JDBC Appender close the connection by itself?
问题
我发现log4j2的jdbc appender每次将错误推送到数据库时都非常慢。我认为jdbc appender每次都关闭连接然后重新建立连接。如果是这样的情况,是否有办法让appender不要每次都关闭连接?我已经将连接存储在我创建的数据库连接类中,并在程序终止之前关闭连接。
英文:
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.
答案1
得分: 3
1 中的文档 表示,您用于连接器的数据源应该使用连接池:
> 可以配置为使用 JNDI 数据源或自定义工厂方法获取 JDBC 连接。
>
> ...
>
> 无论采用哪种方法,都必须由连接池支持。否则,日志性能将严重受损。
因此,这取决于您的实现,您没有在此分享。Log4j2 不负责实现 JDBC 连接池。
尝试实现自定义数据库连接池不是一个好主意。使用现有的库之一。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论