java jdbcTemplate执行查询后如何在Spring Boot中关闭连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40913235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to close the connection in Spring Boot after jdbcTemplate executes the query?
提问by upriser5
In Spring Boot, does jdbcTemplate not close the connection automatically once after the it executes the query? In this case, I am executing a query using jdbcTemplate(where it connects to teradata) but the session is not closing after the query is executed. How can I close the session?
在 Spring Boot 中,jdbcTemplate 在执行查询后不会自动关闭一次连接吗?在这种情况下,我正在使用 jdbcTemplate(它连接到 teradata 的位置)执行查询,但在执行查询后会话并未关闭。如何关闭会话?
This is my dao file -
这是我的 dao 文件 -
@Component
public class DDLReviewDao {
@Autowired
@Qualifier("devbJdbc")
private JdbcTemplate jdbcTemplate;
public static final Logger logger = LogManager.getLogger(DDLReviewDao.class);
public List<DDLObject> getDDLReviewData(DDLQuery ddlQuery) {
String selectSql = MacroGenerator.generateMacro(ddlQuery);
List<DDLObject> ddlObject = jdbcTemplate.query(selectSql, new DDLMapper());
logger.info(ddlObject);
return ddlObject;
}
}
回答by Maciej Dobrowolski
JdbcTemplate
gets its connections from javax.sql.DataSource
implementation - which is passed to its constructor link.
JdbcTemplate
从javax.sql.DataSource
实现获取它的连接——它被传递到它的构造函数链接。
The DataSource
s can be basic(creates Connection
object for each request) or pooling(has pool of connections and just 'borrows' one for given request's use).
该DataSource
可以均是基本的(创建Connection
为每个请求对象)或池(具有连接池,只是借来一用一个给定的请求的使用)。
So, it appears that the connection is not closing because you have passed some pooling datasource to JdbcTemplate
named devbJdbc
. If you realy want to close every connection opened to do the JdbcTemplate
job, you can use a basic DataSource
implementation: org.springframework.jdbc.datasource.SimpleDriverDataSource
just like that:
因此,连接似乎没有关闭,因为您已将一些池数据源传递给JdbcTemplate
named devbJdbc
。如果你真的想关闭每个打开的连接来完成这项JdbcTemplate
工作,你可以使用一个基本的DataSource
实现:org.springframework.jdbc.datasource.SimpleDriverDataSource
就像这样:
@Configuration
class DevbConfig {
@Bean(name = "devbDataSource")
DataSource devbDataSource() {
try {
return new SimpleDriverDataSource(DriverManager.getDriver("jdbc:..."), "jdbc:...", "username", "password");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Bean(name = "devbJdbc")
JdbcTemplate devbJdbc(@Qualifier("devbDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
回答by Kwinten Van de Broeck
According to the spring boot docs you can allocate a maximum number of connections to a spring pool like so:
根据 spring boot docs,您可以像这样为 spring 池分配最大数量的连接:
spring.datasource.tomcat.max-active=50
This will obviously only work with the embedded webserver. If you are deploying it to something like a Jboss you'll have to configure that property in the Server config file.
这显然只适用于嵌入式网络服务器。如果您将它部署到 Jboss 之类的东西,您必须在服务器配置文件中配置该属性。
回答by ootero
In Spring Boot, does jdbcTemplate not close the connection automatically once after the it executes the query?
在 Spring Boot 中,jdbcTemplate 在执行查询后不会自动关闭一次连接吗?
Should it close the connection or return it to the connection pool (in case the DataSource is pooled)?
它应该关闭连接还是将其返回到连接池(以防 DataSource 被池化)?
If you read the source code at http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-jdbc/4.1.7.RELEASE/org/springframework/jdbc/core/JdbcTemplate.java#JdbcTemplate.execute%28org.springframework.jdbc.core.StatementCallback%29it boils down to:
如果您在http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-jdbc/4.1.7.RELEASE/org/springframework/jdbc/core/JdbcTemplate.java阅读源代码#JdbcTemplate.execute%28org.springframework.jdbc.core.StatementCallback%29归结为:
public static void doReleaseConnection(Connection con, DataSource dataSource) throws SQLException {
if (con == null) {
return;
}
if (dataSource != null) {
ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
if (conHolder != null && connectionEquals(conHolder, con)) {
// It's the transactional Connection: Don't close it.
conHolder.released();
return;
}
}
logger.debug("Returning JDBC Connection to DataSource");
doCloseConnection(con, dataSource);
}
and
和
public static void doCloseConnection(Connection con, DataSource dataSource) throws SQLException {
if (!(dataSource instanceof SmartDataSource) || ((SmartDataSource) dataSource).shouldClose(con)) {
con.close();
}
}
Most-likely, if the DataSource instance is pooled, connections are release back for reuse and not closed.
最有可能的是,如果 DataSource 实例被池化,连接将被释放以供重用而不是关闭。