JSF 应用程序创建过多数据库连接

发布于 2024-10-19 11:26:51 字数 882 浏览 2 评论 0原文

我使用 MYSQL 5.1 db 和 Tomcat 7 构建了一个(相对)简单的 JSF 应用程序。一切正常,没有任何问题,但有一个好奇心......

使用 MySQL 工作台,我可以看到这一点,在一段时间内,我建立了一个不断增加的从未释放的数据库连接列表。每个都具有以下格式:

[id] [user] [host] [dbname] [command] [time] [State] [info]

状态始终为空白,命令始终为“睡眠”。

在打开和关闭连接、语句时,我遵循最佳实践,据我所知,我的 ManagedBeans 都没有与数据源有任何持久连接,一旦我在页面之间切换,所有这些都应该超出范围。

我正在努力寻找答案,部分原因是我不太确定该去哪里寻找。关于要审查/排除故障的任何建议,是否有一些我不知道的常见 JSF/DBCP 陷阱?

这可能有用:

<Resource 
    name="jdbc/TrackerDB" 
    auth="Container" 
    type="javax.sql.DataSource"
    maxActive="20" 
    maxIdle="10" 
    maxWait="100"
    username="xxxx" 
    password="xxxx" 
    removeAbandoned="true"
    removeAbandonedTimeout="300"
    logAbandoned="true"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/tracker"
/>

I've built a (relatively)simple JSF application using a MYSQL 5.1 db and Tomcat 7. Things are working in the sense that nothing blows up, but there is a curiosity...

Using the MySQL workbench, I can see that, over a period of time, I build up an ever increasing list of DB connections that are never released. Each one has the format:

[id] [user] [host] [dbname] [command] [time] [State] [info]

State is always blank and Command is always 'sleep'.

I'm following best practices when opening and closing connections, statements, and as far as I can tell, none of my ManagedBeans has any persistent connection to the dataSource, all of them should fall out of scope anyway as soon as I switch between pages.

I'm struggling to find answers, and that's partly because I'm not quite sure where to look. Any suggestions on what to review/troubleshoot, are there some common JSF/DBCP pitfalls I'm not aware of ?

This might be useful:

<Resource 
    name="jdbc/TrackerDB" 
    auth="Container" 
    type="javax.sql.DataSource"
    maxActive="20" 
    maxIdle="10" 
    maxWait="100"
    username="xxxx" 
    password="xxxx" 
    removeAbandoned="true"
    removeAbandonedTimeout="300"
    logAbandoned="true"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/tracker"
/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慕巷 2024-10-26 11:26:51

您使用什么样的持久性框架?

如果是 JPA,您应该每次 close() EntityManager(超出范围还不够好)!

如果处于休眠状态,则每次都应该 close() 会话。

如果是 JDBC,则每次都应该 close() 语句。

关闭此类资源通常在(try 块的)finally 子句中完成。

PS 你没有提到关闭结果集。我想你也关闭了那些?

What kind of persistence framework are you using?

If it's JPA you should close() the EntityManager each time (falling out of scope isn't good enough)!

If it's hibernate you should close() the Session each time.

If it's JDBC you should close() the Statement each time.

Closing such a resource is usually done in a finally clause (of the try block).

P.S. You don't mention closing ResultSets. I assume you close those too?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文