JDBCTemplate会自动关闭连接吗?

huangapple go评论81阅读模式
英文:

JDBCTemplate automatically closes connection?

问题

使用JDBCTemplate,即使我不使用try-with-resources(或者在catch块中抛出异常,假设出现了一些SQLException),连接是否会自动关闭?JDBCTemplate是否足够智能?

这个资源说:

>它会处理资源的创建和释放,比如创建和关闭连接对象等。所以,如果你忘记关闭连接,也不会导致任何问题。

因此,通过使用JDBCTemplate,是否可以简单地忘记关闭连接?

英文:

If I use JDBCTemplate, is the connection automatically closed even if i dont use try with resources (or incase I am throwing an exception in a catch block assuming some sqlexception occured) ? Is JDBCTemplate smart enough?

https://www.javatpoint.com/spring-JdbcTemplate-tutorial
This resource says:

>It takes care of creation and release of resources such as creating and closing of connection object etc. So it will not lead to any problem if you forget to close the connection.

So by usnig JDBCTemplate, one can simply forget about connection closing?

答案1

得分: 1

是的,你可以忘记关闭连接。

当你对教程中提到的内容有疑问时,你应该始终查阅文档。在在这里提问之前,你绝对应该这样做。

如果你查阅了文档,你会发现:

> 3.3.1. 使用 JdbcTemplate
>
> JdbcTemplate 是 JDBC 核心包中的核心类。它处理资源的创建和释放,这有助于你避免常见错误,比如忘记关闭连接。它执行核心 JDBC 工作流的基本任务(比如语句的创建和执行),将应用程序代码留给提供 SQL 并提取结果。

当然,文档还有很多内容,所以你真的应该点击链接并自行阅读。

这份文档写得很好,所以没有好的借口不去阅读它或者在怀疑时引用它。

英文:

Yes, you can forget about connection closing.

When you have questions about something mentioned in a tutorial, you should always check the documentation. You should definitely do that before asking here.

If you had checked the documentation, you'd have found:

> 3.3.1. Using JdbcTemplate
>
> JdbcTemplate is the central class in the JDBC core package. It handles the creation and release of resources, which helps you avoid common errors, such as forgetting to close the connection. It performs the basic tasks of the core JDBC workflow (such as statement creation and execution), leaving application code to provide SQL and extract results.

It goes on, of course, so you should really click the link and read it for yourself.

The documentation is well-written, so there is no good excuse not to read it and/or reference it when in doubt.

huangapple
  • 本文由 发表于 2020年9月15日 03:49:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63890968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定