Spring dynamic multi data source configuration instance Demo

  • 2020-05-27 05:29:32
  • OfStack

Recently, many people have asked spring how to configure multiple data sources, and it is troublesome to answer 11 questions. Besides, there were some problems with the configuration in the previous blog posts. Therefore, we hereby re-publish an Demo for you.

Demo has two data sources in total, MySQL and Oracle, and it has been simply tested. There is no problem in dynamically switching data sources. I hope Demo can help you.

Demo download address:

Spring dynamic switching multiple source Demo: http: / / xiazai ofstack. com / 201701 / yuanma dynamicDatasourceDemo_jb51 rar

In addition, I will give some instructions to explain the key points of multi-data source configuration:

1. Pay attention to the configuration of the transaction interceptor

That's number one. First you have to understand that Spring transaction management and data binding, as the program execution to the 1 transaction management layer (such as service), due to the transaction before entering the layer is opened through interceptors, so in this layer switch the data source is not enough, understand the principle of transaction is particularly important, my previous article, will Dao switch data source of the interceptor configuration in the layer is problematic, because is the sample, so careless, apologise for misled everybody I), but there is no question of thinking offers.

Demo configures the data source switching interceptor (dataSourceInterceptor) to the transaction interceptor (txadvice), which is the Controller layer.

2. Notice the creation of the database tables

Some people like hibernate's ability to automatically create tables, but it is important to note that in multiple data sources, especially for different databases, it is not possible to automatically build tables at all. Because Hibernate automatic table building is triggered at project startup, only the tables for the default data source configured for the project are built, while tables for other data sources are not automatically created. You have to be careful.

3. Hibernate's database dialect (dialect) can be ignored

Dialect Settings can be ignored when using multiple data sources, and Hibernate automatically recognizes different databases when used, so you don't have to struggle with this configuration, or even not configure it.

4. Error reporting No current session

This is caused by the use of sessionFactory.getCurrentSession (). current session is bound to threads, and only one thread will turn on one Session (unless openSession() is used, no error will be reported). Therefore, the binding relationship between session and threads needs to be set.
Demo USES Spring to manage Hibernate's session, so OpenSessionInViewFilter is configured in web.xml, and current_session_context_class is configured in hibernate.cfg.xml. [PS: when Spring is used to manage Hibernate, hibernate.cfg.xml can be removed from the hibernate.cfg.xml, and hibernateProperties is in the configuration file of Spring, which is fully configured.

Finally, some frameworks are simply integrated in Demo, which is convenient for you to use and is a gift to you. As for the configuration and technology of those frameworks, I won't elaborate here. If you want to know more, please refer to the following address:

spring multiple data source configuration: https: / / www ofstack. com article / 102282. htm


Related articles: