JobStoreTX 控制并发代码
/**
* Execute the given callback having optionally aquired the given lock.
* For <code>JobStoreTX</code>, because it manages its own transactions
* and only has the one datasource, this is the same behavior as
* executeInNonManagedTXLock().
* @param lockName The name of the lock to aquire, for example
* "TRIGGER_ACCESS". If null, then no lock is aquired, but the
* lockCallback is still executed in a transaction.
*
* @see JobStoreSupport#executeInNonManagedTXLock(String, TransactionCallback)
* @see JobStoreCMT#executeInLock(String, TransactionCallback)
* @see JobStoreSupport#getNonManagedTXConnection()
* @see JobStoreSupport#getConnection()
*/
protected Object executeInLock(String lockName, TransactionCallback txCallback) throws JobPersistenceException {
return executeInNonManagedTXLock(lockName, txCallback);
}
使用JobStoreSupport.executeInNonManagedTXLock 实现:
/**
* Execute the given callback having optionally aquired the given lock.
* This uses the non-managed transaction connection.
*
* @param lockName The name of the lock to aquire, for example
* "TRIGGER_ACCESS". If null, then no lock is aquired, but the
* lockCallback is still executed in a non-managed transaction.
*/
protected Object executeInNonManagedTXLock(
String lockName,
TransactionCallback txCallback) throws JobPersistenceException {
boolean transOwner = false;
Connection conn = null;
try {
if (lockName != null) {
// If we aren't using db locks, then delay getting DB connection
// until after acquiring the lock since it isn't needed.
if (getLockHandler().requiresConnection()) {
conn = getNonManagedTXConnection();
}
//获取锁
transOwner = getLockHandler().obtainLock(conn, lockName);
}
if (conn == null) {
conn = getNonManagedTXConnection();
}
//回调需要执行的sql语句如:(更新Trigger为运行中(ACQUIRED),删除执行过的Trigger等)
Object result = txCallback.execute(conn);
//JobStoreTX自身维护事务
commitConnection(conn);
Long sigTime = clearAndGetSignalSchengChangeOnTxCompletion();
if(sigTime != null && sigTime >= 0) {
signalSchengChangeImmediately(sigTime);
}
return result;
} catch (JobPersistenceException e) {
rollbackConnection(conn);
throw e;
} catch (RuntimeException e) {
rollbackConnection(conn);
throw new JobPersistenceException("Unexpected runtime exception: " + e.getMessage(), e);
} finally {
try {
//释放锁
releaseLock(conn, lockName, transOwner);
} finally {
cleanupConnection(conn);
}
}
}
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-29777-17.html
让所谓盟国有底气来对抗中国