我注意到我的播放框架应用程序没有将读取查询发送到仅读取的mysql从属。
我正在使用
-
com.mysql.cj.jdbc.Driver
作为javax.persistence.jdbc.driver
。 -
jdbc:mysql:replication://write-db-url,read-db-url/db_name
asjavax.persistence.jdbc.url
- DB是
AWS aurora
MySQL-compatible
,带有multi-az
副本。 - 我正在使用
hibernate
AS ORM。 - 我正在使用
play framework
。
我是否缺少任何配置/代码?
其他一切看起来都很好,例如jdbc
driver
和url
。
因为在您的问题中,提供了与您使用的与ORM或JPA以及您使用的连接代码相关的信息。
我在这里提供一个简单的主程序,您可以使用调试您的问题。完成此操作后,请专注于您的应用程序,您是否缺少同一件事。
这是JDBC
驱动程序确定的方式,是连接master
还是read replica
。
- 如果连接模式为
read+write
,即default
,则转到master
。 - 如果连接模式为
read
,则转到read-replica
。
在这里正式文档。
import java.sql.Connection;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Properties;
import java.sql.DriverManager;
public class ReplicationDemo {
public static void main(String[] args) throws Exception {
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", "password");
//
// 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 =
DriverManager.getConnection("jdbc:mysql:replication://master,slave1,slave2,slave3/test",
props);
//
// 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");
.......
}
}
希望它有帮助。