简体   繁体   English

使用Spring JdbcTemplate

[英]using Spring JdbcTemplate

if i create a new instance of JdbcTemplate like so; 如果我像这样创建一个新的JdbcTemplate实例;

JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource()); JdbcTemplate jdbcTemplate =新的JdbcTemplate(getDataSource());

by passing the datasource as a param (the datasource retrieves a connection from server connection pool) am i required to close the connection when im finished with it? 通过将数据源作为参数传递(数据源从服务器连接池中检索连接),我需要在连接完成后关闭连接吗?

In other words, if i have a pool of connections will the previous code cause my application to create a new connection each time a request executes the code 换句话说,如果我有一个连接池,那么每次请求执行该代码时,先前的代码都会使我的应用程序创建一个新的连接

No. That's the whole deal. 不,这就是全部。 Use the JdbcTemplate and it will manage the ressources (Connection, PreparedStatement, ResultSet). 使用JdbcTemplate,它将管理资源(连接,PreparedStatement,ResultSet)。 It is an implementation of the template method design pattern . 它是模板方法设计模式的一种实现。

Javadoc : Javadoc

It simplifies the use of JDBC and helps to avoid common errors. 它简化了JDBC的使用并有助于避免常见错误。 It executes core JDBC workflow, leaving application code to provide SQL and extract results. 它执行核心的JDBC工作流程,留下应用程序代码以提供SQL并提取结果。

我所做的是扩展JdbcTemplate并重写exe​​cute方法以使用连接池(特殊情况)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM