Spring dynamic multi-data source instance Demo and spring dynamic instance demo
Recently, many people have consulted Spring on how to configure multiple data sources. It is troublesome to answer the questions one by one, and the configuration in previous blog posts is also problematic. Therefore, we hereby release a Demo for you.
There are two data sources in the Demo, namely Mysql and Oracle, and a simple test has been conducted. It is no problem to switch the data source dynamically. We hope this Demo can help you.
Demo:
Spring dynamic switching multi-data source Demo:Http://download.csdn.net/download/wangpeng047/8419953
In addition, I will give some instructions to illustrate the key points of multi-data source Configuration:
1. Pay attention to the configuration of the transaction interceptor.
This is the first item. First, you must 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, as the transaction has been enabled through the interceptor before entering this layer, it is not feasible to switch the data source at this layer. It is particularly important to understand the principles of the transaction. In my previous article, it is problematic to configure the interceptor for data source switching on the Dao layer (because it is an example, it is careless and I am sorry to mislead everyone ), however, there is no problem with the solution.
In the Demo, the Interceptor (dataSourceInterceptor) for data source switching is configured on the previous layer of the transaction Interceptor (txadvice), that is, the Controller layer.
2. Note the creation of database tables
Some people like to use Hibernate's automatic table creation function, but note that it is impossible to create tables automatically in multiple data sources, especially in different databases. Because Hibernate automatically creates a table when the project starts, only the table of the default data source configured for the project is created, and the table of other data sources is not automatically created. Pay attention to this.
3. The database dialect (dialect) of Hibernate can be ignored.
When multiple data sources exist, dialect settings can be ignored. Hibernate automatically identifies different databases during use, so you do not have to worry about this configuration, or even do not configure it.
4. No current session error is reported.
This is because sessionFactory is used. due to getCurrentSession (), the current session is bound to the thread, and only one Session is enabled for a thread (no error will be reported unless openSession () is used ), therefore, you need to set the binding relationship between the session and the thread.
The Demo uses Spring to manage Hibernate sessions. Therefore, OpenSessionInViewFilter is configured in web. xml and current_session_context_class is configured in hibernate. cfg. xml. [PS: When you use Spring to manage Hibernate, you can remove hibernate. cfg. xml, and in all the configured Spring configuration files, that is, hibernateProperties. Look at your preferences]
Finally, some frameworks are integrated in the Demo for your convenience, which is a benefit to everyone. The configurations and technologies of those frameworks are not described here. If you have any questions, please leave a message. If you have time, you will reply to your questions.