log4j2 JDBC Appender会自动关闭连接吗?

huangapple go评论116阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月29日 00:48:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定