简体   繁体   中英

Defining and Removing Spring Boot / Spring JPA Datasources Programmatically at Runtime?

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM