java程序执行命令行,解锁数据库表

有些表锁的时间长或其他原因,在plsql中不能解锁,只能用命令行解锁。

有些功能跨平台系统的交互偶尔会锁表,就需要自动解锁。

下面是解锁的代码:

package com.lg.BreakOracleUtils;

import com.lg.DB.DBProjp;
import com.lg.database.DbManager;
import com.lg.database.DbsConnection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class BreakOracleLock
{
  private static final Log logger = LogFactory.getLog(BreakOracleLock.class);

  public static void breakOracleLock() { logger.info("解锁xx表定时程序已经启动了!");

    DbsConnection connect = null;
    try
    {
      DBProjp db = new DBProjp();

      DbManager dbm = new DbManager();
      connect = dbm.getConnection(db.url, db.userName, db.passWord);

      String sql = "select b.username, b.sid, b.serial#, logon_time, spid, c.object_name\n  from v$locked_object a, v$session b, v$process p, all_objects c\n "+
        " where a.session_id = b.sid\n and b.paddr = p.addr\n and a.object_id = c.object_id\n and (c.object_name like '表名%' or c.object_name like '表名%' )\n "
        +" and b.username in ('数据库名' , '数据库名') \n and logon_time<=(sysdate-30/24/60)\n order by b.logon_time";
///设置锁表时间30分钟,即表的锁定时间小于当前时间30外,具体表的具体业务分析锁表的时间,自己设定 / List list = connect.select(sql); if ((list != null) && (list.size() > 0)) { Map m = (Map)list.get(0); String spid = (String)m.get("spid"); String cmdStr = "orakill orcl " + spid; logger.info("解锁xx表执行命令:" + cmdStr + ",表名:" + m.get("object_name")); Process localProcess = Runtime.getRuntime().exec(cmdStr); } connect.release(); } catch (Exception e) { logger.info("解锁xx表定时程序失败!" + e.getMessage()); try { if (connect != null) connect.release(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } logger.info("解锁xx表定时程序结束!"); } }

  

posted @ 2017-12-22 10:33  田野_7  阅读(737)  评论(0编辑  收藏  举报