MySQL read/write separation using driver Com.mysql.jdbc.ReplicationDriver

Source: Internet
Author: User

Description Document: http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-replication-connection.html

code example:

1 Importjava.sql.Connection;2 ImportJava.sql.ResultSet;3 Importjava.util.Properties;4  5 ImportCom.mysql.jdbc.ReplicationDriver;6  7  Public classReplicationdriverdemo {8  9    Public Static voidMain (string[] args)throwsException {TenReplicationdriver Driver =Newreplicationdriver (); One   AProperties props =NewProperties (); -   -     //We want this for failover on the slaves theProps.put ("AutoReConnect", "true"); -   -     //We want to load balance between the slaves -Props.put ("Roundrobinloadbalance", "true"); +   -Props.put ("User", "foo"); +Props.put ("Password", "Bar"); A   at     // -     //Looks like a normal MySQL JDBC URL with a -     //comma-separated List of hosts, the first -     //being the ' master ', the rest being any number -     //of slaves that the driver would load balance against -     // in   -Connection conn = toDriver.connect ("Jdbc:mysql:replication://master,slave1,slave2,slave3/test", + props); -   the     // *     //Perform read/write work on the master $     //By Setting the READ-ONLY flag to "false"Panax Notoginseng     // -   theConn.setreadonly (false); +Conn.setautocommit (false); AConn.createstatement (). Executeupdate ("UPDATE some_table ....")); the conn.commit (); +   -     // $     //Now , does a query from a slave, the driver automatically picks one $     //From the list -     // -   theConn.setreadonly (true); -  WuyiResultSet rs = theConn.createstatement (). ExecuteQuery ("Select, b from Alt_table"); -   Wu      ....... -   } About}

From: http://www.cnblogs.com/taven/archive/2013/04/24/3040489.html

Related Article

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.