2

This is fairly straight forward with a simple Spring DAO approach. However, using MyBatis, is there a way to setup multiple potential datasources?

The best approach I can think of is to use an ArraList of a Bean each containing datasource.driverclass,datasource.url, datasource.username, datasource.password etc.

The values for the datasources are stored in individual properties files. There could be 1 or 10 of these property files (or more).

So for example, one application startup all the property files would be loaded one at a time into an ArrayList. Then, based on the NAME=value line from the property file, we would know which datasource to hit.

So http:localhost:8080/name=db1

... would access all the data from the datasource configured with the name "09". Each property file would contain:

name=db1
datasource.driverclass=jdbc:sqlserver
datasource.url=jdbc:sqlserver://localhost:1433;databaseName=someDBname
datasource.username=user1
datasource.password=pass1

So the identifier here is "name=db1".

Would the best approach from a MyBatis implementation utilise an ArrayList of Beans?

6
  • Are you looking to provide multitenancy? If so, there's probably a way better solution than trying to roll your own like that. Commented Jan 23, 2017 at 19:22
  • please, edit your question to tell us more about the context, what are you trying to achieve? why are you using multiple datasources? all with same scheme? same data? Commented Jan 23, 2017 at 19:41
  • @Kayaman - that's exactly the plan, a multitenant solution. Any suggestions that you feel would work better in that situation? Commented Jan 23, 2017 at 21:02
  • @blackwizard - I'll add some details above. Basically a multitenant solution, different data/database and same schema. Commented Jan 23, 2017 at 21:02
  • I'd imagine you'd find something by googling "mybatis multitenancy". Commented Jan 23, 2017 at 21:12

2 Answers 2

1

Here are some leads if you want to keep up with multiple DB:

Anyway, I would say datasources shall be managed in the server confiquration instead of in the App.

Then Mybatis main configuration file must be placed in a location added to the classpath, but outside of the app package, because every new datasource must be referenced there inside an environment element.

And for every user request or session (in case of a web app), the configuration will be parsed because SqlSessionFactoryBuilder.build(reader, environment=NAME); must be called to choose the environment (=> the DB).

Sign up to request clarification or add additional context in comments.

Comments

0

I ended up using a hierarchical application.yml file detailing the multitenant connection values, based on a selected tenant code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.