Skip to content

This repository contains example how to initialize different data-sources in runtime and using them to querying SQL statements

License

Notifications You must be signed in to change notification settings

daggerok/spring-boot-runtime-datasource-initialization

Repository files navigation

spring-boot-runtime-datasource-initialization Build Status

Runtime datasource initialization using spring-boot

This repository contains my trying to initialize different data-sources in runtime and use them to querying / handling SQL data. To do so, my starting point is in-memory h2 database. I have implemented two rest resources (GET and POST). GET resource basically showing current (by default in-memory h2) dataSource bean containing from application context. POST resource allows you create new DataSource bean in runtime and add it to dataSourcesHolder bean. lastly we are executing a SQL query to verify if that dataSource is working properly

prepare databases using docker (hope you have enough RAM) and start app
./gradlew composeUp -Pprofile=all
bash build/libs/*.jar

# ora
http :8080/ \
  query="select 1 from dual" \
  springJpaDatabase=ORACLE \
  springDatasourceUrl="jdbc:oracle:thin:@//127.0.0.1:1521/xe" \
  springDatasourceUsername=system \
  springDatasourcePassword=oraclepassword \
  springDatasourceClassName="oracle.jdbc.OracleDriver"

{
    "databaseProductVersion": "Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production",
    "driverName": "Oracle JDBC driver",
    "driverVersion": "11.2.0.4.0",
    "holdingDataSources": "{ORACLE=HikariDataSource (HikariPool-2)}",
    "result": {
        "columnCount": 1
    },
    "url": "jdbc:oracle:thin:@//127.0.0.1:1521/xe",
    "userName": "SYSTEM"
}

# pg
http :8080/ \
  query="select 1" \
  springJpaDatabase=POSTGRESQL \
  springDatasourceUrl="jdbc:postgresql://0.0.0.0:5432/postgresdatabase" \
  springDatasourceUsername=postgresuser \
  springDatasourcePassword=postgrespassword \
  springDatasourceClassName="org.postgresql.Driver"

{
    "databaseProductVersion": "9.4.17",
    "driverName": "PostgreSQL Native Driver",
    "driverVersion": "PostgreSQL 9.4.1212.jre7",
    "holdingDataSources": "{ORACLE=HikariDataSource (HikariPool-2), POSTGRESQL=HikariDataSource (HikariPool-3)}",
    "result": {
        "columnCount": 1
    },
    "url": "jdbc:postgresql://0.0.0.0:5432/postgresdatabase",
    "userName": "postgresuser"
}

./gradlew composeDown -Pprofile=all
# or: docker-compose -f docker-compose-all.yaml down -v; docker-compose -f docker-compose-all.yaml rm -v

generated by jvm yeoman generator

About

This repository contains example how to initialize different data-sources in runtime and using them to querying SQL statements

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published