Search in sources :

Example 41 with KernelTransaction

use of org.neo4j.kernel.api.KernelTransaction in project neo4j by neo4j.

the class Begin method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
    String lineWithoutApp = parser.getLineWithoutApp();
    if (!acceptableText(lineWithoutApp)) {
        out.println("Error: To open a transaction, write BEGIN TRANSACTION");
        return Continuation.INPUT_COMPLETE;
    }
    KernelTransaction tx = currentTransaction(getServer());
    // This is a "begin" app so it will leave a transaction open. Don't close it in here
    getServer().getDb().beginTx();
    Integer txCount = session.getCommitCount();
    int count;
    if (txCount == null) {
        if (tx == null) {
            count = 0;
            out.println("Transaction started");
        } else {
            count = 1;
            out.println("Warning: transaction found that was not started by the shell.");
        }
    } else {
        count = txCount;
        out.println(String.format("Nested transaction started (Tx count: %d)", count + 1));
    }
    session.setCommitCount(++count);
    return Continuation.INPUT_COMPLETE;
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction)

Example 42 with KernelTransaction

use of org.neo4j.kernel.api.KernelTransaction in project neo4j by neo4j.

the class Commit method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
    if (parser.getLineWithoutApp().trim().length() > 0) {
        out.println("Error: COMMIT should  be run without trailing arguments");
        return Continuation.INPUT_COMPLETE;
    }
    Integer txCount = session.getCommitCount();
    KernelTransaction tx = Begin.currentTransaction(getServer());
    if (txCount == null || txCount.equals(0)) {
        if (tx != null) {
            out.println("Warning: committing a transaction not started by the shell");
            txCount = 1;
        } else {
            throw new ShellException("Not in a transaction");
        }
    }
    if (txCount.equals(1)) {
        if (tx == null) {
            throw fail(session, "Not in a transaction");
        }
        try {
            tx.success();
            tx.close();
            session.remove(Variables.TX_COUNT);
            out.println("Transaction committed");
            return Continuation.INPUT_COMPLETE;
        } catch (Exception e) {
            throw fail(session, e.getMessage());
        }
    } else {
        session.set(Variables.TX_COUNT, --txCount);
        out.println(String.format("Nested transaction committed (Tx count: %d)", txCount));
        return Continuation.INPUT_COMPLETE;
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ShellException(org.neo4j.shell.ShellException) ShellException(org.neo4j.shell.ShellException) RemoteException(java.rmi.RemoteException)

Example 43 with KernelTransaction

use of org.neo4j.kernel.api.KernelTransaction in project neo4j by neo4j.

the class Rollback method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
    if (parser.getLineWithoutApp().trim().length() > 0) {
        out.println("Error: ROLLBACK should  be run without trailing arguments");
        return Continuation.INPUT_COMPLETE;
    }
    KernelTransaction tx = Begin.currentTransaction(getServer());
    if (tx == null) {
        throw Commit.fail(session, "Not in a transaction");
    }
    session.remove(Variables.TX_COUNT);
    tx.failure();
    try {
        tx.close();
    } catch (TransactionFailureException e) {
        throw new ShellException(e.getMessage());
    }
    out.println("Transaction rolled back");
    return Continuation.INPUT_COMPLETE;
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) ShellException(org.neo4j.shell.ShellException)

Example 44 with KernelTransaction

use of org.neo4j.kernel.api.KernelTransaction in project neo4j by neo4j.

the class ClusterMembershipChangeIT method discoverClusterMembers.

private List<Object[]> discoverClusterMembers(GraphDatabaseFacade db) throws TransactionFailureException, org.neo4j.kernel.api.exceptions.ProcedureException {
    KernelAPI kernel = db.getDependencyResolver().resolveDependency(KernelAPI.class);
    KernelTransaction transaction = kernel.newTransaction(KernelTransaction.Type.implicit, AnonymousContext.read());
    Statement statement = transaction.acquireStatement();
    // when
    return asList(statement.procedureCallOperations().procedureCallRead(procedureName(GET_SERVERS_V1.fullyQualifiedProcedureName()), new Object[0]));
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Statement(org.neo4j.kernel.api.Statement) KernelAPI(org.neo4j.kernel.api.KernelAPI)

Example 45 with KernelTransaction

use of org.neo4j.kernel.api.KernelTransaction in project neo4j by neo4j.

the class ClusterOverviewIT method clusterOverview.

private List<MemberInfo> clusterOverview(GraphDatabaseFacade db) throws TransactionFailureException, ProcedureException {
    KernelAPI kernel = db.getDependencyResolver().resolveDependency(KernelAPI.class);
    KernelTransaction transaction = kernel.newTransaction(Type.implicit, AnonymousContext.read());
    List<MemberInfo> infos = new ArrayList<>();
    try (Statement statement = transaction.acquireStatement()) {
        RawIterator<Object[], ProcedureException> itr = statement.procedureCallOperations().procedureCallRead(procedureName("dbms", "cluster", ClusterOverviewProcedure.PROCEDURE_NAME), null);
        while (itr.hasNext()) {
            Object[] row = itr.next();
            Object[] addresses = (Object[]) row[1];
            infos.add(new MemberInfo(Arrays.copyOf(addresses, addresses.length, String[].class), Role.valueOf((String) row[2])));
        }
    }
    return infos;
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Statement(org.neo4j.kernel.api.Statement) ArrayList(java.util.ArrayList) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) KernelAPI(org.neo4j.kernel.api.KernelAPI)

Aggregations

KernelTransaction (org.neo4j.kernel.api.KernelTransaction)77 Test (org.junit.Test)37 Statement (org.neo4j.kernel.api.Statement)30 TransactionFailureException (org.neo4j.kernel.api.exceptions.TransactionFailureException)14 ThreadToStatementContextBridge (org.neo4j.kernel.impl.core.ThreadToStatementContextBridge)14 KernelAPI (org.neo4j.kernel.api.KernelAPI)9 TopLevelTransaction (org.neo4j.kernel.impl.coreapi.TopLevelTransaction)8 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)7 TransactionTerminatedException (org.neo4j.graphdb.TransactionTerminatedException)6 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)6 QueryRegistryOperations (org.neo4j.kernel.api.QueryRegistryOperations)4 SecurityContext (org.neo4j.kernel.api.security.SecurityContext)4 KernelStatement (org.neo4j.kernel.impl.api.KernelStatement)4 RemoteException (java.rmi.RemoteException)3 ExpectedException (org.junit.rules.ExpectedException)3 DependencyResolver (org.neo4j.graphdb.DependencyResolver)3 GraphDatabaseQueryService (org.neo4j.kernel.GraphDatabaseQueryService)3 ShellException (org.neo4j.shell.ShellException)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2