gpt4 book ai didi

java - 使用 ReplicationDriver 在 mysql slave 上调用存储过程

转载 作者:行者123 更新时间:2023-11-29 03:00:47 24 4
gpt4 key购买 nike

我有一个 Spring 应用程序,它当前使用存储过程执行一些查询。配置是这样的:

数据源:

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.ReplicationDriver"/>
<property name="url" value="jdbc:mysql:replication://master,slave1,slave2/db?allowMultiQueries=true"/>
<property name="username" value="${db.dbusername}"/>
<property name="password" value="${db.dbpassword}"/>
<property name="defaultReadOnly" value="true"/>
</bean>
<bean id="jdbcDeviceDAO" class="dao.jdbc.JdbcDeviceDAO">
<property name="dataSource" ref="dataSource"/>
</bean>

DAO:

public class JdbcDeviceDAO implements DeviceDAO {
// ...

public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.procGetCombinedDeviceRouting = new SimpleJdbcCall(jdbcTemplate)
.withProcedureName("get_combined_device_routing");
// ...
}

public CombinedDeviceRouting getCombinedDeviceRouting(String deviceName, String deviceNameType) {
SqlParameterSource in = createParameters(deviceName, deviceNameType);

Map<String, Object> results = this.procGetCombinedDeviceRouting.execute(in);

return extractResults(results);
}

现在,当我调用 getCombinedDeviceRouting(...) 时,它会失败并出现以下异常:

org.springframework.dao.TransientDataAccessResourceException: CallableStatementCallback; SQL [{call get_combined_device_routing()}]; Connection is read-only. Queries leading to data modification are not allowed; nested exception is java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

我知道连接是只读的,我需要它是那样的,以便查询在从属主机之间实现负载平衡。但是存储过程实际上是只读的,它只是很多 SELECT 语句,实际上我尝试将 READS SQL DATA 添加到它的定义中但是它没有用。

最后我到了阅读 mysql 的连接器代码的地步,我发现了这个:

protected boolean checkReadOnlySafeStatement() throws SQLException {
synchronized (checkClosed().getConnectionMutex()) {
return this.firstCharOfStmt == 'S' || !this.connection.isReadOnly();
}
}

这听起来很天真,但是连接器是否仅通过将第一个字符与“S”匹配来检查我的语句是否只读?如果是这种情况,似乎无法在从属主机上调用存储过程,因为该语句以“C”开头(CALL ...)。

有谁知道这个问题是否有解决方法?或许我假设这第一个字符检查是错误的?

最佳答案

看起来好像this is a bug with the driver我查看了代码,看看是否有一个简单的扩展点,但看起来您必须扩展很多类才能影响此行为:(

关于java - 使用 ReplicationDriver 在 mysql slave 上调用存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24069407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com