Search in sources :

Example 21 with NotInTransactionException

use of org.neo4j.graphdb.NotInTransactionException in project graphdb by neo4j-attic.

the class IndexConnectionBroker method acquireResourceConnection.

public T acquireResourceConnection() {
    T con = null;
    Transaction tx = this.getCurrentTransaction();
    if (tx == null) {
        throw new NotInTransactionException();
    }
    con = txConnectionMap.get(tx);
    if (con == null) {
        try {
            con = (T) newConnection();
            if (!tx.enlistResource(con.getXaResource())) {
                throw new RuntimeException("Unable to enlist '" + con.getXaResource() + "' in " + tx);
            }
            tx.registerSynchronization(new TxCommitHook(tx));
            txConnectionMap.put(tx, con);
        } catch (javax.transaction.RollbackException re) {
            String msg = "The transaction is marked for rollback only.";
            throw new RuntimeException(msg, re);
        } catch (javax.transaction.SystemException se) {
            String msg = "TM encountered an unexpected error condition.";
            throw new RuntimeException(msg, se);
        }
    }
    return con;
}
Also used : Transaction(javax.transaction.Transaction) NotInTransactionException(org.neo4j.graphdb.NotInTransactionException) SystemException(javax.transaction.SystemException)

Example 22 with NotInTransactionException

use of org.neo4j.graphdb.NotInTransactionException in project graphdb by neo4j-attic.

the class LockReleaser method addLockToTransaction.

/**
     * Invoking this method with no transaction running will cause the lock to 
     * be released right away.
     * 
     * @param resource
     *            the resource on which the lock is taken
     * @param type
     *            type of lock (READ or WRITE)
     * @throws NotInTransactionException
     */
public void addLockToTransaction(Object resource, LockType type) throws NotInTransactionException {
    Transaction tx = getTransaction();
    List<LockElement> lockElements = lockMap.get(tx);
    if (lockElements != null) {
        lockElements.add(new LockElement(resource, type));
    } else {
        if (tx == null) {
            // no transaction we release lock right away
            if (type == LockType.WRITE) {
                lockManager.releaseWriteLock(resource, null);
            } else if (type == LockType.READ) {
                lockManager.releaseReadLock(resource, null);
            }
            return;
        }
        lockElements = new ArrayList<LockElement>();
        lockMap.put(tx, lockElements);
        lockElements.add(new LockElement(resource, type));
        // tx was read only
        try {
            tx.registerSynchronization(new ReadOnlyTxReleaser(tx));
        } catch (Exception e) {
            throw new TransactionFailureException("Failed to register lock release synchronization hook", e);
        }
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(javax.transaction.Transaction) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) SystemException(javax.transaction.SystemException) NotInTransactionException(org.neo4j.graphdb.NotInTransactionException)

Example 23 with NotInTransactionException

use of org.neo4j.graphdb.NotInTransactionException in project neo4j by neo4j.

the class TestNeo4jApiExceptions method testNotInTransactionException.

@Test
public void testNotInTransactionException() {
    Node node1 = graph.createNode();
    node1.setProperty("test", 1);
    Node node2 = graph.createNode();
    Node node3 = graph.createNode();
    Relationship rel = node1.createRelationshipTo(node2, MyRelTypes.TEST);
    rel.setProperty("test", 11);
    commit();
    try {
        graph.createNode();
        fail("Create node with no transaction should throw exception");
    } catch (NotInTransactionException e) {
    // good
    }
    try {
        node1.createRelationshipTo(node2, MyRelTypes.TEST);
        fail("Create relationship with no transaction should " + "throw exception");
    } catch (NotInTransactionException e) {
    // good
    }
    try {
        node1.setProperty("test", 2);
        fail("Set property with no transaction should throw exception");
    } catch (NotInTransactionException e) {
    // good
    }
    try {
        rel.setProperty("test", 22);
        fail("Set property with no transaction should throw exception");
    } catch (NotInTransactionException e) {
    // good
    }
    try {
        node3.delete();
        fail("Delete node with no transaction should " + "throw exception");
    } catch (NotInTransactionException e) {
    // good
    }
    try {
        rel.delete();
        fail("Delete relationship with no transaction should " + "throw exception");
    } catch (NotInTransactionException e) {
    // good
    }
    newTransaction();
    assertEquals(node1.getProperty("test"), 1);
    assertEquals(rel.getProperty("test"), 11);
    assertEquals(rel, node1.getSingleRelationship(MyRelTypes.TEST, Direction.OUTGOING));
    node1.delete();
    node2.delete();
    rel.delete();
    node3.delete();
    // Finally
    rollback();
}
Also used : NotInTransactionException(org.neo4j.graphdb.NotInTransactionException) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 24 with NotInTransactionException

use of org.neo4j.graphdb.NotInTransactionException in project neo4j by neo4j.

the class TestReadOnlyNeo4j method testReadOnlyOperationsAndNoTransaction.

@Test
public void testReadOnlyOperationsAndNoTransaction() {
    GraphDatabaseService db = new TestGraphDatabaseFactory().setFileSystem(fs.get()).newImpermanentDatabase(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.close();
    // 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
    }
    Transaction transaction = db.beginTx();
    assertEquals(node1, db.getNodeById(node1.getId()));
    assertEquals(node2, db.getNodeById(node2.getId()));
    assertEquals(rel, db.getRelationshipById(rel.getId()));
    assertThat(node1, inTx(db, hasProperty("key1").withValue("value1")));
    Relationship loadedRel = node1.getSingleRelationship(withName("TEST"), Direction.OUTGOING);
    assertEquals(rel, loadedRel);
    assertThat(loadedRel, inTx(db, hasProperty("key1").withValue("value1")));
    transaction.close();
    db.shutdown();
}
Also used : 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) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) Test(org.junit.Test)

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