How set initial size connection to postgresql in application.yml - java

Always when i connect to my database, I see 10 idle connection. How can I set this in application.yml.
I use spring boot 1.5.6.RELEASE.
It's not working:
spring:
datasource:
maxActive: 5
maxIdle: 5
minIdle: 5
initialSize: 5
When I created #Bean it's working, but I need solution in application.yml
#Configuration
public class DBConfig {
#Value("${dbconfig.driver-class-name}")
private String driverClassName;
#Value("${dbconfig.url}")
private String url;
#Value("${dbconfig.username}")
private String username;
#Value("${dbconfig.password}")
private String password;
#Bean
public DataSource dataSource() throws SQLException {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(driverClassName);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}

I think you're missing to say these are properties. I think the following will work.
spring:
dataSource:
properties:
maxActive: 5
maxIdle: 5
minIdle: 5
initialSize: 5
Note: If you're using tomcat-jdbc, you have to define it explicitly like,
spring:
dataSource:
tomcat:
max-active: 5
max-idle: 5
min-idle: 5
initial-size: 5

Related

Spring Boot how to configure list of Hikari datasources dynamically

If I only have one datasource and I want to configure it with Hikari then in application.yml I can do something like this:
spring:
datasource:
app:
platform: POSTGRESQL
url: jdbc:postgresql://localhost:5432/mydb
username: mydbuser
password: mydbpass
hikari:
maximum-pool-size: 15
leak-detection-threshold: 15000
max-lifetime: 900000
data-source-properties: stringtype=unspecified
pool-name: defaultHikariPool
And in code I do this:
#Bean
#Primary
#ConfigurationProperties("spring.datasource.app")
fun appDataSourceProperties(): DataSourceProperties {
return DataSourceProperties()
}
#Bean
#ConfigurationProperties(prefix = "spring.datasource.app.hikari")
fun appDataSource(): HikariDataSource {
return appDataSourceProperties()
.initializeDataSourceBuilder()
.type(HikariDataSource::class.java)
.build()
}
This works very well. But let's say I have N datasources where size of N can vary depending on deployment. I created a block like this in the applicaiton.yml:
my-custom-parties:
parties:
- my-custom-party-id: 1
datasource:
platform: POSTGRESQL
url: jdbc:postgresql://localhost:5432/my-db-1
username: user1
password: pass1
hikari:
pool-name: db1HikariPool
maximum-pool-size: 3
leak-detection-threshold: 5000
max-lifetime: 900000
data-source-properties: stringtype=unspecified
- my-custom-party-id: 2
datasource:
platform: POSTGRESQL
url: jdbc:postgresql://localhost:5432/my-db-2
username: user2
password: pass2
hikari:
pool-name: db2HikariPool
maximum-pool-size: 10
leak-detection-threshold: 20000
max-lifetime: 900000
data-source-properties: stringtype=unspecified
And I created a configuration class like this:
#Component
#ConfigurationProperties(prefix = "my-custom-parties")
class MyCustomParties {
private var parties: List<MyCustomParty> = listOf()
fun setParties(parties: List<MyCustomParty>) {
this.parties = parties
}
fun getParties() = parties
}
class MyCustomParty {
lateinit var myCustomPartyId: String
private var datasource: DataSourceProperties? = null
fun setDatasource(datasource: DataSourceProperties) {
this.datasource = datasource
}
fun getDatasource() = datasource?.initializeDataSourceBuilder()?.type(HikariDataSource::class.java)?.build()
}
This intializes a hikari datasource, but it doesn't pick up the configuration properties from the file.
I can't hardcode #Bean's for every datasource because the number of datasources is different in different deployments.
So how can I wire this together correctly?

Spring Boot with Quartz Scheduler: Clustering not working

I am using the dependency spring-boot-starter-quartz and I have the following config in application.yaml for Quartz
spring:
quartz:
job-store-type: jdbc
jdbc:
initialize-schema: always
properties:
org:
quartz:
scheduler:
instanceId: AUTO
threadPool:
threadCount: 2
jobStore:
class: org.quartz.impl.jdbcjobstore.JobStoreTX
driverDelegateClass: org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
useProperties: false
tablePrefix: SCHEDULER_
isClustered: true
clusterCheckinInterval: 20000
I have the following beans defined in the Quartz configuration
#Bean
JobDetailFactoryBean notificationJobDetail() {
JobDetailFactoryBean bean = new JobDetailFactoryBean();
bean.setJobClass(NotificationJob.class);
return bean;
}
#Bean
CronTriggerFactoryBean notificationCronTrigger() {
CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
cronTriggerFactoryBean.setCronExpression(schedulerProperty.getCronNotification());
cronTriggerFactoryBean.setJobDetail(Objects.requireNonNull(notificationJobDetail().getObject()));
return cronTriggerFactoryBean;
}
#Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setTriggers(notificationCronTrigger().getObject());
schedulerFactoryBean.setApplicationContextSchedulerContextKey(QUARTZ_APPLICATION_CONTEXT);
return schedulerFactoryBean;
}
But some how the application.conf properties I guess is not read by Quartz and therefor clustering is not working. In the logs it shows
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

