多并发加锁解锁

//解锁
public boolean unLock() {

		con = DBConnPool.getConnection();
		if (con == null) {
			// @@错误处理
			CError.buildErr(this, "数据库连接失败!");
			return false;
		}
		// 设置为非自动提交

		try {
			con.setAutoCommit(false);
			if (!pareUnLockData(con)) {
				con.rollback();
				con.close();
				return false;
			}
			con.commit();
			con.close();
		} catch (Exception ex) {
			CError.buildErr(this, ex.toString());
			try {
				// 如果发生异常以后,关闭连接
				if (con != null) {
					con.rollback();
					con.close();
				}
			} catch (Exception e) {
			}
			return false;
		}
		return true;

	}
public boolean pareUnLockData(Connection conn) {
		try {
			//tongmeng 2008-10-20 modify
			//使用hashtable
			Enumeration eKey=mLockHashtable.keys(); 
			while(eKey.hasMoreElements()) 
			{ 
				String key=(String)eKey.nextElement();
				String tValue = (String)mLockHashtable.get(key);
				//获取后从hashtable中移除 add by wk
				mLockHashtable.remove(key);
				
				String[] tempNo = key.split(":");
				String tOperatedNo = tempNo[0];
				String tGroupNo = tempNo[1];
				LockAppGroupDB tLockAppGroupDB = new LockAppGroupDB(conn);
				tLockAppGroupDB.setOperatedNo(tOperatedNo);
				tLockAppGroupDB.setLockGroup(tGroupNo);

				if (!tLockAppGroupDB.delete()) {
					// 在外面做连接关闭和回滚操作
					return false;
				}
			}
		} catch (Exception ex) {
			CError.buildErr(this, ex.toString());
			return false;
		}
		return true;
	}

//获取链接
static public DBConn getConnection() {
		JdbcUrl JUrl = new JdbcUrl();
		// update by wangzw,为了支持WebLogic、apache、WebSphere的连接池,在得到连接的最前面判断
		if (JUrl.jndi || JUrl.getDBType().toUpperCase().equals("WEBLOGICPOOL")
				|| JUrl.getDBType().toUpperCase().equals("COMMONSDBCP")
				|| JUrl.getDBType().toUpperCase().equals("WEBSPHERE")) {
			DBConn tDBConn = new DBConn();
			if (tDBConn.createConnection()) {
				return tDBConn;
			} else {
				return null;
			}
		}

		try {
			DBSemaphore.Lock();
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}

		// 如果没有返回,则表示使用的是自己的连接池
		for (int nIndex = 0; nIndex < nConnCount; nIndex++) {
			DBConn dbConn = dbConns[nIndex];
			// 判定连接是否被使用
			if (dbConn.isInUse()) {
				continue;
			}
			if (!dbConn.createConnection()) {
				// 如果创建连接失败
				DBSemaphore.UnLock();
				return null;
			}
			// 如果连接数超过1的话,才输出,否则输出太多,很烦人
			if (nIndex >= 1) {
				logger.debug("DBConnPool : get connection, index is "
						+ String.valueOf(nIndex));
			}
			try {
				// 特殊处理连接的AutoCommit是否已经被设置
				dbConn.setAutoCommit(true);
				dbConn.setInUse();
				DBSemaphore.UnLock();
				return dbConn;
			} catch (Exception ex) {
				ex.printStackTrace();

				DBSemaphore.UnLock();

				return null;
			}
		}
		// 如果全部的连接数都被占用的话,应用程序出错
		logger.debug("DBConnPool : All connections are in use");
		// 如果为了应用正常流转,可以在这里强制释放掉全部的应用连接数
		DBSemaphore.UnLock();
		return null;
	}

//加锁,锁定业务号
public boolean lock(String tOperatedNo, String tLockModule) {
		try {
			con = DBConnPool.getConnection();
			if (con == null) {
				// @@错误处理
				CError.buildErr(this, "数据库连接失败!");
				return false;
			}
			// 设置为非自动提交
			con.setAutoCommit(false);
			if (!pareLockData(con, tOperatedNo, tLockModule)) {
				con.rollback();
				con.close();
				return false;
			}
			con.commit();
			con.close();
		} catch (Exception ex) {
			CError.buildErr(this, ex.toString());
			try {
				// 如果发生异常以后,关闭连接
				if (con != null) {
					con.rollback();
					con.close();
				}
			} catch (Exception e) {
			}
			return false;
		}
		return true;
	}

	/**
	 * 锁表操作
	 */
	public boolean pareLockData(Connection conn, String tOperatedNo,
			String tLockModule) {
		String tMakeDate = PubFun.getCurrentDate();
		String tMakeTime = PubFun.getCurrentTime();
		try {
			LockAppGroupSchema tLockAppGroupSchema = new LockAppGroupSchema();
			LockAppGroupSet tLockAppGroupSet = new LockAppGroupSet();

			// 获取当前业务模块所属的并发控制组.
			LockConfigSet tLockConfigSet = new LockConfigSet();
			tLockConfigSet = this.getLockConfig(conn, tLockModule);
			if (tLockConfigSet.size() <= 0) {
				// 没有为模块配置并发控制组.不做锁定,直接返回true;
				logger.debug("没有配置并发控制组,不做锁定");
				return true;
			}
			for (int i = 1; i <= tLockConfigSet.size(); i++) {
				LockAppGroupDB tLockAppGroupDB = new LockAppGroupDB(conn);
				tLockAppGroupDB.setOperatedNo(tOperatedNo);
				tLockAppGroupDB.setLockGroup(tLockConfigSet.get(i)
						.getLockGroup());
				tLockAppGroupDB.setState("1");
				tLockAppGroupDB.setOperator(mOpeartor);
				tLockAppGroupDB.setReason(tLockModule);
				tLockAppGroupDB.setMakeDate(tMakeDate);
				tLockAppGroupDB.setMakeTime(tMakeTime);
				tLockAppGroupDB.setModifyDate(tMakeDate);
				tLockAppGroupDB.setModifyTime(tMakeTime);
				if (!tLockAppGroupDB.insert()) {//数据插入操作
					CError.buildErr(this, "业务号" + tOperatedNo + "所属业务组"
							+ tLockConfigSet.get(i).getLockGroup()
							+ "当前被锁定,请稍后重试!");
					// 插入失败,认为已经被锁定
					// 在外面做连接关闭和回滚操作
					// 清除所有和tLockModule 有关的数据
					//使用hashtable
					Enumeration eKey=mLockHashtable.keys(); 
					while(eKey.hasMoreElements()) 
					{ 
						String key=(String)eKey.nextElement();
						String tValue = (String)mLockHashtable.get(key);
						if ((tValue).equals(tLockModule + tOperatedNo)) {
							mLockHashtable.remove(key);
						}
					}
					return false;
				}
		// 锁定成功,将锁定成功的业务号和业务组保存起来,便于后面解锁
				this.mLockHashtable.put(tOperatedNo + ":" + tLockConfigSet.get(i).getLockGroup(), tLockModule + tOperatedNo);
			}
		} catch (Exception ex) {
			CError.buildErr(this, ex.toString());
			return false;
		}
		return true;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值