Spring dynamic Multi-data Source instance Demo

Source: Internet
Author: User

Recently due to the many people consulting spring How to configure a multi-data source, the answer is more troublesome, and the previous blog configuration is also problematic, so we hereby republish a demo to everyone.

The demo has two data sources, namely MySQL and Oracle, and has carried out a simple test, dynamic switch data source is no problem, I hope this demo can help everyone.

Demo:

Spring Dynamic switch Multiple data source demo:http://download.csdn.net/download/wangpeng047/8419953


In addition, I give some instructions, explain the next multi-data source configuration when the focus:

1. Note the configuration of the transaction interceptor

This is the first one. First you have to understand that spring's transaction management is bound to the data source, once the program executes to the level of transaction management (such as service), because the transaction has been opened through the interceptor before entering the layer, so it is not possible to switch the data source at that layer, Understand the principle of the transaction is particularly important, my previous article, will switch the data source interceptor configuration in the DAO layer is problematic (because it is an example, so careless, to mislead everyone I apologize), but provide the idea is no problem.

In the demo, The Interceptor (Datasourceinterceptor) that switches the data source is configured on the upper level of the transaction interceptor (Txadvice), which is the controller layer.

2. Note the creation of database tables

Some people like the ability to automatically create tables with hibernate, but it's important to note that it's not possible to automatically build tables in multiple data sources, especially multiple data sources in different databases. Because hibernate automatic tables are triggered when the project starts, only the tables for the default data source for the project configuration are established, and the tables for other data sources are not created automatically. Everyone should pay attention to the point.

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

In a multi-data source, the dialect settings can be ignored, hibernate will automatically recognize the different databases when used, so there is no need to tangle with this configuration, or even not configured.

4. Report no current session error

This is due to the use of sessionfactory.getcurrentsession (), the current session is bound to the thread, a thread will only open a session (unless the use of opensession () will not error), Therefore, you need to set the session binding relationship with the thread.

Spring Management Hibernate session is used in the demo, so Opensessioninviewfilter is configured in Web. XML and configured in Hibernate.cfg.xml current_session _context_class. PS: When using spring to manage hibernate, you can remove the hibernate.cfg.xml, and in the full configuration of the spring configuration file, the hibernateproperties. Let's have a look at someone's liking.


Finally, the demo in a simple integration of some framework, convenient for everyone to use, is to give everyone's welfare, about those framework configuration and technology, here is no longer elaborated. If you have any questions, can give me a message, I have the time, will actively reply to the questions of everyone.

Spring dynamic Multi-data Source instance Demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.