Spring dynamic multi data source configuration instance demo

Recently, many people have consulted spring on how to configure multiple data sources, so it is troublesome to answer them one by one, and there are problems with the configuration in previous blog posts. Therefore, we hereby re release a demo to you.

There are two data sources in the demo, namely MySQL and Oracle, and simple tests have been carried out. There is no problem in dynamically switching data sources. I hope this demo can help you.

Demo download address:

Spring dynamic switch multi data source Demo: http://xiazai.jb51.net/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 transaction interceptor

This is the first one. First of all, you should understand that spring's transaction management is bound to the data source, Once the program is executed to the transaction management layer (such as service), since the transaction has been opened through the interceptor before entering this layer, it is not possible to switch the data source at this layer. It is particularly important to understand the principle of transaction. In my previous article, it is problematic to configure the interceptor for switching the data source at the Dao layer (it's careless because it's an example. I apologize for misleading everyone), but the ideas provided are no problem.

In the demo, the data source interceptor for switching data sources is configured at the upper layer of the transaction interceptor (txadvice), that is, the controller layer.

2. Pay attention to the creation of database tables

Some people like to use hibernate's function of automatically creating tables, but it should be noted that in multiple data sources, especially multiple data sources of different databases, it is impossible to automatically create tables. Because hibernate automatic table creation is triggered when the project is started, only the tables of the default data source configured by the project will be created, while the tables of other data sources will not be created automatically. Everyone should pay attention.

3. Hibernate database dialect can be ignored

In case of multiple data sources, the dialect setting can be ignored. Hibernate will automatically identify different databases when using, so there is no need to tangle with this configuration, or even no configuration.

4. No current session error is reported

This is because sessionfactory is used As a result of getcurrentsession(), the current session is bound to the thread, and a thread will only open one session (no error will be reported unless opensession() is used). Therefore, it is necessary to set the binding relationship between the session and the thread. Spring is used in the demo to manage hibernate sessions, so it is on the web Opensessioninview filter is configured in hibernate. XML cfg. Current is configured in XML_ session_ context_ class。 [PS: when using spring to manage hibernate, hibernate.cfg.xml can be removed, and all configured spring configuration files are hibernateproperties. See your personal preferences]

Finally, some frameworks are simply integrated in the demo, which is convenient for you to use. It is a benefit to you. The configuration and technology of those frameworks will not be described here. If you want to know, you can see the address:

Spring multi data source configuration: https://www.oudahe.com/p/11860/

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>