问题描述
使用DatasSourceBuilder创建数据源
@Bean
DriverManagerDataSource dataSource() throws sqlException {
DriverManagerDataSource dataSource = DriverManagerDataSource();
dataSource.setUrl("");
...
..
return dataSource;
}
使用DriverManagerDataSource创建数据源
@Bean
public JdbcTemplate jdbcTemplate()
{
return new jdbcTemplate(dataSource());
}
我正在使用上述两种方法创建jdbc
void m1()
{
simpleJdbc = new simpleJdbc(jdbcTemplate);
simpleJdbc.execute(procedure)
}
我正在像下面那样使用jdbcTemplate
@org.springframework.boot.autoconfigure.SpringBootApplication
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class,args);
}
所以我的问题是,如果我重复调用m1()50次,那么在这两种情况下都会创建多少个连接,即DriverManagerDataSource和DatasSourceBuilder
解决方法
简单的答案是
-
DriverManagerDataSource
->调用方法时的连接数量 -
DataSourceBuilder
->由max poolsize属性指定的连接数。
但是真正的答案取决于它。如果您在单个事务中调用m1
并调用50次,则它将以单个连接的任一方式打开。打开的连接绑定到事务并重新使用。
话虽如此,您不应该将DriverManagerDataSource
用于可用于生产的应用程序。它不是连接池,会在需要时打开连接,打开连接速度很慢,并且会创建无数个连接(取决于需要),因此如果您有100个需要连接的请求,它将打开100个连接(可能充斥您的数据库)。
DriverManagerDataSource
仅用于测试和演示,而不能用于生产应用。