I need to create Spring Boot Datasources dynamically at runtime. Anyone know how to do this. Effectively I need to do this on demand:
spring:
datasource:
url: jdbc:h2:mem:testdb1;
username: sa
password:
driver-class-name: org.h2.Driver
platform: h2
spring:
datasource:
url: jdbc:h2:mem:testdb2;
username: sa
password:
driver-class-name: org.h2.Driver
platform: h2
...
spring:
datasource:
url: jdbc:h2:mem:testdbN;
username: sa
password:
driver-class-name: org.h2.Driver
platform: h2
Thoughts?
Try below way to create data-source programmatically
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl("jdbc:h2:mem:testdb1");
dataSource.setUsername("sa");
dataSource.setPassword("");
dataSource.setDriverClassName("org.h2.Driver");
Related
I have below implementation currently working in spring boot 2.3. But when I migrate the same to spring boot 2.4. properties not reading from config server.
Code
#ConditionalOnProperty({"app.xzy.hosts"})
public class clientConfig {
// implementation
}
bootstrap.yml
spring:
cloud:
config:
uri: http://main-config-server.com
username: user
password: pass
fail-fast: true
application.yml
app:
xyz:
hosts: ${app.main.config.hosts}
application.yml in config server
app:
main:
config
hosts: http://myhost.com
This implementation is working fine in spring boot 2.3. But after the upgrade to spring boot 2.4 this giving below error in the startup.
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'app.xzy.hosts' in value "${app.main.config.hosts}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
Finally figured out the issue.
bootstrap.yml file is no longer enabled by default. You need enable it by adding new dependency in spring cloud 2020.0.0 and it solved my issue.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
Reference: https://spring.io/blog/2020/10/07/spring-cloud-2020-0-0-m4-aka-ilford-is-available
I using spring boot with apache camel and I want to create quartz scheduler but when I using this configuration nothing has happened:
camel:
component:
quartz2:
properties:
org:
quartz:
scheduler:
instanceName: ClusteredSchedular
instanceId: AUTO
threadPool:
class: org.quartz.simpl.SimpleThreadPool
threadCount: 25
threadPriority: 5
jobStore:
class: org.quartz.impl.jdbcjobstore.JobStoreTX
driverDelegateClass: org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
dataSource: quartz
isClustered: true
clusterCheckinInterval: 20000
dataSource:
quartz:
driver: oracle.jdbc.driver.OracleDriver
URL: jdbc:oracle:thin:#localhost:1521:xe
user: test
password: test
maxConnections: 5
validationQuery: select 1 from dual
Does anyone have an idea why this configuration doesn't work? Camel only see this configuration from default config
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
org.quartz.jobStore.misfireThreshold: 60000
org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
I would prefer to use springboot datasource to enable quartz clustering.
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://127.0.0.1:5432/postgres
username: postgres
password: password
quartz:
scheduler-name: quartzSchedulernot work anymore
jobStoreType: jdbc
startup-delay: PT10S
wait-for-jobs-to-complete-on-shutdown: true
properties:
org.quartz.scheduler.instanceId: AUTO
org.quartz.scheduler.jmx.export: true
org.quartz.threadPool.threadCount: 15
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
org.quartz.jobStore.driverDelegateClass: org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
org.quartz.jobStore.tablePrefix: QRTZ_
org.quartz.jobStore.isClustered: true
org.quartz.jobStore.clusterCheckinInterval: 1000
I would like to connect Heroku Postgres with Spring Boot App.
Properties:
spring:
datasource:
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://ec2-54-75-251-84.eu-west-1.compute.amazonaws.com:5432/ddjbpu7qcel282?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
username: my_username
password: my_password
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
generate-ddl: true
show-sql: true
hibernate:
ddl-auto: update
Exception during application start:
Caused by: org.postgresql.util.PSQLException: This ResultSet is closed.
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkClosed(AbstractJdbc2ResultSet.java:2839) ~[postgresql-9.3-1100-jdbc41.jar:na]
What should i do to get my database connection working?
Thanks.
PostGres DB Properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:postgresql://<host>:5432/<db_name>?ssl=true&sslmode=require&user=<username><password>&sslfactory=org.postgresql.ssl.NonValidatingFactory
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driverClassName=org.postgresql.Driver
and following dependencies in POM.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
</dependency>
Nothing else is required for connecting heroku postgres with your local application.
I want to deploy my java spring boot application on Google cloud engine. It works well when I do not configure a db. But then when I use jpa, it just cannot find the connection. I tried multiple approached but non worked so far.
spring:
jpa:
database: MYSQL
show-sql: false
hibernate:
ddl-auto: update
datasource:
driver-class-name: com.mysql.jdbc.GoogleDrive
username: root
password: ********
url: jdbc:google:mysql://something-app:us-central1:something/something?user=root
This is the last config that I tried but it cannot find the driver. I have this in my pom.cml
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory</artifactId>
<version>1.0.0-beta1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.4</version>
</dependency>
Any idea of what I am doing wrong. Or example (github or other) that has it working?
stack trace:
2016-10-08 14:13:13 default[20161008t161026] java.sql.SQLException: Driver:com.google.appengine.api.rdbms.AppEngineDriver#746b25b1 returned null for URL:jdbc:google:mysql://something-app:us-central1:something/something?user=root
I finally got it working with the following configuration.
spring:
jpa:
database: MYSQL
show-sql: false
hibernate:
ddl-auto: update
datasource:
url: jdbc:mysql://google/myproject?cloudSqlInstance=myproject-app:us-central1:myproject&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=******&password=**********
I used this in the pom.xml
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory</artifactId>
<version>1.0.1</version>
</dependency>
I receive an error when I deploy my web application, using IntelliJ 14.0, to a local Apache Tomcat instance. I am using Spring Boot 1.2.1.RELEASE and with a JNDI connection.
The contents of my application.yaml file looks like this:
spring:
profiles:
active: production
---
spring:
profiles: development
datasource:
platform: h2
---
spring:
profiles: production
datasource:
jndi-name: java:/comp/env/jdbc/teams
platform: mysql
jpa:
hibernate:
ddl-auto: create-drop
My configuration file, Application.java, looks like this:
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
I am using Apache Tomcat 8.0.15 and have configured a JNDI connection in the context.xml file.
<Resource name="jdbc/teams" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="***" password="***" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/teams"/>
I am using IntelliJ 14.0 to deploy my application locally to Apache Tomcat using the production profile.
The error I receive when when I attempt to deploy the application is as follows:
org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [org.apache.tomcat.dbcp.dbcp2.BasicDataSource#4f83492a] with key 'dataSource'; nested exception is javax.management.InstanceAlreadyExistsException: Catalina:type=DataSource,host=localhost,context=/project,class=javax.sql.DataSource,name="jdbc/teams"
What could be wrong with the way the application is configured?
I modified the application.yaml file and the application is now working. I added spring.jmx.enabled and spring.jpa.database-platform properties to the application.yaml file.
spring:
profiles:
active: production
spring:
profiles: development
datasource:
platform: h2
spring:
profiles: production
datasource:
jndi-name: java:/comp/env/jdbc/teams
platform: mysql
jpa:
hibernate:
ddl-auto: create-drop
database-platform: org.hibernate.dialect.MySQL5Dialect
jmx:
enabled: false