Learn sharding JDBC from getting started to going out-02: Source Speculation

Source: Internet
Author: User

SJDBC has the ability to read and write, to use this feature when creating a data source object is to use the class: Masterslavedatasource, and set the primary and standby data source and database name

This object has the following properties:

Name: Names of databases

Masterdatasource: Primary Data source Object

Slavedatasources: Repository Data source category

Slaveloadbalancestrategy: Select from algorithm

/**     * Gets the data source name of the primary or slave node.     *     * @param sqlstatementtype SQL type     * @return data source for primary or slave nodes */public    DataSource Getdatasource (final Sqlstatementtype sqlstatementtype) {        if (sqlstatementtype.select! = Sqlstatementtype | | Dml_flag.get () | | Hintmanagerholder.ismasterrouteonly ()) {            Dml_flag.set (true);            return masterdatasource;        }        Return Slaveloadbalancestrategy.getdatasource (name, slavedatasources);    }

The following three scenarios will take the main library

(1) write the main library, read and go from the library
(2) The current thread, the previous operation went to the main library, the query also go to the main library. Applies to the write operation, and then query the scene, this time if read away from the library, there may be no query, because the master-slave synchronization will have a delay
(3) Use hint to force the main library.
Hintmanager Hintmanager = Hintmanager.getinstance ();
Hintmanager.setmasterrouteonly ();

Learn sharding JDBC from getting started to going out-02: Source Speculation

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.