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