原先背景与做法
我们在使用资源的时候,必须关闭资源,比如使用jdbc连接或者inputStream的时候,必须在finally中将资源关闭。
比如获取Mybatis框架的SqlSession的时候
package com.dao; import com.lingaolu.dao.DeptDao; import com.lingaolu.pojo.Dept; import com.lingaolu.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; /** * @author 林高禄 * @create 2020-10-12-9:56 */ public class DeptDaoTest { @Test public void getDeptList(){ SqlSession sqlSession = null; try{ sqlSession = MybatisUtils.getSqlSession(); DeptDao mapper = sqlSession.getMapper(DeptDao.class); List<Dept> deptList = mapper.getDeptList(); deptList.forEach(System.out::println); }catch (Exception e){ }finally { if(null != sqlSession){ sqlSession.close(); } } } }
但是这样写显很麻烦,并且有的时候我们会忘记关闭资源。那么有没有更好的方法呢?
改进
我使用的Java版本是1.8,这里也有了提示
扫描二维码关注公众号,回复: 12008678 查看本文章
从jdk1.7开始, Java 7增强了try语句的功能——它允许在try关键字后跟一对圆括号,圆括号可以声明,初始化一个或多个资源,此处的资源指得是那些必须在程序结束时必须关闭的资源(比如数据库连接,网络连接等),try语句在该语句结束时自动关闭这些资源。这种称为try-with-resources语句。
再看Mybatis的官网,也使用了这种做法
所以我们就把代码改为这种方式
package com.dao; import com.lingaolu.dao.DeptDao; import com.lingaolu.pojo.Dept; import com.lingaolu.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; /** * @author 林高禄 * @create 2020-10-12-9:56 */ public class DeptDaoTest { @Test public void getDeptList(){ try(SqlSession sqlSession = MybatisUtils.getSqlSession()){ DeptDao mapper = sqlSession.getMapper(DeptDao.class); List<Dept> deptList = mapper.getDeptList(); deptList.forEach(System.out::println); } } }
像这样的话,执行完sqlsession会自动关闭,不用我们在finally中关闭,也再也不用担心忘记关闭了。
说明
那么为什么这样可以自动关闭资源呢?是不是所有的资源都可以这样关闭呢?
实际上只要这些资源实现类实现了Closeable或AutoCloseable接口,就可以自动关闭。比如Sqlsession它就是extends Closeable, Closeable extends AutoCloseable。
我们看SqlSession是实现了Closeable接口
几乎所有的资源都可以用这种方式实现自动关闭资源,比如
OutputStream,BufferedReader,PrintStream,InputStream等,都可以。据说到目前为止,只有JavaMail Transport对象不能利用这种方式实现自动关闭。
注意:
- 如果try()里面有两个资源,用逗号分开,资源的close方法的调用顺序与它们的创建顺序相反。
- 带有资源的try语句可以像一般的try语句一样具有catch和finally块。在try-with-resources语句中,任何catch或finally块都是在声明的资源被关闭后才会执行的。