@Test public void calls_procedures_with_simple_input_type_returning_void() { try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() ) { session.run( "CALL " + procedureNamespace + ".simpleInput00()" ); session.run( "CALL " + procedureNamespace + ".simpleInput01('string')" ); session.run( "CALL " + procedureNamespace + ".simpleInput02(42)" ); session.run( "CALL " + procedureNamespace + ".simpleInput03(42)" ); session.run( "CALL " + procedureNamespace + ".simpleInput04(4.2)" ); session.run( "CALL " + procedureNamespace + ".simpleInput05(true)" ); session.run( "CALL " + procedureNamespace + ".simpleInput06(false)" ); session.run( "CALL " + procedureNamespace + ".simpleInput07({foo:'bar'})" ); session.run( "MATCH (n) CALL " + procedureNamespace + ".simpleInput08(n) RETURN n" ); session.run( "MATCH p=(()-[r]->()) CALL " + procedureNamespace + ".simpleInput09(p) RETURN p" ); session.run( "MATCH ()-[r]->() CALL " + procedureNamespace + ".simpleInput10(r) RETURN r" ); } }
private StatementResult runStatement(@Name("kernelTransaction") String statement, Session session, Map<String, Object> finalParams, boolean read) { if (read) return session.readTransaction((Transaction tx) -> tx.run(statement, finalParams)); else return session.writeTransaction((Transaction tx) -> tx.run(statement, finalParams)); }
private int getTotalRelationships() { Session session = this.driver.session(); int count = session.run("MATCH ()-[r]-() RETURN count(r) AS count").peek().get("count").asInt(); session.close(); return count; // return this.session.run("MATCH ()-[r]-() RETURN count(r) AS count").peek().get("count").asInt(); }
private void createIndexes() { Session session = this.driver.session(); if (session != null) { try (Transaction tx = session.beginTransaction()) { tx.run("CREATE INDEX ON :" + NodeTypes.PHYSICAL_ENTITY + "(id)"); tx.run("CREATE INDEX ON :" + NodeTypes.PHYSICAL_ENTITY + "(name)"); tx.run("CREATE INDEX ON :" + NodeTypes.INTERACTION + "(id)"); tx.run("CREATE INDEX ON :" + NodeTypes.INTERACTION + "(name)"); tx.run("CREATE INDEX ON :" + NodeTypes.XREF + "(id)"); tx.run("CREATE INDEX ON :Tissue(tissue)"); tx.run("CREATE INDEX ON :TimeSeries(timeseries)"); tx.success(); } session.close(); } }
private void closeSession() { try { session.close(); } finally { session = null; } }
Neo4jTransaction(Session session, boolean logCypher) { this.logCypher = logCypher; this.neo4jTransaction = session.beginTransaction(); this.result = new CompletableFuture<>(); }
private void reconnect() { String bookmark = null; if (session != null) { bookmark = session.lastBookmark(); session.close(); } session = driver.session(AccessMode.WRITE, bookmark); StatementResult run = session.run("RETURN 1"); this.version = run.summary().server().version(); run.consume(); }
private Driver stubVersionInAnOpenSession(StatementResult versionMock, Session sessionMock, String value) { Driver driverMock = mock(Driver.class); ResultSummary resultSummary = mock(ResultSummary.class); ServerInfo serverInfo = mock(ServerInfo.class); when(resultSummary.server()).thenReturn(serverInfo); when(serverInfo.version()).thenReturn(value); when(versionMock.summary()).thenReturn(resultSummary); when(sessionMock.isOpen()).thenReturn(true); when(sessionMock.run("RETURN 1")).thenReturn(versionMock); when(driverMock.session(any(), anyString())).thenReturn(sessionMock); return driverMock; }
@Override public void commit() { final boolean canCommit = transactionManager.canCommit(); try { if (canCommit) { LOGGER.debug("Committing native transaction: {}", nativeTransaction); if (nativeTransaction.isOpen()) { nativeTransaction.success(); nativeTransaction.close(); nativeSession.close(); } else { throw new IllegalStateException("Transaction is already closed"); } } } catch (ClientException ce) { closeNativeSessionIfPossible(); if (ce.code().startsWith(NEO_CLIENT_ERROR_SECURITY)) { throw new ConnectionException("Security Error: " + ce.code() + ", " + ce.getMessage(), ce); } throw new CypherException(ce.code(), ce.getMessage(), ce); } catch (Exception e) { closeNativeSessionIfPossible(); throw new TransactionException(e.getLocalizedMessage(), e); } finally { super.commit(); if (canCommit) { transactionManager.bookmark(nativeSession.lastBookmark()); } } }
public Object process(ProceedingJoinPoint pjp) throws Throwable { Object[] args = pjp.getArgs(); int index = -1; Transaction tx = null; MethodSignature signature = (MethodSignature) pjp.getSignature(); Class<?>[] parameterTypes = signature.getParameterTypes(); for(int i=0; i<parameterTypes.length; i++) { if(parameterTypes[i].isAssignableFrom(Transaction.class)) { index = i; tx = (Transaction) args[i]; } } @SuppressWarnings("resource") final Session session = tx == null ? this.driver.session() : null; final Transaction transaction = tx == null ? session.beginTransaction() : tx; if(tx == null) args[index] = transaction; Object obj = pjp.proceed(args); CompletionStage<?> cs = (CompletionStage<?>) obj; if(index != -1) cs.thenAcceptAsync(o -> transaction.commitAsync().thenAcceptAsync(v -> session.closeAsync())) .exceptionally(ex -> { log.error("SQL process has an exception" , ex); transaction.rollbackAsync().thenAcceptAsync(v -> session.closeAsync()); return null; }); return obj; } }
@Before public void setUp() throws Exception { BoltDriver driver = new BoltDriver(nativeDriver); session = new Neo4jSession(new MetaData("org.neo4j.ogm.empty"), driver); when(nativeDriver.session(any(AccessMode.class), anyIterable())).thenReturn(nativeSession); when(nativeSession.beginTransaction().isOpen()).thenReturn(true); when(nativeSession.lastBookmark()).thenReturn("last-bookmark"); }
@Override public boolean isConnected() { return session != null && session.isOpen(); }
private List<BoltResult> executeWithRetry(List<Statement> transactionStatements, BiFunction<Statement, Transaction, BoltResult> biFunction) { return session.writeTransaction(tx -> transactionStatements.stream() .map(transactionStatement -> biFunction.apply(transactionStatement, tx)) .collect(Collectors.toList())); } }
<T> T readTransaction(Function<org.neo4j.driver.v1.Transaction, T> work) { boolean committed = false; try (Session session = driver.session(AccessMode.READ)) { T result = session.readTransaction(tx -> work.apply(tx)); committed = true; return result; } catch (PersistenceException e) { throw e; } catch (RuntimeException e) { throw new PersistenceException(e); } finally { if (logCypher) { if (committed) { System.out.println("READ COMMITED"); } else { System.out.println("READ ROLLED-BACK"); } } } }
private int getTotalXrefRelationships() { Session session = this.driver.session(); int count = session.run("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY + ")-[r:" + RelTypes.XREF + "]->(m:" + NodeTypes.XREF + ") RETURN count(r) AS count").peek().get("count").asInt(); session.close(); return count; // return this.session.run("MATCH (n:" // + NodeTypes.PHYSICAL_ENTITY + ")-[r:" + RelTypes.XREF + "]->(m:" // + NodeTypes.XREF + ") RETURN count(r) AS count").peek().get("count").asInt(); }
/** * Method to annotate Xrefs in the database. * * @param nodeID ID of the node we want to annotate * @param xrefList List containing all the Xref annotations to be added in the database */ @Override public void addXrefs(String nodeID, List<Xref> xrefList) throws BioNetDBException { Session session = this.driver.session(); try (Transaction tx = session.beginTransaction()) { StatementResult xrefNode = getNode(tx, NodeTypes.XREF.toString(), new ObjectMap("id", nodeID)); if (xrefNode.hasNext()) { // Look for the physical entity to which the xref is associated with StatementResult pE = tx.run("MATCH (n:" + NodeTypes.PHYSICAL_ENTITY + ")-[" + RelTypes.XREF + "]->(x:" + NodeTypes.XREF + ") WHERE ID(x) = " + xrefNode.peek().get("ID").toString() + " RETURN ID(n) AS ID, LABELS(n) AS LABELS"); for (Xref x : xrefList) { StatementResult myxref = getOrCreateNode(tx, NodeTypes.XREF.toString(), parseXref(x)); addRelationship(tx, concatenateLabels(pE.peek().get("LABELS")), NodeTypes.XREF.toString(), pE.peek().get("ID").toString(), myxref.peek().get("ID").toString(), RelTypes.XREF); } } else { throw new BioNetDBException("The node to be annotated does not exist in the database."); } tx.success(); } session.close(); }
private void closeSession() { try { session.close(); } finally { session = null; } }
@Override public void beginTransaction() { if ( session == null ) { session = driver.session(); } if ( tx == null ) { tx = session.beginTransaction(); } }