mycat mysql毫秒_MyCat不支持毫秒 bug fix

问题描述:

mysql jdbc的驱动(mysql-connector-java-5.1.34.jar)设置的服务器的版本号最低是5.6.4才不会截取时间毫秒,但是现在取的是mycat 的版本号 5.5.8-mycat-1.5.3.0-RELEASE-20170927190645 ,而不是实际的服务器版本5.6.21-log,所以时间精度丢失了

利用反射修改 JDBC4PreparedStatement 属性

public class JDBC4PreparedStatementWrapper extendsJDBC4PreparedStatement {public JDBC4PreparedStatementWrapper(MySQLConnection conn, String catalog) throwsSQLException {super(conn, catalog);

}public JDBC4PreparedStatementWrapper(MySQLConnection conn, String sql, String catalog) throwsSQLException {super(conn, sql, catalog);

}public JDBC4PreparedStatementWrapper(MySQLConnection conn, String sql, String catalog, PreparedStatement.ParseInfo cachedParseInfo) throwsSQLException {super(conn, sql, catalog, cachedParseInfo);

}protected void detectFractionalSecondsSupport() throwsSQLException {this.serverSupportsFracSecs = this.connection != null;

System.out.println("=======================>true");

}

}

public class MyCatBugFixBean implementsInitializingBean {private Logger logger =LoggerFactory.getLogger(getClass());

@Overridepublic void afterPropertiesSet() throwsException {try{

Field f1= PreparedStatement.class.getDeclaredField("JDBC_4_PSTMT_2_ARG_CTOR");

Field f2= PreparedStatement.class.getDeclaredField("JDBC_4_PSTMT_3_ARG_CTOR");

Field f3= PreparedStatement.class.getDeclaredField("JDBC_4_PSTMT_4_ARG_CTOR");

f1.setAccessible(true);

f2.setAccessible(true);

f3.setAccessible(true);

Field modifiers1= f1.getClass().getDeclaredField("modifiers");

modifiers1.setAccessible(true);

modifiers1.setInt(f1, f1.getModifiers()& ~Modifier.FINAL);

Field modifiers2= f2.getClass().getDeclaredField("modifiers");

modifiers2.setAccessible(true);

modifiers2.setInt(f2, f2.getModifiers()& ~Modifier.FINAL);

Field modifiers3= f3.getClass().getDeclaredField("modifiers");

modifiers3.setAccessible(true);

modifiers3.setInt(f3, f3.getModifiers()& ~Modifier.FINAL);

f1.set(null, JDBC4PreparedStatementWrapper.class.getConstructor(new Class[]{MySQLConnection.class, String.class}));

f2.set(null, JDBC4PreparedStatementWrapper.class.getConstructor(new Class[] { MySQLConnection.class, String.class, String.class}));

f3.set(null, JDBC4PreparedStatementWrapper.class.getConstructor(new Class[] { MySQLConnection.class, String.class, String.class, PreparedStatement.ParseInfo.class}));

modifiers1.setInt(f1, f1.getModifiers()& ~Modifier.FINAL);

modifiers2.setInt(f2, f2.getModifiers()& ~Modifier.FINAL);

modifiers3.setInt(f3, f3.getModifiers()& ~Modifier.FINAL);

}catch(Exception e) {//fix bug 异常

logger.error("fix mycat 不支持毫秒异常", e);

}

}

}

注:

运行时动态替换类的方法行不通。

PreparedStatement 拷贝后编译报错,所以运行时动态替换 PreparedStatement#detectFractionalSecondsSupport() 行不通。

运行时动态替换 JDBC4PreparedStatement 会更改类的定义(重写 detectFractionalSecondsSupport() 方法), JDK 不支持。

不允许新增、修改和删除成员变量

不允许新增和删除方法

不允许修改方法签名

参考:

Java反射-修改字段值, 反射修改static final修饰的字段:http://www.cnblogs.com/noKing/p/9038234.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值