String password = conf.getString(dbkey + ".password"); Driver driver = GraphDatabase.driver(db, AuthTokens.basic(username, password)); Session session = driver.session(); props.setProperty(DBProperties.SERVER_ROOT_URI, db); keys.generate(Driver.class, dbkey, k -> binder.bind(k).toInstance(driver));
@Override public GraphQueryResult execute(String query, Map<String, Object> statementParameters) { try { Driver driver = GraphDatabase.driver(url, auth); try { try (Session session = driver.session()) { Neo4jBoltBuffer buffer = new Neo4jBoltBuffer(); long startTime = System.currentTimeMillis(); StatementResult statementResult = session.run(query, statementParameters); buffer.addColumns(statementResult.keys()); for (Record record : statementResult.list()) { // Add row buffer.addRow(record.asMap()); } buffer.addResultSummary(statementResult.consume()); long endTime = System.currentTimeMillis(); return new Neo4jBoltQueryResult(endTime - startTime, buffer); } } finally { driver.closeAsync(); } } catch (UnresolvedAddressException e) { throw new ClientException(e.getMessage()); } }
public void close() { if ( driver != null ) { driver.close(); } }
@Override public void run() { driver.close(); } });
@Test public void calls_procedures_with_simple_input_type_returning_record_with_primitive_fields() { try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() ) { assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput11('string') YIELD field04 AS p RETURN p" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput12(42)" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput13(42)" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput14(4.2)" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput15(true)" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput16(false)" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput17({foo:'bar'})" ).single() ) .isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput21()" ).single() ).isNotNull(); } }
@Override public void close() { driver.close(); }
@Test public void shouldStart() { boolean actual = neo4jContainer.isRunning(); assertThat(actual).isTrue(); try (Driver driver = GraphDatabase .driver(neo4jContainer.getBoltUrl(), AuthTokens.basic("neo4j", "password")); Session session = driver.session() ) { long one = session.run("RETURN 1", Collections.emptyMap()).next().get(0).asLong(); assertThat(one).isEqualTo(1L); } catch (Exception e) { fail(e.getMessage()); } }
public void close() { // this.session.close(); this.driver.close(); }
@Test public void calls_simplistic_procedure() { try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() ) { StatementResult result = session.run( "CALL " + procedureNamespace + ".theAnswer()" ); assertThat( result.single().get( "value" ).asLong() ).isEqualTo( 42L ); } }
@Override public synchronized void close() { if (boltDriver != null) { try { LOGGER.info("Shutting down Bolt driver {} ", boltDriver); boltDriver.close(); } catch (Exception e) { throw new RuntimeException(e); } } }
@Test public void shouldDisableAuthentication() { try ( Neo4jContainer neo4jContainer = new Neo4jContainer().withAdminPassword(null); ) { neo4jContainer.start(); try (Driver driver = getDriver(neo4jContainer); Session session = driver.session() ) { long one = session.run("RETURN 1", Collections.emptyMap()).next().get(0).asLong(); assertThat(one).isEqualTo(1L); } } }
@Override public synchronized void close() { if (boltDriver != null) { try { LOGGER.info("Shutting down Bolt driver {} ", boltDriver); boltDriver.close(); } catch (Exception e) { throw new RuntimeException(e); } } }
@Test public void shouldRunEnterprise() { assumeThat(Neo4jContainerTest.class.getResource(ACCEPTANCE_FILE_LOCATION)).isNotNull(); try ( Neo4jContainer neo4jContainer = new Neo4jContainer() .withEnterpriseEdition() .withAdminPassword("Picard123") ) { neo4jContainer.start(); try ( Driver driver = getDriver(neo4jContainer); Session session = driver.session() ) { String edition = session .run("CALL dbms.components() YIELD edition RETURN edition", Collections.emptyMap()) .next().get(0).asString(); assertThat(edition).isEqualTo("enterprise"); } } }
@Override protected void shutDown() { try { if (session != null) session.close(); } catch (Throwable e) { // do nothing } try { if (driver != null) driver.close(); } catch (Throwable e) { // do nothing } }
@Test public void calls_procedures_with_different_modes_returning_void() { try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() ) { session.run( "CALL " + procedureNamespace + ".performsWrites()" ); session.run( "CALL " + procedureNamespace + ".defaultMode()" ); session.run( "CALL " + procedureNamespace + ".readMode()" ); session.run( "CALL " + procedureNamespace + ".writeMode()" ); session.run( "CALL " + procedureNamespace + ".schemaMode()" ); session.run( "CALL " + procedureNamespace + ".dbmsMode()" ); } }
@Override protected void shutDown() { try { if (session != null) session.close(); } catch (Throwable e) { // do nothing } try { if (driver != null) driver.close(); } catch (Throwable e) { // do nothing } }
@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" ); } }
/** * Disconnect from Neo4j, clearing up any session resources, but don't give any output. * Intended only to be used if connect fails. */ void silentDisconnect() { try { if (session != null) { session.close(); } if (driver != null) { driver.close(); } } finally { session = null; driver = null; } }
@Procedure() @Description("apoc.bolt.load(url-or-key, kernelTransaction, params, config) - access to other databases via bolt for read") public Stream<RowResult> load(@Name("url") String url, @Name("kernelTransaction") String statement, @Name(value = "params", defaultValue = "{}") Map<String, Object> params, @Name(value = "config", defaultValue = "{}") Map<String, Object> config) throws URISyntaxException { if (params == null) params = Collections.emptyMap(); if (config == null) config = Collections.emptyMap(); boolean virtual = (boolean) config.getOrDefault("virtual", false); boolean addStatistics = (boolean) config.getOrDefault("statistics", false); boolean readOnly = (boolean) config.getOrDefault("readOnly", true); Config driverConfig = toDriverConfig(config.getOrDefault("driverConfig", map())); UriResolver uri = new UriResolver(url, "bolt"); uri.initialize(); try (Driver driver = GraphDatabase.driver(uri.getConfiguredUri(), uri.getToken(), driverConfig); Session session = driver.session()) { if (addStatistics) return Stream.of(new RowResult(toMap(runStatement(statement, session, params, readOnly).summary().counters()))); else return getRowResultStream(virtual, session, params, statement, readOnly); } catch (Exception e) { throw new RuntimeException("It's not possible to create a connection due to: " + e.getMessage()); } }
@Override protected void doDestroy() { try { driver.close(); } catch (Exception e) { getMonitor().warn("Error closing graph " + createGraph().configuration(), e); } super.doDestroy(); } }