package net.safedata.springboot.training.d02.s05.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * A simple {@link javax.sql.DataSource} configuration, which:
 * <ul>
 *     <li>configures the JPA repositories, using the {@link EnableJpaRepositories} annotation</li>
 *     <li>configures a custom {@link DataSource}, using the {@link HikariDataSource} class</li>
 * </ul>
 *
 * @author bogdan.solga
 */
@Configuration
@EnableJpaRepositories(basePackages = "net.safedata.springboot.training.d02.s05.repository")
@EnableTransactionManagement
public class CustomDataSourceConfig {

    private static final int AVAILABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();

    // as properties from YAML files cannot be wired using @Value, we need to use a RelaxedPropertyResolver
    // to read the config values
    private final RelaxedPropertyResolver propertyResolver;

    @Autowired
    public CustomDataSourceConfig(final Environment environment) {
        propertyResolver = new RelaxedPropertyResolver(environment, "spring.datasource.");
    }

    // if the @Bean is commented, the configured data-source will be wired
    @Bean
    public DataSource dataSource() {
        final HikariConfig hikariConfig = new HikariConfig();

        hikariConfig.setPoolName("spring-boot-demo-pool");
        hikariConfig.setMaximumPoolSize(AVAILABLE_PROCESSORS * 2);
        hikariConfig.setJdbcUrl(propertyResolver.getProperty("url"));
        hikariConfig.setUsername(propertyResolver.getProperty("username"));
        hikariConfig.setPassword(propertyResolver.getProperty("password"));

        return new HikariDataSource(hikariConfig);
    }
}

@@ -148,120 +148,101 @@
 org.
-springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.boot.bind.RelaxedPropertyResolver
+apache.tomcat.jdbc.pool.DataSource;%0Aimport org.springframework.beans.factory.annotation.Value
 ;%0Aim
@@ -385,26 +385,32 @@
 k.co
-re.env.Environment
+ntext.annotation.Primary
 ;%0Aim
@@ -568,38 +568,8 @@
 t;%0A%0A
-import javax.sql.DataSource;%0A%0A
 /**%0A
@@ -777,16 +777,26 @@
  %7B@link 
+javax.sql.
 DataSour
@@ -1167,384 +1167,304 @@
     
-// as properties from YAML files cannot be wired using @Value, we need to use a RelaxedPropertyResolver%0A    // to read the config values%0A    private final RelaxedPropertyResolver propertyResolver;%0A%0A    @Autowired%0A    public CustomDataS
+@Value(%22$%7Bspring.datasource.url%7D%22)%0A    private String url;%0A%0A    @Value(%22$%7Bspring.datasource.username%7D%22)%0A    private String userName;%0A%0A    @Value(%22$%7Bspring.datas
 ource
-Config(final Environment environment) %7B%0A        propertyResolver = new RelaxedPropertyResolver(environment, %22spring.datasource.%22);%0A    %7D
+.password%7D%22)%0A    private String password;%0A%0A    @Value(%22$%7Bspring.datasource.driver-class-name%7D%22)%0A    private String driverClassName;
 %0A%0A  
@@ -1536,16 +1536,29 @@
 e wired%0A
+    @Primary%0A
     @Bea
@@ -1570,16 +1570,26 @@
  public 
+javax.sql.
 DataSour
@@ -1595,18 +1595,28 @@
 rce 
-dataSource
+hikariConnectionPool
 () %7B
@@ -1717,24 +1717,25 @@
 me(%22
-spring-boot-demo
+hikari-connection
 -poo
@@ -1834,51 +1834,158 @@
 .set
-JdbcUrl(propertyResolver.getProperty(%22
+ConnectionTimeout(30000);%0A        hikariConfig.setIdleTimeout(60000);%0A        hikariConfig.setMaxLifetime(120000);%0A        hikariConfig.setJdbcUrl(
 url
-%22)
 );%0A 
@@ -2020,59 +2020,130 @@
 ame(
-propertyResolver.getProperty(%22usern
+userName);%0A        hikariConfig.setPassword(password);%0A        hikariConfig.setDriverClassName(driverClassN
 ame
-%22)
 );%0A
+%0A
         
 hika
@@ -2142,71 +2142,349 @@
     
-hikariConfig.setPassword(propertyResolver.getProperty(%22
+return new HikariDataSource(hikariConfig);%0A    %7D%0A%0A    @Bean%0A    public javax.sql.DataSource tomcatConnectionPool() %7B%0A        final DataSource dataSource = new DataSource();%0A%0A        dataSource.setName(%22tomcat-connection-pool%22);%0A        dataSource.setUrl(url);%0A        dataSource.setUsername(userName);%0A        dataSource.setPassword(
 password
 %22));
@@ -2483,22 +2483,19 @@
 word
-%22)
 );%0A
-%0A
         
 retu
@@ -2494,49 +2494,236 @@
     
-return new HikariDataSource(hikariConfig)
+dataSource.setDriverClassName(driverClassName);%0A        dataSource.setMinIdle(2);%0A        dataSource.setMaxIdle(AVAILABLE_PROCESSORS / 2);%0A        dataSource.setMaxActive(AVAILABLE_PROCESSORS * 2 + 2);%0A%0A        return dataSource
 ;%0A  

Use the config options via Value, created a Tomcat connection pool