Oracle 5.0 Reference Manual page 2103

Table of Contents

Advertisement

standalone application. Because it accepts the same URL format as the standard MySQL JDBC driver,
ReplicationDriver
creation unless it is the only MySQL JDBC driver registered with the
Here is a short example of how
import java.sql.Connection;
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();
// We want this for failover on the slaves
props.put("autoReconnect", "true");
// We want to load balance between the slaves
props.put("roundRobinLoadBalance", "true");
props.put("user", "foo");
props.put("password", "bar");
//
// 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 will load balance against
//
Connection conn =
driver.connect("jdbc:mysql:replication://master,slave1,slave2,slave3/test",
//
// Perform read/write work on the master
// by setting the read-only flag to "false"
//
conn.setReadOnly(false);
conn.setAutoCommit(false);
conn.createStatement().executeUpdate("UPDATE some_table ....");
conn.commit();
//
// Now, do a query from a slave, the driver automatically picks one
// from the list
//
conn.setReadOnly(true);
ResultSet rs =
conn.createStatement().executeQuery("SELECT a,b FROM alt_table");
.......
}
}
Consider investigating the Load Balancing JDBC Pool (lbpool) tool, which provides a wrapper around
the standard JDBC driver and enables you to use DB connection pools that includes checks for system
failures and uneven load distribution. For more information, see
20.3.5.8. Mapping MySQL Error Numbers to JDBC SQLState Codes
The table below provides a mapping of the MySQL error numbers to JDBC
Connector/J (JDBC) Reference
does not currently work with java.sql.DriverManager-based connection
ReplicationDriver
props);
might be used in a standalone application:
Load Balancing JDBC Pool
2083
.
DriverManager
values.
SQLState
(lbpool).

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents