如何从jdbcTemaplate获取连接池?

问题描述 投票:0回答:1

好吧,我已经将

jdbcTemplate
融入到了某堂课中:

@Service
public MyService {

    @Autowired
    private JdbcTemplate jdbcTemplate;
   
}

但是如何从中获取连接池实现呢?我看到

getDataSource()
方法,但是
DataSource
没有关于连接池的内容:(

java spring spring-boot spring-jdbc
1个回答
0
投票

例如,如果您的依赖项管理器中有此依赖项

<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
</dependency>

DataSource
接口的实现将是
HikariDataSource
,这是Spring将用于构造
JdbcTemplate
的实例。

现在,当您在代码中调用

jdbcTemplate.getDataSource().getConnection()
HikariDataSource
从池中获取连接。

© www.soinside.com 2019 - 2024. All rights reserved.