int nbTx = task.txToExecute.length;
PersistenceManager pm = getPM(task, threadId, txId);
try {
res.beginTest();
beginTx(pm, task, threadId, txId);
LinkedIntUserId current=null, next=null;
if (!pctx.linkOnPrepare) {
if (plus > 0) {
//The first transaction creates the additional object
for (int oid = 0; txId == 0 && oid < plus; oid++) {
current = new LinkedIntUserId(oid, "Obj No " + oid, null);
pm.makePersistent(current);
if (pctx.keepOidOnPrepare) {
pctx.oids[oid] = pm.getObjectId(current);
}
}
}
// The other transactions create 'nbCreation' objects
for (int no = 0; no < NB_CREATION; no++) {
int oid = (txId * NB_CREATION) + no + plus;
if (oid < pctx.dbSize) {
current = new LinkedIntUserId(oid, "Obj No " + oid, null);
pm.makePersistent(current);
if (pctx.keepOidOnPrepare) {
pctx.oids[oid] = pm.getObjectId(current);
}
}
}
} else if (!pctx.endPrepare) {
/*
* First : save the oid of objects to be linked between transactions
*/
int oid = 0;
if (plus > 0 && txId == 0) {
current = new LinkedIntUserId(oid, "Obj No " + oid, null);
pm.makePersistent(current);
if (pctx.keepOidOnPrepare) {
pctx.oids[oid] = pm.getObjectId(current);
}
oid++;
next = new LinkedIntUserId(oid, "Obj No " + oid, null);
pm.makePersistent(next);
if (pctx.keepOidOnPrepare) {
pctx.oids[oid] = pm.getObjectId(next);
}
current.setNext(next);
pctx.currentsOnPrepare[txId] = pm.getObjectId(current);
//The first transaction creates the additional object
for (oid = 2; oid < plus; oid++) {
current = new LinkedIntUserId(oid, "Obj No " + oid, null);
pm.makePersistent(current);
if (pctx.keepOidOnPrepare) {
pctx.oids[oid] = pm.getObjectId(current);
}
next.setNext(current);
pm.makePersistent(next);
next = current;
}
}
// The other transactions create 'nbCreation' objects
for (int no = 0; no < NB_CREATION; no++) {
oid = (txId * NB_CREATION) + no + plus;
if (oid < pctx.dbSize) {
current = new LinkedIntUserId(oid, "Obj No " + oid, null);
pm.makePersistent(current);
if (no == 0 && pctx.currentsOnPrepare[txId] == null) {
pctx.currentsOnPrepare[txId] = pm.getObjectId(current);
}
if (pctx.keepOidOnPrepare) {
pctx.oids[oid] = pm.getObjectId(current);
}
if (next != null) {
next.setNext(current);
pm.makePersistent(next);
}
next = current;
}
}
// save the oid of objects to be linked between transactions
pctx.nextsOnPrepare[txId] = pm.getObjectId(next);
} else if (txId == nbTx - 1){ // do it once only
/*
* Second : link the oid
*/
if (debug) {
for (int i=0; i < nbTx; i++) {
logger.log(BasicLevel.DEBUG, "currentsOnPrepare["+i+"]="+ pctx.currentsOnPrepare[i]);
logger.log(BasicLevel.DEBUG, "nextsOnPrepare["+i+"]="+ pctx.nextsOnPrepare[i]);
}
}
// make circular list
next = (LinkedIntUserId) pm.getObjectById(pctx.currentsOnPrepare[0], false);
current = (LinkedIntUserId) pm.getObjectById(pctx.nextsOnPrepare[txId], false);
if (plus != 0) {
if (txId == 0) {
current = (LinkedIntUserId) pm.getObjectById(pctx.nextsOnPrepare[txId], false);
} else {
current = (LinkedIntUserId) pm.getObjectById(pctx.nextsOnPrepare[txId-1], false);
}
}
current.setNext(next);
pm.makePersistent(current);
if (pctx.keepOidOnPrepare) {
int indice = (nbTx * NB_CREATION) - 1;
if (plus != 0) {
indice = (txId * NB_CREATION) + plus - 1;
}
pctx.oids[indice] = pm.getObjectId(current);
}
// link objects between transactions
for (int no = 0; no < nbTx - 1; no++) {
if (pctx.currentsOnPrepare[no+1] != null) {
next = (LinkedIntUserId) pm.getObjectById(pctx.currentsOnPrepare[no+1], false);
current = (LinkedIntUserId) pm.getObjectById(pctx.nextsOnPrepare[no], false);
current.setNext(next);
}
}
// verify all
for (int i=0; i<pctx.oids.length-1; i++) {
Assert.assertNotNull("i="+i, pctx.oids[i]);
Assert.assertNotNull("i="+i+1, pctx.oids[i+1]);
current = (LinkedIntUserId) pm.getObjectById(pctx.oids[i], false);
next = (LinkedIntUserId) pm.getObjectById(pctx.oids[i+1], false);
Assert.assertEquals("name="+current.getName(), current.getNext().getName(), next.getName());
}
next = (LinkedIntUserId) pm.getObjectById(pctx.oids[0], false);
current = (LinkedIntUserId) pm.getObjectById(pctx.oids[pctx.oids.length-1], false);
Assert.assertEquals("name="+current.getName(), current.getNext().getName(), next.getName());
}
commitTx(pm, task, threadId, txId);
res.endTest();