|
1 | 1 | package ir.bigz.springbootreal.configuration; |
2 | 2 |
|
| 3 | +import com.zaxxer.hikari.HikariConfig; |
3 | 4 | import com.zaxxer.hikari.HikariDataSource; |
4 | 5 | import org.springframework.beans.factory.annotation.Autowired; |
5 | 6 | import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; |
@@ -31,24 +32,39 @@ public DataSourceProperties dataSourceProperties(){ |
31 | 32 |
|
32 | 33 |
|
33 | 34 | @Bean |
34 | | - public HikariDataSource dataSource(){ |
| 35 | + public DataSource dataSource(){ |
35 | 36 | DataSourceProperties dataSourceProperties = dataSourceProperties(); |
36 | | - final HikariDataSource dataSource = new HikariDataSource(); |
37 | | - dataSource.setDriverClassName(dataSourceProperties.getDriverClassName()); |
38 | | - dataSource.setJdbcUrl(dataSourceProperties.getUrl()); |
39 | | - dataSource.setUsername(dataSourceProperties.getUsername()); |
40 | | - dataSource.setPassword(dataSourceProperties.getPassword()); |
41 | | - dataSource.setConnectionTimeout(Integer.parseInt(env.getProperty("spring.datasource.hikari.connectionTimeout"))); |
42 | | - dataSource.setMaximumPoolSize(Integer.parseInt(env.getProperty("spring.datasource.hikari.maximumPoolSize"))); |
43 | | - return dataSource; |
44 | | -// return DataSourceBuilder.create() |
45 | | -// .driverClassName(dataSourceProperties.getDriverClassName()) |
46 | | -// .url(dataSourceProperties.getUrl()) |
47 | | -// .username(dataSourceProperties.getUsername()) |
48 | | -// .password(dataSourceProperties.getPassword()) |
49 | | -// .build(); |
| 37 | + // final HikariDataSource dataSource = new HikariDataSource(); |
| 38 | + // dataSource.setDriverClassName(dataSourceProperties.getDriverClassName()); |
| 39 | + // dataSource.setJdbcUrl(dataSourceProperties.getUrl()); |
| 40 | + // dataSource.setUsername(dataSourceProperties.getUsername()); |
| 41 | + // dataSource.setPassword(dataSourceProperties.getPassword()); |
| 42 | + // dataSource.setConnectionTimeout(Integer.parseInt(env.getProperty("spring.datasource.hikari.connectionTimeout"))); |
| 43 | + // dataSource.setMaximumPoolSize(Integer.parseInt(env.getProperty("spring.datasource.hikari.maximumPoolSize"))); |
| 44 | + // return dataSource; |
| 45 | + return connectionPoolDataSource(DataSourceBuilder.create() |
| 46 | + .driverClassName(dataSourceProperties.getDriverClassName()) |
| 47 | + .url(dataSourceProperties.getUrl()) |
| 48 | + .username(dataSourceProperties.getUsername()) |
| 49 | + .password(dataSourceProperties.getPassword()) |
| 50 | + .build()); |
50 | 51 | } |
51 | 52 |
|
| 53 | + protected HikariConfig hikariConfig( |
| 54 | + DataSource dataSource) { |
| 55 | + HikariConfig hikariConfig = new HikariConfig(); |
| 56 | + int cpuCores = Runtime.getRuntime().availableProcessors(); |
| 57 | + hikariConfig.setMaximumPoolSize(cpuCores * 4); |
| 58 | + hikariConfig.setDataSource(dataSource); |
| 59 | + // hikariConfig.setAutoCommit(false); |
| 60 | + return hikariConfig; |
| 61 | +} |
| 62 | + |
| 63 | +protected HikariDataSource connectionPoolDataSource( |
| 64 | + DataSource dataSource) { |
| 65 | + return new HikariDataSource(hikariConfig(dataSource)); |
| 66 | +} |
| 67 | + |
52 | 68 | @Bean |
53 | 69 | public LocalContainerEntityManagerFactoryBean entityManagerFactory(){ |
54 | 70 | LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); |
|
0 commit comments