有没有永远不关闭JDBC连接的原因吗? [英] Is there a reason for never closing a JDBC connection?

查看:307
本文介绍了有没有永远不关闭JDBC连接的原因吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从上一个在系统上工作的开发人员那里读取代码,他从未关闭与数据库的任何连接.他只关闭PreparedStatementResultSet连接,但不关闭Connection.

I'm reading a code from the last developer that worked on the system and he never closes any connections with the database. He only closes PreparedStatement and ResultSet connections but never the Connection.

系统不使用连接池.

有什么理由不关闭所有内容(ConnectionPreparedStatementResultSet)吗?

Is there any reason not to close everything (Connection, PreparedStatement and ResultSet)?

推荐答案

没有充分的理由,这将导致应用程序非常脆弱.如果出现网络问题或数据库在短时间内没有响应,则数据库连接很容易过时,并且依靠单个现有连接,没有重新启动应用程序就无法恢复.

There is no good reason for this, it will result in a very brittle application. It's easy for a database connection to go stale if there's a network problem or if the database doesn't respond for a little while, and with the reliance on a single existing connection there's no recovering without restarting the application.

对此还有其他不利之处.例如,通常连接是同步的,因此,如果以此方式构建具有多个并发用户的Web应用程序,它将限制应用程序的并发性.但是,您的应用程序无法从瞬时问题中恢复就足以使此问题值得解决.

There are other bad points to this. For instance, typically connections are synchronized so if a web application with multiple concurrent users was built this way it would limit the concurrency of the application. But the inability of your application to recover from transient problems is enough to make this worth fixing.

这篇关于有没有永远不关闭JDBC连接的原因吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