免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3043 | 回复: 5

jdbc和主从复制的奇怪问题 [复制链接]

论坛徽章:
6
CU大牛徽章
日期:2013-04-17 10:59:39CU大牛徽章
日期:2013-04-17 11:01:45CU大牛徽章
日期:2013-04-17 11:02:15CU大牛徽章
日期:2013-04-17 11:02:36CU大牛徽章
日期:2013-04-17 11:02:582015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2011-04-08 14:33 |显示全部楼层
按照mysql的ref设置了一下
用jdbc:mysql://master,slave/testdb 连接
发现只要master或者slave fail掉了,都无法update了(可以select)可用性反而降低了?

测试代码

  1. package test;

  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.util.Properties;

  5. public class SerivceTest {

  6.   public static void main(String[] args) throws Exception {
  7.     com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
  8. //    ReplicationDriver driver = new ReplicationDriver();
  9.     Properties props = new Properties();

  10. //    // We want this for failover on the slaves
  11. //    props.put("autoReconnect", "true");
  12. //
  13. //    // We want to load balance between the slaves
  14. //    props.put("roundRobinLoadBalance", "true");

  15.     props.put("user", "crud");
  16.     props.put("password", "");

  17.     //
  18.     // Looks like a normal MySQL JDBC url, with a
  19.     // comma-separated list of hosts, the first
  20.     // being the 'master', the rest being any number
  21.     // of slaves that the driver will load balance against
  22.     //

  23.     Connection conn =
  24.         driver.connect("jdbc:mysql://master,slave/testdb",
  25.             props);

  26.     //
  27.     // Perform read/write work on the master
  28.     // by setting the read-only flag to "false"
  29.     //

  30.     conn.setReadOnly(false);
  31.     conn.setAutoCommit(false);
  32.     //conn.createStatement().executeUpdate("UPDATE some_table ....");
  33.     conn.commit();

  34.     //
  35.     // Now, do a query from a slave, the driver automatically picks one
  36.     // from the list
  37.     //

  38. //    conn.setReadOnly(true);

  39.     ResultSet rs =
  40.       conn.createStatement().executeQuery("SELECT * FROM test ");
  41.     while(rs.next()) {
  42.       System.out.println(rs.getString("id"));
  43.     }
  44.   }

  45. }

复制代码
conn.commit();
以后没反应了.
一直在等待,貌似在等待连接已经fail的那个mysql

论坛徽章:
6
CU大牛徽章
日期:2013-04-17 10:59:39CU大牛徽章
日期:2013-04-17 11:01:45CU大牛徽章
日期:2013-04-17 11:02:15CU大牛徽章
日期:2013-04-17 11:02:36CU大牛徽章
日期:2013-04-17 11:02:582015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2011-04-08 16:16 |显示全部楼层
大家都不是使用jdbc的么

论坛徽章:
0
发表于 2011-04-08 18:02 |显示全部楼层
本帖最后由 horizonhyg 于 2011-04-08 18:06 编辑

回复 1# realmon

论坛徽章:
6
CU大牛徽章
日期:2013-04-17 10:59:39CU大牛徽章
日期:2013-04-17 11:01:45CU大牛徽章
日期:2013-04-17 11:02:15CU大牛徽章
日期:2013-04-17 11:02:36CU大牛徽章
日期:2013-04-17 11:02:582015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2011-04-08 18:11 |显示全部楼层
ReplicationDriver,com.mysql.jdbc.Driver我都试过
按官方的解释,ReplicationDriver是可以把readonly的请求route到slave去,这个功能我可要可不要.
不能忍受的是 为什么slave fail了以后,master 也不能用了呢

论坛徽章:
0
发表于 2011-04-08 18:30 |显示全部楼层
回复 4# realmon


    以前遇到过,主坏了不行,从坏了也不行,主从关系坏了也不行

论坛徽章:
6
CU大牛徽章
日期:2013-04-17 10:59:39CU大牛徽章
日期:2013-04-17 11:01:45CU大牛徽章
日期:2013-04-17 11:02:15CU大牛徽章
日期:2013-04-17 11:02:36CU大牛徽章
日期:2013-04-17 11:02:582015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2011-04-13 13:44 |显示全部楼层
找到问题了,原来是个bug
Bug #56429 Failover functionality establishes useless connections to non-primary servers
http://bugs.mysql.com/bug.php?id=56429
把驱动升级到5.1.14以上就OK了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP