如果用户离开 JSF 中的页面,数据库连接是否会关闭?
我正在使用 Java Server Faces (JSF) 创建一个基于 Web 的应用程序。我只是想知道如果数据库连接打开并且用户离开网页(关闭浏览器),JSF 是否会自动关闭此连接?
I'm creating a web based application using Java Server Faces (JSF). I'm just wondering if a database connection is open and the user leaves the web page (close the browser), does JSF automatically closes this connection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
用户离开页面不会影响数据库连接。用户请求页面,服务器响应。他们的交流就这样结束了。如果用户关闭浏览器或按回键,服务器不会收到此通知。
唯一可以关闭它的地方是在创建 HTTP 响应之前在 Java 代码中。
User leaving the page cannot have an effect on the database connection. A user requests a page, the server responds. That is the end of the their intercommunication. If the user closes the browser or presses back the server is not notified of this.
The only place you can close it is in your Java code before you create the HTTP response.
您应该在将数据发送回客户端后立即关闭数据库连接 - 在将其发送回客户端之前。最好在
finally
块中执行此操作。没有真正的理由让它保持打开状态,但这不仅会带来潜在的内存泄漏,而且还会带来很大的安全风险。You should close the database connection immediately after getting your data to send back to the client - BEFORE sending it back to the client. Best to do it in a
finally
block. There is no real reason to keep it open but it does not only introduce a potential memory leak but also a great security risk.如果您关闭数据库连接,数据库连接也会关闭,或者连接池(如果有)关闭它,或者它会被垃圾收集。
The database connection closes if you close it, or the connection pool if any closes it, or it gets garbage collected.