Spring boot application properties with different keys [duplicate]

This question already has answers here:
Spring Boot - inject map from application.yml
(8 answers)
Closed 4 years ago.
How can i define a Object to read my application.mysql-databases by key:
swaw:
stage: dev
ip: x.x.x.x
port: 3306
databases:
mysql:
url: "jdbc:mysql://...."
password:
dba: AAA
read: BBB
mssql:
url: "jdbc:mssql://...."
password:
dba: CCC
read: DDD
informix:
....
I try with this object:
#ConfigurationProperties(prefix = "swaw.databases")
public class Databases {
private Map<String, DatabasesConfig> map;
public static class DatabasesConfig {
private String url;
private Password password;
//GETTER AND SETTER
i get per request: {"ip":"1x.x.x.x","port":"3306","databases":null}
#emoleumassi - Try this one :
#ConfigurationProperties(prefix = "databases")
public class Databases {
private String url;
private Password password;
//GETTER AND SETTER
}

How to use 2 or more jdbcTemplate with spring-boot?

I want to use 2 or more jdbcTemplate in my project using application.properties.I try but got runtime exception.
########## My application.properties:-
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ccm_new
spring.datasource.username=test
spring.datasource.password=test
spring.oracledatasource.url=jdbc:oracle:thin:#localhost:1521:mastera
spring.oracledatasource.password=test
spring.oracledatasource.username=test
spring.oracledatasource.driver-class-name=oracle.jdbc.driver.OracleDriver
#Bean(name = "dsMaster") ############
#Primary
#ConfigurationProperties(prefix="spring.oracledatasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
#Bean(name = "jdbcMaster") #############
public JdbcTemplate masterJdbcTemplate(#Qualifier("dsMaster") DataSource dsMaster)
{
return new JdbcTemplate(dsMaster);
}
################I use the mysql connection normally but on use of oracle connection i got
org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:81)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:371)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:446)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456)
at enter code here
I got it where i am wrong,I want to make mysql connection through application.properties without #bean configuration.If you want to take 2 or more connection you just need to define all the datasource with their #ConfigurationProperties(prefix="spring.mysqldatasource") different prifix other than "spring.datasource".prifix " spring.datasource" use only when we need to make connection from only one database.Here is the final working code example:-
application.properties
spring.mysqldatasource.driver-class-name=com.mysql.jdbc.Driver
spring.mysqldatasource.url=jdbc:mysql://localhost:3306/ccm_new
spring.mysqldatasource.username=test
spring.mysqldatasource.password=test
spring.mysqldatasource.dbcp2.initial-size=5
spring.mysqldatasource.dbcp2.max-total=15
spring.mysqldatasource.dbcp2.pool-prepared-statements=true
spring.oracledatasource.url=jdbc:oracle:thin:#localhost:1521:mastera
spring.oracledatasource.password=test
spring.oracledatasource.username=test
spring.oracledatasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.oracledatasource.dbcp2.initial-size=5
spring.oracledatasource.dbcp2.max-total=15
spring.oracledatasource.dbcp2.pool-prepared-statements=true
#Configuration
public class PrototypeUtility {
#Bean(name = "dsMaster")
#Primary
#ConfigurationProperties(prefix="spring.oracledatasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
#Bean(name = "jdbcMaster")
public JdbcTemplate masterJdbcTemplate(#Qualifier("dsMaster") DataSource dsMaster) {
return new JdbcTemplate(dsMaster);
}
#Bean(name = "dsMasterMysql")
#ConfigurationProperties(prefix="spring.mysqldatasource")
public DataSource primaryDataSourceMysql() {
return DataSourceBuilder.create().build();
}
#Bean(name = "jdbcMasterMysql")
public JdbcTemplate masterMysqlJdbcTemplate(#Qualifier("dsMasterMysql") DataSource dsMasterMysql) {
return new JdbcTemplate(dsMasterMysql);
}
}
and then i autowired the both connection :-
#Autowired
private JdbcTemplate jdbcMasterMysql;
#Autowired
public JdbcTemplate jdbcMaster;
This code run successfully for me .
If any one have doubt,Don't hesitate to ask.
I got it where i am wrong,I want to make mysql connection through application.properties without #bean configuration.If you want to take 2 or more connection you just need to define all the datasource with their #ConfigurationProperties(prefix="spring.mysqldatasource") different prifix other than "spring.datasource".prifix " spring.datasource" use only when we need to make connection from only one database.Here is the final working code example:-
application.properties
spring.mysqldatasource.driver-class-name=com.mysql.jdbc.Driver
spring.mysqldatasource.url=jdbc:mysql://localhost:3306/ccm_new
spring.mysqldatasource.username=test
spring.mysqldatasource.password=test
spring.mysqldatasource.dbcp2.initial-size=5
spring.mysqldatasource.dbcp2.max-total=15
spring.mysqldatasource.dbcp2.pool-prepared-statements=true
spring.oracledatasource.url=jdbc:oracle:thin:#localhost:1521:mastera
spring.oracledatasource.password=test
spring.oracledatasource.username=test
spring.oracledatasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.oracledatasource.dbcp2.initial-size=5
spring.oracledatasource.dbcp2.max-total=15
spring.oracledatasource.dbcp2.pool-prepared-statements=true
#Configuration
public class PrototypeUtility {
#Bean(name = "dsMaster")
#Primary
#ConfigurationProperties(prefix="spring.oracledatasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
#Bean(name = "jdbcMaster")
public JdbcTemplate masterJdbcTemplate(#Qualifier("dsMaster") DataSource dsMaster) {
return new JdbcTemplate(dsMaster);
}
#Bean(name = "dsMasterMysql")
#ConfigurationProperties(prefix="spring.mysqldatasource")
public DataSource primaryDataSourceMysql() {
return DataSourceBuilder.create().build();
}
#Bean(name = "jdbcMasterMysql")
public JdbcTemplate masterMysqlJdbcTemplate(#Qualifier("dsMasterMysql") DataSource dsMasterMysql) {
return new JdbcTemplate(dsMasterMysql);
}
}

