我把程序贴上来大家看看。
private BOPrimaryKeyInfo getNextPrimaryKey(int entity, int range, String tableName, String keyColName) throws DAOException {
Vector param = new Vector(5);
param.addElement(new Integer(entity));
if (range < 0)
throw new RuntimeException("Can't specify a negative range : " + range);
int nextPrimaryKey = 1;
Transaction tx = null;
DAOResult rs = null;
try {
if (tm == null) {
logger.debug("tm is null");
tx = TransactionManager.getTransaction();
tx.begin();
} else {
logger.debug("tm is not null");
tx = TransactionManager.getTxTransaction();
}
logger.debug("before rs");
rs = this.executeQuery(SELECT_AND_LOCK_PRIMARY_KEY_TABLE, param, tx);
logger.debug("after rs");
if (rs.next()) {
nextPrimaryKey = rs.getInt(1);
if (!rs.wasNull()) {
param.removeAllElements();
int newNextPrimaryKey = nextPrimaryKey + range;
param.addElement(new Integer(newNextPrimaryKey));
param.addElement(new Integer(entity));
logger.debug("before update");
executeUpdate(UPDATE_PRIMARY_KEY_TABLE, param, tx);
logger.debug("after update");
if (tm == null)
tx.commit();
logger.debug("before return BOPrimaryKeyInfo entity_key");
return new BOPrimaryKeyInfo(entity, tableName, keyColName, nextPrimaryKey);
} else {
throw new DAOException("error happened when try to get primary key range is available");
}
}
} catch (Exception ex) {
if (tm == null && tx != null)
tx.rollback();
throw new DAOException("error happened when try to update primary key range is new " + ex);
} finally {
if (rs != null)
rs.close();
if (tx != null)
tx.close();
}
在UPDATE后tx commit报错了,after update这行日志打出来了,但before return BOPrimaryKeyInfo entity_key这行日志没有打出来。回滚也失败了,日志报Cannot call Connection.rollback in distributed transaction. Transaction Manager will commit the resource manager when the distributed transaction is committed。谁能帮忙看看吗
|