Search in sources :

Example 6 with NotInTransactionException

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"));
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) NotInTransactionException(org.neo4j.graphdb.NotInTransactionException) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 7 with NotInTransactionException

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;
}
Also used : Transaction(javax.transaction.Transaction) NotInTransactionException(org.neo4j.graphdb.NotInTransactionException)

Example 8 with NotInTransactionException

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);
        }
    }
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) NotInTransactionException(org.neo4j.graphdb.NotInTransactionException)

Example 9 with NotInTransactionException

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);
        }
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) NotInTransactionException(org.neo4j.graphdb.NotInTransactionException)

Example 10 with NotInTransactionException

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;
}
Also used : XAResource(javax.transaction.xa.XAResource) Transaction(javax.transaction.Transaction) NotInTransactionException(org.neo4j.graphdb.NotInTransactionException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) XaConnection(org.neo4j.kernel.impl.transaction.xaframework.XaConnection)

Aggregations

NotInTransactionException (org.neo4j.graphdb.NotInTransactionException)24 Transaction (javax.transaction.Transaction)14 SystemException (javax.transaction.SystemException)10 Test (org.junit.Test)6 Node (org.neo4j.graphdb.Node)4 Relationship (org.neo4j.graphdb.Relationship)4 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)4 Vertex (com.tinkerpop.blueprints.Vertex)2 RollbackException (javax.transaction.RollbackException)2 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)2 Transaction (org.neo4j.graphdb.Transaction)2 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)2 HighlyAvailableGraphDatabase (org.neo4j.kernel.ha.HighlyAvailableGraphDatabase)2 Edge (com.tinkerpop.blueprints.Edge)1 XAResource (javax.transaction.xa.XAResource)1 EmbeddedGraphDatabase (org.neo4j.kernel.EmbeddedGraphDatabase)1 XaConnection (org.neo4j.kernel.impl.transaction.xaframework.XaConnection)1 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)1