Closed taotao365s closed 7 years ago
这里查询出了所有的超时服务
@Override protected List<Transaction> doFindAllUnmodifiedSince(java.util.Date date) { List<Transaction> transactions = new ArrayList<Transaction>(); Connection connection = null; PreparedStatement stmt = null; try { connection = this.getConnection(); StringBuilder builder = new StringBuilder(); builder.append("SELECT GLOBAL_TX_ID, BRANCH_QUALIFIER, CONTENT,STATUS,TRANSACTION_TYPE,CREATE_TIME,LAST_UPDATE_TIME,RETRIED_COUNT,VERSION"); builder.append(StringUtils.isNotEmpty(domain) ? ",DOMAIN" : ""); builder.append(" FROM " + getTableName() + " WHERE LAST_UPDATE_TIME < ?"); builder.append(StringUtils.isNotEmpty(domain) ? " AND DOMAIN = ?" : ""); stmt = connection.prepareStatement(builder.toString()); stmt.setTimestamp(1, new Timestamp(date.getTime())); if (StringUtils.isNotEmpty(domain)) { stmt.setString(2, domain); } ResultSet resultSet = stmt.executeQuery(); while (resultSet.next()) { byte[] transactionBytes = resultSet.getBytes(3); Transaction transaction = (Transaction) serializer.deserialize(transactionBytes); transaction.changeStatus(TransactionStatus.valueOf(resultSet.getInt(4))); transaction.setLastUpdateTime(resultSet.getDate(7)); transaction.setVersion(resultSet.getLong(9)); transaction.resetRetriedCount(resultSet.getInt(8)); transactions.add(transaction); } } catch (Throwable e) { throw new TransactionIOException(e); } finally { closeStatement(stmt); this.releaseConnection(connection); } return transactions; }
初看了下源码,这里用到了version,乐观锁可以解决大部分并发行为么?
version
protected int doUpdate(Transaction transaction) { Connection connection = null; PreparedStatement stmt = null; java.util.Date lastUpdateTime = transaction.getLastUpdateTime(); long currentVersion = transaction.getVersion(); transaction.updateTime(); transaction.updateVersion(); try { connection = this.getConnection(); StringBuilder builder = new StringBuilder(); builder.append("UPDATE " + getTableName() + " SET " + "CONTENT = ?,STATUS = ?,LAST_UPDATE_TIME = ?, RETRIED_COUNT = ?,VERSION = VERSION+1 WHERE GLOBAL_TX_ID = ? AND BRANCH_QUALIFIER = ? AND VERSION = ?"); builder.append(StringUtils.isNotEmpty(domain) ? " AND DOMAIN = ?" : ""); stmt = connection.prepareStatement(builder.toString()); stmt.setBytes(1, serializer.serialize(transaction)); stmt.setInt(2, transaction.getStatus().getId()); stmt.setTimestamp(3, new Timestamp(transaction.getLastUpdateTime().getTime())); stmt.setInt(4, transaction.getRetriedCount()); stmt.setBytes(5, transaction.getXid().getGlobalTransactionId()); stmt.setBytes(6, transaction.getXid().getBranchQualifier()); stmt.setLong(7, currentVersion); if (StringUtils.isNotEmpty(domain)) { stmt.setString(8, domain); } int result = stmt.executeUpdate(); return result; } catch (Throwable e) { transaction.setLastUpdateTime(lastUpdateTime); transaction.setVersion(currentVersion); throw new TransactionIOException(e); } finally { closeStatement(stmt); this.releaseConnection(connection); } }
@shipengyan 通过乐观锁解决,只有一个节点能获得控制权来处理,其它节点不能处理则忽略
这里查询出了所有的超时服务
初看了下源码,这里用到了
version
,乐观锁可以解决大部分并发行为么?