Recently, there are many people who are consulting on how to configure multiple data sources for spring, and answering them one by one is troublesome, and the configuration in previous blog posts is also problematic, so I will release a demo for everyone.
There are two data sources in Demo, namely MySQL and Oracle, and simple tests have been conducted. There is no problem in dynamically switching data sources. I hope this demo can help everyone.
Demo download address:
Spring dynamically switches multi-data source Demo: http://xiazai.VeVB.COM/201701/yuanma/dynamicDatasourceDemo_jb51.rar
I will also give some explanations to explain the key points when configuring multi-data source:
1. Pay attention to the configuration of transaction interceptor
This is the first priority. First of all, you need to understand that Spring's transaction management is bound to the data source. Once the program executes 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 transactions. In my previous article, it is problematic to configure the interceptor to switch the data source to the Dao layer (because it is an example, I am careless and apologize for misleading everyone), but the ideas provided are not problematic.
In the demo, the dataSourceInterceptor that switches data sources is configured on the previous 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 automatic table creation function, but it should be noted that in multiple data sources, especially in multiple data sources of different databases, it is not possible to automatically create tables. Because Hibernate automatic table creation is triggered at project startup, only tables for the default data source configured by the project are created, while tables for other data sources are not automatically created. Everyone should pay attention.
3. Hibernate's database dialect can be ignored
When multiple data sources, the dialect settings can be ignored. Hibernate will automatically recognize different databases when used, so there is no need to worry about this configuration, and it is even possible not to configure it.
4. Report No current session error
This is caused by using sessionFactory.getCurrentSession(). The current session is bound to the thread. A thread will only open one session (there will not be an error unless you use openSession()). Therefore, the binding relationship between the session and the thread needs to be set.
The demo uses Spring to manage Hibernate session, so OpenSessionInViewFilter is configured in web.xml, and current_session_context_class is configured in hibernate.cfg.xml. [PS: When using Spring to manage Hibernate, you can remove hibernate.cfg.xml, and the configuration files of all configured Spring are hibernateProperties. Depend on personal preferences]
Finally, some frameworks are simply integrated in the demo, which are convenient for everyone to use. It is a benefit for everyone. I will not explain the configuration and technology of those frameworks here. If you want to know, you can check the address:
spring multi-data source configuration://www.VeVB.COM/article/102282.htm
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.