Manage left-over objects across management server sessions

This commit is contained in:
Kelven Yang 2011-01-28 17:49:08 -08:00
parent ec98a539b4
commit c51e68a2d5
4 changed files with 54 additions and 33 deletions

View File

@ -66,6 +66,11 @@ public class StackMaid {
pop(msid_setby_manager, savePoint);
}
public void pop() {
if(currentSeq > 0)
pop(currentSeq -1);
}
/**
* must be called within thread context
* @param currentMsid
@ -78,12 +83,6 @@ public class StackMaid {
}
public void exitCleanup() {
/*
assert(msid_setby_manager != 0) : "Fatal, make sure StackMaidManager is loaded";
if(msid_setby_manager == 0)
s_logger.error("Fatal, make sure StackMaidManager is loaded");
*/
exitCleanup(msid_setby_manager);
}

View File

@ -76,7 +76,7 @@ public class StackMaidManagerImpl implements StackMaidManager {
public void reallyRun() {
try {
Date cutTime = new Date(DateUtil.currentGMTTime().getTime() - 3600000);
Date cutTime = new Date(DateUtil.currentGMTTime().getTime() - 7200000);
List<StackMaidVO> l = _maidDao.listLeftoversByCutTime(cutTime);
cleanupLeftovers(l);
} catch(Throwable e) {

View File

@ -40,39 +40,55 @@ public class StackMaidDaoImpl extends GenericDaoBase<StackMaidVO, Long> implemen
@DB
public void pushCleanupDelegate(long msid, int seq, String delegateClzName, Object context) {
StackMaidVO delegateItem = new StackMaidVO();
delegateItem.setMsid(msid);
delegateItem.setThreadId(Thread.currentThread().getId());
delegateItem.setSeq(seq);
delegateItem.setDelegate(delegateClzName);
delegateItem.setContext(SerializerHelper.toSerializedStringOld(context));
delegateItem.setCreated(DateUtil.currentGMTTime());
super.persist(delegateItem);
Transaction txn = Transaction.open(Transaction.CLOUD_DB);
try {
StackMaidVO delegateItem = new StackMaidVO();
delegateItem.setMsid(msid);
delegateItem.setThreadId(Thread.currentThread().getId());
delegateItem.setSeq(seq);
delegateItem.setDelegate(delegateClzName);
delegateItem.setContext(SerializerHelper.toSerializedStringOld(context));
delegateItem.setCreated(DateUtil.currentGMTTime());
super.persist(delegateItem);
} finally {
txn.close();
}
}
@DB
public StackMaidVO popCleanupDelegate(long msid) {
SearchCriteria<StackMaidVO> sc = popSearch.create();
sc.setParameters("msid", msid);
sc.setParameters("threadId", Thread.currentThread().getId());
Filter filter = new Filter(StackMaidVO.class, "seq", false, 0L, (long)1);
List<StackMaidVO> l = listIncludingRemovedBy(sc, filter);
if(l != null && l.size() > 0) {
expunge(l.get(0).getId());
return l.get(0);
}
Transaction txn = Transaction.open(Transaction.CLOUD_DB);
try {
SearchCriteria<StackMaidVO> sc = popSearch.create();
sc.setParameters("msid", msid);
sc.setParameters("threadId", Thread.currentThread().getId());
Filter filter = new Filter(StackMaidVO.class, "seq", false, 0L, (long)1);
List<StackMaidVO> l = listIncludingRemovedBy(sc, filter);
if(l != null && l.size() > 0) {
expunge(l.get(0).getId());
return l.get(0);
}
} finally {
txn.close();
}
return null;
}
@DB
public void clearStack(long msid) {
SearchCriteria<StackMaidVO> sc = clearSearch.create();
sc.setParameters("msid", msid);
expunge(sc);
Transaction txn = Transaction.open(Transaction.CLOUD_DB);
try {
SearchCriteria<StackMaidVO> sc = clearSearch.create();
sc.setParameters("msid", msid);
expunge(sc);
} finally {
txn.close();
}
}
@DB
@ -80,7 +96,7 @@ public class StackMaidDaoImpl extends GenericDaoBase<StackMaidVO, Long> implemen
List<StackMaidVO> l = new ArrayList<StackMaidVO>();
String sql = "select * from stack_maid where msid=? order by msid asc, thread_id asc, seq desc";
Transaction txn = Transaction.currentTxn();;
Transaction txn = Transaction.open(Transaction.CLOUD_DB);
PreparedStatement pstmt = null;
try {
pstmt = txn.prepareAutoCloseStatement(sql);
@ -94,6 +110,8 @@ public class StackMaidDaoImpl extends GenericDaoBase<StackMaidVO, Long> implemen
s_logger.error("unexcpected exception " + e.getMessage(), e);
} catch (Throwable e) {
s_logger.error("unexcpected exception " + e.getMessage(), e);
} finally {
txn.close();
}
return l;
}
@ -104,7 +122,7 @@ public class StackMaidDaoImpl extends GenericDaoBase<StackMaidVO, Long> implemen
List<StackMaidVO> l = new ArrayList<StackMaidVO>();
String sql = "select * from stack_maid where created < ? order by msid asc, thread_id asc, seq desc";
Transaction txn = Transaction.currentTxn();;
Transaction txn = Transaction.open(Transaction.CLOUD_DB);
PreparedStatement pstmt = null;
try {
pstmt = txn.prepareAutoCloseStatement(sql);
@ -119,6 +137,8 @@ public class StackMaidDaoImpl extends GenericDaoBase<StackMaidVO, Long> implemen
s_logger.error("unexcpected exception " + e.getMessage(), e);
} catch (Throwable e) {
s_logger.error("unexcpected exception " + e.getMessage(), e);
} finally {
txn.close();
}
return l;
}

View File

@ -88,7 +88,9 @@ public class Transaction {
public static Transaction currentTxn() {
Transaction txn = tls.get();
assert txn != null : "No Transaction on stack. Did you mark the method with @DB?";
assert checkAnnotation(3, txn) : "Did you even read the guide to use Transaction...IOW...other people's code? Try method can't be private. What about @DB? hmmm... could that be it? " + txn.toString();
// loosen the requirement to let people use explicit transaction management (i.e., in Unit tests)
// assert checkAnnotation(3, txn) : "Did you even read the guide to use Transaction...IOW...other people's code? Try method can't be private. What about @DB? hmmm... could that be it? " + txn.toString();
return txn;
}