在shark的安装路径doc/HowTo/how_to.html 下面文档
Client interface
How to use Shark library
Example 8. Using external transactions
Each method of Shark API invocation presents separate transaction: engine internally creates, uses, optionally commits, and eventually releases transaction. This means that even quite simple code utilizing Shark may unknowingly use many transactions.
Sometimes, it is required to do things a differently, therefore SharkTransaction is introduced. An application('s developer) may choose to use external transactions for number of reasons including usage of the same database to store application (non work-flow related) data, to avoid constantly creating/discarding transactions, ...
Of course, this approach comes with a price: you must obey the rules of usage. Transactions are created calling Shark.getInstance().createTransaction();, just before you release one, your application must call Shark.getInstance().unlockProcesses(st); notifying Shark to do it's internal bookkeeping. If anything goes awry, you must catch a Throwable and call Shark.getInstance().emptyCaches(st);. Yes, you've read it right even Error's must be caught, otherwise you'll leave the engine in undefined state.
Here is the variable setting example modified to use a single transaction.

/**//*
SharkConnection sConn;
String activityId;
String vName;
String vValue;
*/
SharkTransaction st = Shark.getInstance().createTransaction();

try ...{
WfAssignment a = null;
WfAssignment[] ar = sConn.getResourceObject(st)
.get_sequence_work_item(st, 0);

for (int i = 0; i < ar.length; ++i) ...{

if (activityId.equals(ar[i].activity(st).key(st))) ...{
a = ar[i];
break;
}
}
if (null == a) throw new BaseException("Activity:"
+ activityId
+ " not found in "
+ sConn.getResourceObject(st)
.resource_key(st)
+ "'s worklist");
if (!a.get_accepted_status(st)) throw new BaseException("I don't own activity "
+ activityId);
Map _m = new HashMap();
WfActivity activity = a.activity(st);
Object c = activity.process_context(st).get(vName);

if (c instanceof Long) ...{
c = new Long(vValue);

} else ...{
c = vValue;
}
_m.put(vName, c);
activity.set_result(st, _m);
activity.complete(st);
st.commit();

} catch (Throwable t) ...{
Shark.getInstance().emptyCaches(st);
st.rollback();
if (t instanceof RootException)
throw (RootException) t;
else
throw new RootException(t);

} finally ...{

try ...{
Shark.getInstance().unlockProcesses(st);

} catch (Exception _) ...{}
st.release();
}

/**//*
SharkConnection sConn;
String activityId;
String vName;
String vValue;
*/
SharkTransaction st = Shark.getInstance().createTransaction();
try ...{
WfAssignment a = null;
WfAssignment[] ar = sConn.getResourceObject(st)
.get_sequence_work_item(st, 0);
for (int i = 0; i < ar.length; ++i) ...{
if (activityId.equals(ar[i].activity(st).key(st))) ...{
a = ar[i];
break;
}
}
if (null == a) throw new BaseException("Activity:"
+ activityId
+ " not found in "
+ sConn.getResourceObject(st)
.resource_key(st)
+ "'s worklist");
if (!a.get_accepted_status(st)) throw new BaseException("I don't own activity "
+ activityId);
Map _m = new HashMap();
WfActivity activity = a.activity(st);
Object c = activity.process_context(st).get(vName);
if (c instanceof Long) ...{
c = new Long(vValue);
} else ...{
c = vValue;
}
_m.put(vName, c);
activity.set_result(st, _m);
activity.complete(st);
st.commit();
} catch (Throwable t) ...{
Shark.getInstance().emptyCaches(st);
st.rollback();
if (t instanceof RootException)
throw (RootException) t;
else
throw new RootException(t);
} finally ...{
try ...{
Shark.getInstance().unlockProcesses(st);
} catch (Exception _) ...{}
st.release();
}

179

被折叠的 条评论
为什么被折叠?



