我正在使用mySql对数据库进行维护。我已经设置了一个脚本来使用Percona不断检查死锁。我想要做的是让我的脚本只在出现新事件时向我们的管理员发送电子邮件。我设置了一个切换列“emailSent”来区分新旧死锁。我要检查的是死锁表是否为空,如果没有,则将电子邮件发送给我们的管理员。我的“其他”脚本工作正常,但我遇到了初始“if”的问题:
package edu.aspire.daos;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import edu.aspire.li.Student;
//Insert Student object as student record into DataBase
class InsertStudent {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
//Open Session
Session s = sf.openSession();
//Begin transaction
Transaction tx=s.beginTransaction();
//Create Student object
Student s1 = new Student();
s1.setSno(1);
s1.setName("abc");
s1.setEmail("abc@aspire.com");
s1.setMobile(7788198899L);
s.save(s1);
s.flush();
tx.commit();
s.close();
}
}
当我所有行的lock_mode为'X'且emailSent ='1'(或表为空)时,我本质上想要打印“当前没有新的死锁活动”。如果他们有emailSent ='0'和lock_mode为'X'我想让我的其他人执行。
非常感谢任何帮助。
答案 0 :(得分:3)
您可以执行以下操作
MYSQL_HOST=10.20.30.40
MYSQL_USER=root
MYSQL_PASS=rootpassword
MYSQL_CONN="-h${MYSQL_HOST} -u${MYSQL_USER} -p${MYSQL_PASS}"
SQL="SELECT COUNT(1) from deadlocks WHERE lock_mode='X' AND emailSent='0'"
DEADLOCKS_FOUND=`mysql ${MYSQL_CONN} -ANe "${SQL}"`
if [ ${DEADLOCKS_FOUND} -gt 0 ]
then
echo "Deadlocks Detected"
fi
或者如果您在本地工作
MYSQL_USER=root
MYSQL_PASS=rootpassword
MYSQL_CONN="-u${MYSQL_USER} -p${MYSQL_PASS}"
SQL="SELECT COUNT(1) from deadlocks WHERE lock_mode='X' AND emailSent='0'"
DEADLOCKS_FOUND=`mysql ${MYSQL_CONN} -ANe "${SQL}"`
if [ ${DEADLOCKS_FOUND} -gt 0 ]
then
echo "Deadlocks Detected"
fi