use of org.neo4j.graphdb.NotInTransactionException in project graphdb by neo4j-attic.
the class TestReadOnlyNeo4j method testReadOnlyOperationsAndNoTransaction.
@Test
public void testReadOnlyOperationsAndNoTransaction() {
GraphDatabaseService db = new EmbeddedGraphDatabase(PATH);
Transaction tx = db.beginTx();
Node node1 = db.createNode();
Node node2 = db.createNode();
Relationship rel = node1.createRelationshipTo(node2, withName("TEST"));
node1.setProperty("key1", "value1");
rel.setProperty("key1", "value1");
tx.success();
tx.finish();
// make sure write operations still throw exception
try {
db.createNode();
fail("Write operation and no transaction should throw exception");
} catch (NotInTransactionException e) {
// good
}
try {
node1.createRelationshipTo(node2, withName("TEST2"));
fail("Write operation and no transaction should throw exception");
} catch (NotInTransactionException e) {
// good
}
try {
node1.setProperty("key1", "value2");
fail("Write operation and no transaction should throw exception");
} catch (NotInTransactionException e) {
// good
}
try {
rel.removeProperty("key1");
fail("Write operation and no transaction should throw exception");
} catch (NotInTransactionException e) {
// good
}
// clear caches and try reads
((AbstractGraphDatabase) db).getConfig().getGraphDbModule().getNodeManager().clearCache();
assertEquals(node1, db.getNodeById(node1.getId()));
assertEquals(node2, db.getNodeById(node2.getId()));
assertEquals(rel, db.getRelationshipById(rel.getId()));
((AbstractGraphDatabase) db).getConfig().getGraphDbModule().getNodeManager().clearCache();
assertEquals("value1", node1.getProperty("key1"));
Relationship loadedRel = node1.getSingleRelationship(DynamicRelationshipType.withName("TEST"), Direction.OUTGOING);
assertEquals(rel, loadedRel);
assertEquals("value1", loadedRel.getProperty("key1"));
}
use of org.neo4j.graphdb.NotInTransactionException in project graphdb by neo4j-attic.
the class PropertyIndexManager method createPropertyIndex.
// concurent transactions may create duplicate keys, oh well
PropertyIndex createPropertyIndex(String key) {
Transaction tx = getTransaction();
if (tx == null) {
throw new NotInTransactionException("Unable to create property index for " + key);
}
TxCommitHook hook = txCommitHooks.get(tx);
if (hook == null) {
hook = new TxCommitHook();
txCommitHooks.put(tx, hook);
}
PropertyIndex index = hook.getIndex(key);
if (index != null) {
return index;
}
int id = (int) idGenerator.nextId(PropertyIndex.class);
index = new PropertyIndex(key, id);
hook.addIndex(index);
persistenceManager.createPropertyIndex(key, id);
return index;
}
use of org.neo4j.graphdb.NotInTransactionException in project graphdb by neo4j-attic.
the class IndexConnectionBroker method delistResourcesForTransaction.
void delistResourcesForTransaction() throws NotInTransactionException {
Transaction tx = this.getCurrentTransaction();
if (tx == null) {
throw new NotInTransactionException();
}
T con = txConnectionMap.get(tx);
if (con != null) {
try {
tx.delistResource(con.getXaResource(), XAResource.TMSUCCESS);
} catch (IllegalStateException e) {
throw new RuntimeException("Unable to delist lucene resource from tx", e);
} catch (SystemException e) {
throw new RuntimeException("Unable to delist lucene resource from tx", e);
}
}
}
use of org.neo4j.graphdb.NotInTransactionException in project graphdb by neo4j-attic.
the class PersistenceManager method delistResourcesForTransaction.
void delistResourcesForTransaction() throws NotInTransactionException {
Transaction tx = this.getCurrentTransaction();
if (tx == null) {
throw new NotInTransactionException();
}
ResourceConnection con = txConnectionMap.get(tx);
if (con != null) {
try {
tx.delistResource(con.getXAResource(), XAResource.TMSUCCESS);
} catch (SystemException e) {
throw new TransactionFailureException("Failed to delist resource '" + con + "' from current transaction.", e);
}
}
}
use of org.neo4j.graphdb.NotInTransactionException in project neo4j-mobile-android by neo4j-contrib.
the class PersistenceManager method getResource.
private NeoStoreTransaction getResource(boolean registerEventHooks) {
NeoStoreTransaction con = null;
Transaction tx = this.getCurrentTransaction();
if (tx == null) {
throw new NotInTransactionException();
}
con = txConnectionMap.get(tx);
if (con == null) {
try {
XaConnection xaConnection = persistenceSource.getXaDataSource().getXaConnection();
XAResource xaResource = xaConnection.getXaResource();
if (!tx.enlistResource(xaResource)) {
throw new ResourceAcquisitionFailedException("Unable to enlist '" + xaResource + "' in " + "transaction");
}
con = persistenceSource.createTransaction(xaConnection);
tx.registerSynchronization(new TxCommitHook(tx));
if (registerEventHooks)
registerTransactionEventHookIfNeeded();
txConnectionMap.put(tx, con);
} catch (javax.transaction.RollbackException re) {
String msg = "The transaction is marked for rollback only.";
throw new ResourceAcquisitionFailedException(msg, re);
} catch (javax.transaction.SystemException se) {
String msg = "TM encountered an unexpected error condition.";
throw new ResourceAcquisitionFailedException(msg, se);
}
}
return con;
}
Aggregations