到目前为止,每当我查询数据库时,我都会打开一个与数据库的新连接。如何实现一旦打开连接就可以重用该属性?
完成此操作后,请告诉我是否可以泄漏资源。
基本上你需要 JDBC 连接池,通常实现DataSource界面。看一下dbcp and c3p0。您的容器/服务器很可能已经提供了连接池的实现。
When you use a connection pool every time you open a connection you are actually taking one from the pool (or opening if pool is empty). When closing the connection, it is actually returned to the pool. The leak can only occur if you forget the latter. (or forget closing ResultSet
, Statement
...)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)