Spring boot unable to read from profile specific yaml file

I was playing around with spring boot and attempted to create a profile specific configuration file. I called it application-local.yml.
And added:
spring:
profiles:
active: local
mysql:
db:
url: jdbc:mysql://localhost:3306/db?serverTimezone=UTC
driverClassName: com.mysql.jdbc.Driver
username: root
password:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
show_sql: false
server:
port: 8080
And in my DatabaseConfig.java file I attempt to read from application-local.yml and configure the database:
#Configuration
#EnableTransactionManagement
public class DatabaseConfig {
#Value("${spring.mysql.db.url}")
private String url;
#Value("${spring.mysql.db.username}")
private String userName;
#Value("${spring.mysql.db.password}")
private String password;
#Value("${spring.mysql.db.driverClassName}")
private String driverClassName;
//hibernate
#Value("${hibernate.dialect}")
private String hibernateDialect;
#Value("${hibernate.show_sql}")
private String hibernateShowSql;
#Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl(url);
dataSource.setDriverClassName(driverClassName);
dataSource.setUsername(userName);
dataSource.setPassword(password);
return dataSource;
}
#Bean(name="entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean =
new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setDataSource(dataSource());
localContainerEntityManagerFactoryBean.setPackagesToScan("xxxx");
JpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
localContainerEntityManagerFactoryBean.setJpaProperties(hibernateProperties());
return localContainerEntityManagerFactoryBean;
}
#Bean
public PlatformTransactionManager platformTransactionManager() {
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
return jpaTransactionManager;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", hibernateDialect);
properties.put("hibernate.show_sql", hibernateShowSql);
return properties;
}
}
Now I get the error it's unable to
Error creating bean with name 'databaseConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.mysql.db.url' in value "${spring.mysql.db.url}"
but when I rename my property file to application.yml. It works perfectly fine.
I also attempted to run from the terminal using gradle command: ./gradlew -Dspring.profiles.active=local bootRun but I get the same error. It only works when I refactor the YAML file to application.yml. What am I doing wrong? I intend to have 3 profiles such as local, dev and prod.
Either add the following in your gradle file
bootRun {
profile: local
}
or try
./gradlew bootRun -Dspring.profiles.active=local
I had the same issue. I resolved it by adding
Spring:
profiles: local
Please make sure its indented properly.

Categories