Skip to content

Commit

Permalink
* Use WrappedNodeManager to make sure there is a transactor associate…
Browse files Browse the repository at this point in the history
…d with the current thread,

  creating local transactions if necessary.
  • Loading branch information
hns committed Feb 8, 2008
1 parent c681b2b commit 15f98a9
Showing 1 changed file with 71 additions and 5 deletions.
76 changes: 71 additions & 5 deletions src/helma/objectmodel/db/WrappedNodeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import helma.objectmodel.ObjectNotFoundException;

import java.util.List;
import java.util.Vector;

/**
Expand Down Expand Up @@ -56,11 +55,17 @@ public Node getNode(String id, DbMapping dbmap) {
* @return
*/
public Node getNode(Key key) {
Transactor tx = checkLocalTransactor();
try {
return nmgr.getNode(key);
beginLocalTransaction(tx, "getNode");
Node node = nmgr.getNode(key);
commitLocalTransaction(tx);
return node;
} catch (ObjectNotFoundException x) {
abortLocalTransaction(tx);
return null;
} catch (Exception x) {
abortLocalTransaction(tx);
nmgr.app.logError("Error retrieving Node for " + key, x);
throw new RuntimeException("Error retrieving Node", x);
}
Expand All @@ -75,11 +80,17 @@ public Node getNode(Key key) {
* @return
*/
public Node getNode(Node home, String id, Relation rel) {
Transactor tx = checkLocalTransactor();
try {
return nmgr.getNode(home, id, rel);
beginLocalTransaction(tx, "getNode");
Node node = nmgr.getNode(home, id, rel);
commitLocalTransaction(tx);
return node;
} catch (ObjectNotFoundException x) {
abortLocalTransaction(tx);
return null;
} catch (Exception x) {
abortLocalTransaction(tx);
nmgr.app.logError("Error retrieving Node \"" + id + "\" from " + home, x);
throw new RuntimeException("Error retrieving Node", x);
}
Expand All @@ -94,9 +105,14 @@ public Node getNode(Node home, String id, Relation rel) {
* @return
*/
public SubnodeList getNodes(Node home, Relation rel) {
Transactor tx = checkLocalTransactor();
try {
return nmgr.getNodes(home, rel);
beginLocalTransaction(tx, "getNodes");
SubnodeList list = nmgr.getNodes(home, rel);
commitLocalTransaction(tx);
return list;
} catch (Exception x) {
abortLocalTransaction(tx);
throw new RuntimeException("Error retrieving Nodes", x);
}
}
Expand Down Expand Up @@ -150,9 +166,13 @@ public int countNodes(Node home, Relation rel) {
* @param node
*/
public void deleteNode(Node node) {
Transactor tx = checkLocalTransactor();
try {
beginLocalTransaction(tx, "deleteNode");
nmgr.deleteNode(node);
commitLocalTransaction(tx);
} catch (Exception x) {
abortLocalTransaction(tx);
throw new RuntimeException("Error deleting Node", x);
}
}
Expand Down Expand Up @@ -236,9 +256,14 @@ public String generateID(DbMapping map) {
* Gets the application's root node.
*/
public Node getRootNode() {
Transactor tx = checkLocalTransactor();
try {
return nmgr.getRootNode();
beginLocalTransaction(tx, "getRootNode");
Node node = nmgr.getRootNode();
commitLocalTransaction(tx);
return node;
} catch (Exception x) {
abortLocalTransaction(tx);
throw new RuntimeException(x.toString(), x);
}
}
Expand Down Expand Up @@ -275,4 +300,45 @@ public void logEvent(String msg) {
public DbMapping getDbMapping(String name) {
return nmgr.app.getDbMapping(name);
}

// helper methods to wrap execution inside local transactions

private Transactor checkLocalTransactor() {
Transactor tx = Transactor.getInstance();
if (tx != null) {
// transactor already associated with current thread - return null
return null;
}
return Transactor.getInstance(nmgr);
}

private void beginLocalTransaction(Transactor tx, String name) {
if (tx != null) {
try {
tx.begin(name);
} catch (Exception x) {
nmgr.app.logError("Error in beginLocalTransaction", x);
}
}
}

private void commitLocalTransaction(Transactor tx) {
if (tx != null) {
try {
tx.commit();
} catch (Exception x) {
nmgr.app.logError("Error in commitLocalTransaction", x);
}
}
}

private void abortLocalTransaction(Transactor tx) {
if (tx != null) {
try {
tx.abort();
} catch (Exception x) {
nmgr.app.logError("Error in abortLocalTransaction", x);
}
}
}
}

0 comments on commit 15f98a9

Please sign in to comment.