日常修改bug。。。
场景:database.properties中druid.minIdle=10,在下载资源时下载第11次页面直接跳出一大串错误信息。。。经查看源代码发现是sqlSession用了没关闭。
认知中原关闭资源方法:
public static void main(String[] args) {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(new File("demo"));
System.out.println(inputStream.read());
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
}
java7后使用try-with-resource语法:
public static void main(String[] args) {
try (FileInputStream inputStream = new FileInputStream(new File("demo"))) {
System.out.println(inputStream.read());
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
上述代码是不是比第一种少了很多,结构也易懂且减少错误的发生。