MySQL implemented using separate read and write drive ReplicationDriver

Master database from the replication environment has a good job, which is to solve the system how to read and write a separate function. The MySQL jdbc driver provides an implementation ReplicationDriver.

Both versions 1 database address

Reference: https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-url-format.html

Because subsequent configuration may be used, first tell us about these two usages mysql url host addresses.

The simplest wording is host: port, such as 192.168.5.128:3306, if the default port is 3306, you can not write, direct use 192.168.5.128.

More complex wording is specified attribute format address = (host = host_or_ip) (port = port) (key1 = value1) (key2 = value2) ... (keyN = valueN), such as address = (host = 192.168.5.128 ) (port = 3306) (type = master).

2 Pool Configuration

Reference: https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-master-slave-replication-connection.html

driverClassName: com.mysql.jdbc.ReplicationDriver

url string format:

jdbc:mysql:replication://[master host][:port],[slave host 1][:port][,[slave host 2][:port]]...[/[database]][?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]

There are two database address written above are a simple wording, ReplicationDriver the first default database server as a Master repository, other database servers as a follow-up from the database.

If the database is provided from a plurality of master database as Master, the format of the url:

jdbc:mysql:replication://address=(type=master)(host=master1host),address=(type=master)(host=master2host),address=(type=slave)(host=slave1host)/database[?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]

3 ReplicationDriver method call

Mysql driver uses the value of what use master or slave database, depending on Connection.getReadOnly () of. If the value is false, the command will be sent to the master database, if the value is true, the command is sent to the slave database. When multiple slave database, using the round robin scheduling (round-robin) algorithm to select a slave station database.

Therefore, in order for the drive to accurately mysql will send commands to the master or Slave, the code after obtaining the required data connection, Run Connection.setReadOnly (true) or Connection.setReadOnly (false).

The following is a sample code provided by the official mysql

import java.sql.ResultSet;

import java.util.Properties;

import com.mysql.jdbc.ReplicationDriver;

public class ReplicationDriverDemo {

  public static void main(String[] args) throws Exception {

    ReplicationDriver driver = new ReplicationDriver();

    Properties props = new Properties();

    // allow automatic reconnection from the library

    props.put("autoReconnect", "true");

    // more than one from the library using a load balancer

    props.put("roundRobinLoadBalance", "true");

    props.put("user", "foo");

    props.put("password", "password");

    Connection conn = driver.connect("jdbc:mysql:replication://master,slave1,slave2 /test", props);

    // On the main library to read and write, read-only set to "false"

    conn.setReadOnly(false);

    conn.setAutoCommit(false);

    conn.createStatement().executeUpdate("UPDATE some_table ....");

    conn.commit();

    // Now the query is executed from the library, the drive is automatically selected from a database query execution

    conn.setReadOnly(true);

    ResultSet rs = conn.createStatement().executeQuery("SELECT a,b FROM alt_table");

    .......

  }

}
 

 

4 Spring TX separate read and write

Project uses Spring Transaction Management Services, Transactional annotation has helped us to do the packaging, annotation attribute readOnly, default is false.


/**

 * {@code true} if the transaction is read-only.

 * Defaults to {@code false}.

 * <p>This just serves as a hint for the actual transaction subsystem;

 * it will <i>not necessarily</i> cause failure of write access attempts.

 * A transaction manager which cannot interpret the read-only hint will

 * <i>not</i> throw an exception when asked for a read-only transaction.

 * @see org.springframework.transaction.interceptor.TransactionAttribute#isReadOnly()

 */

boolean readOnly() default false;
 

Simple usage


@Transactional(readOnly = true)

public List<Enterprise> queryList(EnterpriseCriteria criteria) {

      return enterpriseDao.queryList(criteria);

}
 

Pull a branch existing project, use primary MariaDB built from the library were tested on VMWare, separate read and write can be definitely realized.

5 also need to understand the expansion of knowledge

It should further be free time to read the code, take a look at some of the following contents:
1.Transactional notes, is how to gradually call Connection.setReadOnly () is?
2. When the function is called multiple Transactional annotation, readOnly parameter whether it will have to call every time?

 

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160439.htm