Java Code Examples for org.neo4j.driver.Driver#session()

The following examples show how to use org.neo4j.driver.Driver#session() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExampleUsingDynamicDatabaseNameTest.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@BeforeAll
static void createDatabase(@Autowired Driver driver) throws IOException {
	try (Session session = driver.session(SessionConfig.forDatabase("system"))) {
		// Corresponds to the mocked user at the top of this class.
		session.run("CREATE DATABASE someMovieEnthusiast");
	}

	try (BufferedReader moviesReader = new BufferedReader(
		new InputStreamReader(ExampleUsingDynamicDatabaseNameTest.class.getResourceAsStream("/movies.cypher")));
		Session session = driver.session(SessionConfig.forDatabase("someMovieEnthusiast"))) {

		session.run("MATCH (n) DETACH DELETE n");
		String moviesCypher = moviesReader.lines().collect(Collectors.joining(" "));
		session.run(moviesCypher);
	}
}
 
Example 2
Source File: ExampleUsingStaticDatabaseNameTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@BeforeAll
static void createDatabase(@Autowired Driver driver) {

	try (Session session = driver.session(SessionConfig.forDatabase("system"))) {
		session.run("CREATE DATABASE movies");
	}
}
 
Example 3
Source File: ExampleUsingMultipleTransactionManagerTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@BeforeAll
static void createDatabase(@Autowired Driver driver) {

	try (Session session = driver.session(SessionConfig.forDatabase("system"))) {
		session.run("CREATE DATABASE movies");
		session.run("CREATE DATABASE otherDb");
	}
}
 
Example 4
Source File: RepositoryIT.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void setup(@Autowired Driver driver) throws IOException {
	try (BufferedReader moviesReader = new BufferedReader(
		new InputStreamReader(this.getClass().getResourceAsStream("/movies.cypher")));
		Session session = driver.session()) {
		session.run("MATCH (n) DETACH DELETE n");
		String moviesCypher = moviesReader.lines().collect(Collectors.joining(" "));
		session.run(moviesCypher);
	}
}
 
Example 5
Source File: Neo4jTransactionManager.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
/**
 * This methods provides a native Neo4j transaction to be used from within a {@link org.neo4j.springframework.data.core.Neo4jClient}.
 * In most cases this the native transaction will be controlled from the Neo4j specific
 * {@link org.springframework.transaction.PlatformTransactionManager}. However, SDN-RX provides support for other
 * transaction managers as well. This methods registers a session synchronization in such cases on the foreign transaction manager.
 *
 * @param driver         The driver that has been used as a synchronization object.
 * @param targetDatabase The target database
 * @return An optional managed transaction or {@literal null} if the method hasn't been called inside
 * an ongoing Spring transaction
 */
public static @Nullable Transaction retrieveTransaction(final Driver driver,
	@Nullable final String targetDatabase) {

	if (!TransactionSynchronizationManager.isSynchronizationActive()) {
		return null;
	}

	// Check whether we have a transaction managed by a Neo4j transaction manager
	Neo4jTransactionHolder connectionHolder = (Neo4jTransactionHolder) TransactionSynchronizationManager
		.getResource(driver);

	if (connectionHolder != null) {
		Transaction optionalOngoingTransaction = connectionHolder.getTransaction(targetDatabase);

		if (optionalOngoingTransaction != null) {
			return optionalOngoingTransaction;
		}

		throw new IllegalStateException(
			formatOngoingTxInAnotherDbErrorMessage(connectionHolder.getDatabaseName(), targetDatabase));
	}

	// Otherwise we open a session and synchronize it.
	Session session = driver.session(defaultSessionConfig(targetDatabase));
	Transaction transaction = session.beginTransaction(TransactionConfig.empty());
	// Manually create a new synchronization
	connectionHolder = new Neo4jTransactionHolder(new Neo4jTransactionContext(targetDatabase), session, transaction);
	connectionHolder.setSynchronizedWithTransaction(true);

	TransactionSynchronizationManager.registerSynchronization(
		new Neo4jSessionSynchronization(connectionHolder, driver));

	TransactionSynchronizationManager.bindResource(driver, connectionHolder);
	return connectionHolder.getTransaction(targetDatabase);
}
 
Example 6
Source File: ExceptionTranslationIT.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@BeforeAll
static void createConstraints(@Autowired Driver driver) {

	try (Session session = driver.session()) {
		session.run("CREATE CONSTRAINT ON (person:SimplePerson) ASSERT person.name IS UNIQUE").consume();
	}
}
 
Example 7
Source File: ExceptionTranslationIT.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@AfterAll
static void dropConstraints(@Autowired Driver driver) {

	try (Session session = driver.session()) {
		session.run("DROP CONSTRAINT ON (person:SimplePerson) ASSERT person.name IS UNIQUE").consume();
	}
}
 
Example 8
Source File: ExceptionTranslationIT.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void clearDatabase(@Autowired Driver driver) {

	try (Session session = driver.session()) {
		session.run("MATCH (n) DETACH DELETE n").consume();
	}
}
 
Example 9
Source File: Neo4jResource.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static void createNodes(Driver driver) {
    try (Session session = driver.session();
            Transaction transaction = session.beginTransaction()) {
        transaction.run("CREATE (f:Framework {name: $name}) - [:CAN_USE] -> (n:Database {name: 'Neo4j'})",
                Values.parameters("name", "Quarkus"));
        transaction.commit();
    }
}
 
Example 10
Source File: Neo4jResource.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static void readNodes(Driver driver) {
    try (Session session = driver.session();
            Transaction transaction = session.beginTransaction()) {
        Result result = transaction
                .run("MATCH (f:Framework {name: $name}) - [:CAN_USE] -> (n) RETURN f, n",
                        Values.parameters("name", "Quarkus"));
        result.forEachRemaining(
                record -> System.out.println(String.format("%s works with %s", record.get("n").get("name").asString(),
                        record.get("f").get("name").asString())));
        transaction.commit();
    }
}
 
Example 11
Source File: Neo4JTestGraphProvider.java    From neo4j-gremlin-bolt with Apache License 2.0 5 votes vote down vote up
@Override
public void clear(Graph graph, Configuration configuration) throws Exception {
    // check graph instance
    if (graph != null) {
        // close graph instance
        graph.close();
    }
    // create driver instance
    Driver driver = Neo4JGraphFactory.createDriverInstance(configuration);
    // session configuration
    SessionConfig.Builder config = SessionConfig.builder();
    // check database is set
    String database = configuration.getString(Neo4JGraphConfigurationBuilder.Neo4JDatabaseConfigurationKey, null);
    if (database != null) {
        // update session config
        config.withDatabase(database);
    }
    // open session
    try (Session session = driver.session(config.build())) {
        // begin transaction
        try (org.neo4j.driver.Transaction transaction = session.beginTransaction()) {
            // delete everything in database
            transaction.run("MATCH (n) DETACH DELETE n");
            // commit
            transaction.commit();
        }
    }
}