From 23b0b876741f7968141fa6a04124940b3a6a62cf Mon Sep 17 00:00:00 2001 From: Pinaki Poddar Date: Sun, 8 Mar 2009 18:11:55 +0000 Subject: [PATCH] OPENJPA-966: Support MySQL Replication Driver git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@751487 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/jdbc/kernel/JDBCStoreManager.java | 5 +++ .../openjpa/jdbc/sql/MySQLDictionary.java | 7 ++++ .../src/doc/manual/ref_guide_dbsetup.xml | 35 +++++++++++++++++-- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java index 3c7c4b353..304b4aac9 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java @@ -641,6 +641,11 @@ public class JDBCStoreManager } public Collection flush(Collection sms) { + try { + if (_conn != null && _conn.getInnermostDelegate().isReadOnly()) + _conn.setReadOnly(false); + } catch (SQLException e) { + } return _conf.getUpdateManagerInstance().flush(sms, this); } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MySQLDictionary.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MySQLDictionary.java index 3d25d95a4..1dfcc6613 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MySQLDictionary.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MySQLDictionary.java @@ -146,6 +146,13 @@ public class MySQLDictionary } } + public Connection decorate(Connection conn) throws SQLException { + String driver = conf.getConnectionDriverName(); + if ("com.mysql.jdbc.ReplicationDriver".equals(driver)) + conn.setReadOnly(true); + return conn; + } + private static int[] getMajorMinorVersions(String versionStr) throws IllegalArgumentException { int beginIndex = 0; diff --git a/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml b/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml index 7747ee1a9..eee8e7851 100644 --- a/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml +++ b/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml @@ -3215,9 +3215,40 @@ action is used. (See for more info about deleteTableContents.) Defaults to false, since the statement may fail if using InnoDB tables and delete constraints. - + + - + + + +Starting with Connector/J 3.1.7, MySQL supports a variant of the driver +com.mysql.jdbc.ReplicationDriver that automatically sends +queries to a read/write master, or a failover or round-robin load balanced set +of slaves based on the state of read-only status of the connection. +See +MySQL Reference for more details. + + +This replication feature can be used transparently with OpenJPA application by +following configuration: + + + + +openjpa.ConnectionDriverName: com.mysql.jdbc.ReplicationDriver + + + + +openjpa.ConnectionFactoryProperties: autoReconnect=true,roundRobinLoadBalance=true + + +OpenJPA will use a read-only connection with replicated database configuration +and will automatically switch the connection to a non-readonly mode if the +transaction is writing to the database. + + +