0

I have a MySQL server that contains around 15 Databases (each database has between 5-20 tables). (Not my choice and not something that is able to be changed)

I have read several stackoverflow posts, baeldung, etc, which all talk about multiple data sources but seem to mean using Postgres/MySQL, or MySQL/H2. I am searching for a scalable solution for MySQL that allows for several databases to be used.

I am somewhat new to Spring, but not to Java, so I may just be missing something. This would be an example of my properties file.

application.properties

spring.datasource.database1.db.url=jdbc:mysql://localhost:3306/database1
spring.datasource.database1.db.username=user
spring.datasource.database1.db.password=password
spring.datasource.database1.db.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.database1.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.datasource.database1.jpa.show-sql=true
spring.datasource.database1.jpa.generate-ddl=true
spring.datasource.database1.jpa.hibernate.ddl-auto=update

spring.datasource.database2.db.url=jdbc:mysql://localhost:3306/database2
spring.datasource.database2.db.username=user
spring.datasource.database2.db.password=password
spring.datasource.database2.db.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.database2.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.datasource.database2.jpa.show-sql=true
spring.datasource.database2.jpa.generate-ddl=true
spring.datasource.database2.jpa.hibernate.ddl-auto=update

spring.datasource.database3.db.url=jdbc:mysql://localhost:3306/database3
spring.datasource.database3.db.username=user
spring.datasource.database3.db.password=password
spring.datasource.database3.db.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.database3.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.datasource.database3.jpa.show-sql=true
spring.datasource.database3.jpa.generate-ddl=true
spring.datasource.database3.jpa.hibernate.ddl-auto=update

I feel that the jpa portion of the properties are redundant, but when I tried to reuse them in the config file, it complained and wouldn't compile. I am not sure how to reuse the @ConfigurationProperties.

My file structure would contain a folder for each database, which would in turn have folders for each of the models, controllers, repositories, etc. For example:

Project │ pom.xml │ manifest.yml │ └───src └───main └───java | └───com | └───myorg | | Application.java | └───config | | | DatabaseConfig.java | | | └───databases | └───database1 | | | Database1Config.java | | └───models | | | | TableA.java | | | | TableB.java | | | | TableC.java | | | | | └───controllers | | | | TableAController.java | | | | TableBController.java | | | | TableCController.java | | | | | └───repositories | | | TableARepository.java | | | TableBRepository.java | | | TableCRepository.java | | | └───database2 | | Database2Config.java | └───models | | | TableD.java | | | TableE.java | | | TableF.java | | | └───controllers | | | TableEController.java | | | TableDController.java | | | TableFController.java | | | └───repositories | | TableDRepository.java | | TableERepository.java | | TableFRepository.java | └───resources | application.properties

Essentially, this is what I would like to have as a file structure, knowing that it will be growing with tables in the future. If there is something else that makes more sense, I am all ears.

If anyone could provide a link to documentation that describes what I need, that would be great. I am not looking for someone to code for me, just need to be pointed in the correct direction.

Please let me know if more clarification is needed. Thanks!

  • 1
    Properties support only single instance of the database, if you need multiple instances then you'll have to create beans for them yourself. Checkout this answer http://stackoverflow.com/a/30344608/5343269 – 11thdimension Apr 11 '17 at 16:19

1 Answers1

0

Hibernate provides multi-tenancy which allows for dynamically switching between multiple schemas or databases. The Hibernate docs describe it in more detail. A more spring-oriented example can i.e. be found here and here.

ldz
  • 2,217
  • 16
  • 21