关于jdbc各种close的问题

「已注销」 2013-06-20 04:42:54
如果ResultSet,Statement不关,直接关Connection是不是默认也将ResultSet和Statement也一起close了?这样处理好不好?不好在哪里?
...全文
272 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
dracularking 2013-06-20
  • 打赏
  • 举报
回复
是否会连带关闭完全取决于Connection的实现类如何操作 我认为一般是会关闭的,也完全可以做到关闭
「已注销」 2013-06-20
  • 打赏
  • 举报
回复
引用 1 楼 haozkui 的回复:
有深度 搜 开发驿站 java 试试 你的选择。 前辈说要关闭, 你说你关不关, 不关闭,那close方法有毛用。 还有借口中说, close是立即关闭, 你直接关闭connection, 就像你只管大门, 不穿内裤一样。 希望楼主认真考虑, 要不要这么做。
还是不是很理解,ResultSet和Statement的连接不是基于Connection的连接么?
「已注销」 2013-06-20
  • 打赏
  • 举报
回复
引用 4 楼 shadowsick 的回复:
[quote=引用 3 楼 beichui 的回复:] 单纯从JAVA API的限制来说,它并没规定Connection的close方法必须关闭附属对象。

    /**
     * Releases this <code>Connection</code> object's database and JDBC resources
     * immediately instead of waiting for them to be automatically released.
     * <P>
     * Calling the method <code>close</code> on a <code>Connection</code>
     * object that is already closed is a no-op.
     * <P>
     * It is <b>strongly recommended</b> that an application explicitly 
     * commits or rolls back an active transaction prior to calling the 
     * <code>close</code> method.  If the <code>close</code> method is called
     * and there is an active transaction, the results are implementation-defined.
     * <P>
     * 
     * @exception SQLException SQLException if a database access error occurs 
     */
根据上面的API,事务在连接关闭时是否回滚都完全取决于实现类,至于其他对象更是没提。 个人观点,你这样做完全是寄希望于数据库驱动JAR包的作者比较有良心,还是不要偷懒的好。
很明显要关闭是因为数据库链接是有连接数上限的,如果用完不关闭,当连接数用完后面的人就连接不上了[/quote]我说的是直接关闭数据库的连接,但不关闭结果集和Statement
小丑哥_V5 2013-06-20
  • 打赏
  • 举报
回复
引用 3 楼 beichui 的回复:
单纯从JAVA API的限制来说,它并没规定Connection的close方法必须关闭附属对象。

    /**
     * Releases this <code>Connection</code> object's database and JDBC resources
     * immediately instead of waiting for them to be automatically released.
     * <P>
     * Calling the method <code>close</code> on a <code>Connection</code>
     * object that is already closed is a no-op.
     * <P>
     * It is <b>strongly recommended</b> that an application explicitly 
     * commits or rolls back an active transaction prior to calling the 
     * <code>close</code> method.  If the <code>close</code> method is called
     * and there is an active transaction, the results are implementation-defined.
     * <P>
     * 
     * @exception SQLException SQLException if a database access error occurs 
     */
根据上面的API,事务在连接关闭时是否回滚都完全取决于实现类,至于其他对象更是没提。 个人观点,你这样做完全是寄希望于数据库驱动JAR包的作者比较有良心,还是不要偷懒的好。
很明显要关闭是因为数据库链接是有连接数上限的,如果用完不关闭,当连接数用完后面的人就连接不上了
北吹 2013-06-20
  • 打赏
  • 举报
回复
单纯从JAVA API的限制来说,它并没规定Connection的close方法必须关闭附属对象。

    /**
     * Releases this <code>Connection</code> object's database and JDBC resources
     * immediately instead of waiting for them to be automatically released.
     * <P>
     * Calling the method <code>close</code> on a <code>Connection</code>
     * object that is already closed is a no-op.
     * <P>
     * It is <b>strongly recommended</b> that an application explicitly 
     * commits or rolls back an active transaction prior to calling the 
     * <code>close</code> method.  If the <code>close</code> method is called
     * and there is an active transaction, the results are implementation-defined.
     * <P>
     * 
     * @exception SQLException SQLException if a database access error occurs 
     */
根据上面的API,事务在连接关闭时是否回滚都完全取决于实现类,至于其他对象更是没提。 个人观点,你这样做完全是寄希望于数据库驱动JAR包的作者比较有良心,还是不要偷懒的好。
傅小司 2013-06-20
  • 打赏
  • 举报
回复
关大门不穿内裤, 楼主还是都关闭了吧,先ResultSet,在Statement,最后con
haozkui 2013-06-20
  • 打赏
  • 举报
回复
有深度 搜 开发驿站 java 试试 你的选择。 前辈说要关闭, 你说你关不关, 不关闭,那close方法有毛用。 还有借口中说, close是立即关闭, 你直接关闭connection, 就像你只管大门, 不穿内裤一样。 希望楼主认真考虑, 要不要这么做。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