我有 Java - Spring Boot - Hibernate - Postgres 应用程序。Hibernate 在每次请求时都会创建数据库连接,为什么?有可配置的吗?例如,一次会议持续 10 分钟?我的休眠配置:@Autowiredprivate Environment environment;@Beanpublic LocalSessionFactoryBean sessionFactory() { LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); sessionFactory.setPackagesToScan(new String[] { "monitoring" }); sessionFactory.setHibernateProperties(hibernateProperties()); return sessionFactory;}@Beanpublic DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.postgresql.Driver"); dataSource.setUrl(environment.getRequiredProperty("spring.datasource.url")); dataSource.setUsername(environment.getRequiredProperty("spring.datasource.username")); dataSource.setPassword(environment.getRequiredProperty("spring.datasource.password")); return dataSource;}private Properties hibernateProperties() { Properties properties = new Properties(); properties.put("hibernate.dialect", environment.getRequiredProperty("spring.jpa.properties.hibernate.dialect")); properties.put("hibernate.show_sql", environment.getRequiredProperty("spring.jpa.hibernate.show-sql")); properties.put("hibernate.format_sql", "false"); properties.put("hibernate.jdbc.lob.non_contextual_creation", "true"); return properties;}我收到的每个请求都包含以下日志:11:19:13.584 [http-nio-8080-exec-2] DEBUG o.s.j.d.DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:postgresql://localhost:5432/monit]为什么?我怎样才能改变它?
1 回答
绝地无双
TA贡献1946条经验 获得超4个赞
由于定义了数据源,因此需要手动配置连接池。来自Spring 官方文档:
如果您定义自己的 DataSource bean,则不会发生自动配置。
从 spring boot 2 开始,HikariCP
默认连接池嵌入了 spring boot starter (spring-boot-starter-jdbc
和spring-boot-starter-data-jpa
)。
您可以使用以下配置来配置最大池大小HikariCP
spring.datasource.hikari.maximum-pool-size= 10
添加回答
举报
0/150
提交
取消