Java Hibernate检查MySQL复制是否同步 [英] Java Hibernate Check MySQL Replication is in sync
问题描述
通常,我们通过控制台命令检查从属服务器上的复制是否正常工作
Normally we check if replication on slaves is working via the console command
SHOW SLAVE STATUS \G;
我想将此功能合并到servlet报告应用程序中. 但是,休眠似乎不允许这样做:
I would like to incorporate this functionality into a servlet reporting application. However, hibernate does not seem to allow this :
createSQLQuery("SHOW SLAVE STATUS");
...executing query...
java.lang.UnsupportedOperationException: not yet implemented for SQL queries
我确定可以使用本机JDBC实现此目标,但是也许有更好的方法吗? 环境:Linux 2.6.18,Tomcat 6/7,Hibernate 3.4,Java 1.6,MySQL 5 注意:我对在主服务器上插入时间戳的解决方案不感兴趣.
I'm sure it's possible to achieve this using native JDBC, however maybe there is a better way? Environment: Linux 2.6.18, Tomcat 6/7, Hibernate 3.4, Java 1.6, MySQL 5 Note: I'm not interested in solutions where we insert timestamps on master.
推荐答案
这对我有用..
SQLQuery query =session.createSQLQuery("show slave status");
ArrayList<Object[]> results = (ArrayList<Object[]>)query.list();
//there is only one row returned
Object [] result = results.get(0);
//Seconds Behind Master should always be the last value in the row.
String secondsBehindMaster = ""+result[result.length-1];
确保数据库用户具有REPLICATION CLIENT权限才能进行此查询.
Ensure DB user has REPLICATION CLIENT permission to make this query.
GRANT REPLICATION CLIENT ON *.* TO 'db_user'@'db_server' IDENTIFIED BY 'db_password';
这篇关于Java Hibernate检查MySQL复制是否同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!