avoid mysql lock-promotion situation.

This commit is contained in:
Kelven Yang 2014-03-15 12:56:19 -07:00
parent 0c1b6b44a8
commit de252adadf
2 changed files with 34 additions and 30 deletions

View File

@ -899,7 +899,8 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
@Override
public boolean updateState(Status oldStatus, Event event, Status newStatus, Host vo, Object data) {
HostVO host = findById(vo.getId());
// lock target row from beginning to avoid lock-promotion caused deadlock
HostVO host = lockRow(vo.getId(), true);
if (host == null) {
if (event == Event.Remove && newStatus == Status.Removed) {
host = findByIdIncludingRemoved(vo.getId());

View File

@ -425,18 +425,21 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
}
@SuppressWarnings("unchecked")
Pair<Long, Long> hosts = (Pair<Long, Long>)opaque;
Pair<Long, Long> hosts = (Pair<Long,Long>)opaque;
Long newHostId = hosts.second();
VMInstanceVO vmi = (VMInstanceVO)vm;
Long oldHostId = vmi.getHostId();
Long oldUpdated = vmi.getUpdated();
Date oldUpdateDate = vmi.getUpdateTime();
if (newState.equals(oldState) && newHostId != null && newHostId.equals(oldHostId)) {
if ( newState.equals(oldState) && newHostId != null && newHostId.equals(oldHostId) ) {
// state is same, don't need to update
return true;
}
// lock the target row at beginning to avoid lock-promotion caused deadlock
lockRow(vm.getId(), true);
SearchCriteria<VMInstanceVO> sc = StateChangeSearch.create();
sc.setParameters("id", vmi.getId());
sc.setParameters("states", oldState);