单纯从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包的作者比较有良心,还是不要偷懒的好。
还有借口中说, close是立即关闭, 你直接关闭connection, 就像你只管大门, 不穿内裤一样。
希望楼主认真考虑, 要不要这么做。
楼主还是都关闭了吧,先ResultSet,在Statement,最后con
* 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包的作者比较有良心,还是不要偷懒的好。
很明显要关闭是因为数据库链接是有连接数上限的,如果用完不关闭,当连接数用完后面的人就连接不上了我说的是直接关闭数据库的连接,但不关闭结果集和Statement
我认为一般是会关闭的,也完全可以做到关闭