org.neo4j.driver.SessionConfig Java Examples

The following examples show how to use org.neo4j.driver.SessionConfig. 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: Neo4JGraph.java    From neo4j-gremlin-bolt with Apache License 2.0 6 votes vote down vote up
Neo4JSession currentSession() {
    // get current session
    Neo4JSession session = this.session.get();
    if (session == null) {
        // session config
        SessionConfig.Builder config = SessionConfig.builder()
            .withDefaultAccessMode(readonly ? AccessMode.READ : AccessMode.WRITE)
            .withBookmarks(bookmarks);
        // set database if needed
        if (database != null)
            config.withDatabase(database);
        // create new session
        session = new Neo4JSession(this, driver.session(config.build()), vertexIdProvider, edgeIdProvider, readonly);
        // attach it to current thread
        this.session.set(session);
    }
    return session;
}
 
Example #3
Source File: ReactiveDataNeo4jTestIT.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Test
void testTemplate() {

	Mono
		.just(new ExampleEntity("Look, new @ReactiveDataNeo4jTest!"))
		.flatMap(neo4jTemplate::save)
		.as(StepVerifier::create)
		.expectNextCount(1)
		.verifyComplete();

	try (Session session = driver
		.session(SessionConfig.builder().withDefaultAccessMode(AccessMode.READ).build())) {
		long cnt = session.run("MATCH (n:ExampleEntity) RETURN count(n) as cnt").single().get("cnt").asLong();
		assertThat(cnt).isEqualTo(1L);
	}
}
 
Example #4
Source File: Neo4jTransactionManagerTest.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@BeforeEach
void setUp() {

	AtomicBoolean sessionIsOpen = new AtomicBoolean(true);
	AtomicBoolean transactionIsOpen = new AtomicBoolean(true);

	when(driver.session(any(SessionConfig.class))).thenReturn(session);

	when(session.beginTransaction(any(TransactionConfig.class))).thenReturn(transaction);
	doAnswer(invocation -> {
		sessionIsOpen.set(false);
		return null;
	}).when(session).close();
	when(session.isOpen()).thenAnswer(invocation -> sessionIsOpen.get());

	doAnswer(invocation -> {
		transactionIsOpen.set(false);
		return null;
	}).when(transaction).close();
	when(transaction.isOpen()).thenAnswer(invocation -> transactionIsOpen.get());
}
 
Example #5
Source File: TransactionHandlingTest.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Test
void shouldCloseUnmanagedSessionOnError() {

	when(driver.rxSession(any(SessionConfig.class))).thenReturn(session);
	when(session.beginTransaction()).thenReturn(Mono.just(transaction));
	when(transaction.rollback()).thenReturn(Mono.empty());
	when(session.close()).thenReturn(Mono.empty());

	DefaultReactiveNeo4jClient neo4jClient = new DefaultReactiveNeo4jClient(driver);

	Mono<String> sequence = neo4jClient
		.doInQueryRunnerForMono("aDatabase", tx -> Mono.error(new SomeException()));

	StepVerifier.create(sequence)
		.expectError(SomeException.class)
		.verify();

	verify(driver).rxSession(any(SessionConfig.class));
	verify(session).beginTransaction();
	verify(transaction).commit();
	verify(transaction).rollback();
	verify(session).close();
	verifyNoMoreInteractions(driver, session, transaction);
}
 
Example #6
Source File: TransactionHandlingTest.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Test
void shouldCloseUnmanagedSessionOnComplete() {

	when(driver.rxSession(any(SessionConfig.class))).thenReturn(session);
	when(session.beginTransaction()).thenReturn(Mono.just(transaction));
	when(transaction.commit()).thenReturn(Mono.empty());
	when(session.close()).thenReturn(Mono.empty());

	DefaultReactiveNeo4jClient neo4jClient = new DefaultReactiveNeo4jClient(driver);

	Mono<String> sequence = neo4jClient.doInQueryRunnerForMono("aDatabase", tx -> Mono.just("1"));

	StepVerifier.create(sequence)
		.expectNext("1")
		.verifyComplete();

	verify(driver).rxSession(any(SessionConfig.class));
	verify(session).beginTransaction();
	verify(transaction).commit();
	verify(transaction).rollback();
	verify(session).close();
	verifyNoMoreInteractions(driver, session, transaction);
}
 
Example #7
Source File: TransactionHandlingTest.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Test
void shouldCallCloseOnSession() {

	ArgumentCaptor<SessionConfig> configArgumentCaptor = ArgumentCaptor.forClass(SessionConfig.class);

	when(driver.session(any(SessionConfig.class))).thenReturn(session);

	// Make template acquire session
	DefaultNeo4jClient neo4jClient = new DefaultNeo4jClient(driver);
	try (DefaultNeo4jClient.AutoCloseableQueryRunner s = neo4jClient.getQueryRunner("aDatabase")) {
		s.run("MATCH (n) RETURN n");
	}

	verify(driver).session(configArgumentCaptor.capture());
	SessionConfig sessionConfig = configArgumentCaptor.getValue();
	assertThat(sessionConfig.database()).isPresent().contains("aDatabase");

	verify(session).run(any(String.class));
	verify(session).close();

	verifyNoMoreInteractions(driver, session, transaction);
}
 
Example #8
Source File: ReactiveNeo4jTransactionManagerTest.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Test
void shouldSynchronizeWithExternalWithCommit() {

	R2dbcTransactionManager t = new R2dbcTransactionManager(new H2ConnectionFactory(
		H2ConnectionConfiguration.builder().inMemory("test").build()));

	TransactionalOperator transactionalOperator = TransactionalOperator.create(t);

	transactionalOperator
		.execute(transactionStatus -> TransactionSynchronizationManager
			.forCurrentTransaction().doOnNext(tsm -> assertThat(tsm.hasResource(driver)).isFalse())
			.then(retrieveReactiveTransaction(driver, databaseName))
			.flatMap(ignoredNativeTx -> TransactionSynchronizationManager.forCurrentTransaction()
				.doOnNext(tsm -> assertThat(tsm.hasResource(driver)).isTrue()))
		)
		.as(StepVerifier::create)
		.expectNextCount(1L)
		.verifyComplete();

	verify(driver).rxSession(any(SessionConfig.class));

	verify(session).beginTransaction(any(TransactionConfig.class));
	verify(session).close();
	verify(transaction).commit();
	verify(transaction, never()).rollback();
}
 
Example #9
Source File: ReactiveNeo4jTransactionManagerTest.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Test
void shouldUseTxFromNeo4jTxManager() {

	ReactiveNeo4jTransactionManager txManager = new ReactiveNeo4jTransactionManager(driver, ReactiveDatabaseSelectionProvider
		.createStaticDatabaseSelectionProvider(databaseName));
	TransactionalOperator transactionalOperator = TransactionalOperator.create(txManager);

	transactionalOperator
		.execute(transactionStatus -> TransactionSynchronizationManager
			.forCurrentTransaction().doOnNext(tsm -> {
				assertThat(tsm.hasResource(driver)).isTrue();
				transactionStatus.setRollbackOnly();
			}).then(retrieveReactiveTransaction(driver, databaseName))
		)
		.as(StepVerifier::create)
		.expectNextCount(1L)
		.verifyComplete();

	verify(driver).rxSession(any(SessionConfig.class));

	verify(session).beginTransaction(any(TransactionConfig.class));
	verify(session).close();
	verify(transaction).rollback();
	verify(transaction, never()).commit();
}
 
Example #10
Source File: ReactiveNeo4jTransactionManagerTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void shouldSynchronizeWithExternalWithRollback() {

	R2dbcTransactionManager t = new R2dbcTransactionManager(new H2ConnectionFactory(
		H2ConnectionConfiguration.builder().inMemory("test").build()));

	TransactionalOperator transactionalOperator = TransactionalOperator.create(t);

	transactionalOperator
		.execute(transactionStatus -> TransactionSynchronizationManager
			.forCurrentTransaction()
			.doOnNext(tsm -> {
				assertThat(tsm.hasResource(driver)).isFalse();
				transactionStatus.setRollbackOnly();
			})
			.then(retrieveReactiveTransaction(driver, databaseName))
			.flatMap(ignoredNativeTx -> TransactionSynchronizationManager.forCurrentTransaction()
				.doOnNext(tsm -> assertThat(tsm.hasResource(driver)).isTrue()))
		)
		.as(StepVerifier::create)
		.expectNextCount(1L)
		.verifyComplete();

	verify(driver).rxSession(any(SessionConfig.class));

	verify(session).beginTransaction(any(TransactionConfig.class));
	verify(session).close();
	verify(transaction).rollback();
	verify(transaction, never()).commit();
}
 
Example #11
Source File: Neo4jTransactionManagerTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void shouldUseTxFromNeo4jTxManager() {

	Neo4jTransactionManager txManager = new Neo4jTransactionManager(driver, DatabaseSelectionProvider.createStaticDatabaseSelectionProvider(databaseName));
	TransactionTemplate txTemplate = new TransactionTemplate(txManager);

	txTemplate.execute(new TransactionCallbackWithoutResult() {

		@Override
		protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {

			assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
			assertThat(transactionStatus.isNewTransaction()).isTrue();
			assertThat(TransactionSynchronizationManager.hasResource(driver)).isTrue();

			Transaction optionalTransaction = retrieveTransaction(driver, databaseName);
			assertThat(optionalTransaction).isNotNull();

			transactionStatus.setRollbackOnly();
		}
	});

	verify(driver).session(any(SessionConfig.class));

	verify(session).isOpen();
	verify(session).beginTransaction(any(TransactionConfig.class));
	verify(session).close();

	verify(transaction, times(2)).isOpen();
	verify(transaction).rollback();
	verify(transaction).close();
}
 
Example #12
Source File: Neo4jTransactionManagerTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void shouldParticipateInOngoingTransactionWithCommit() throws Exception {

	when(userTransaction.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE,
			Status.STATUS_ACTIVE);

	JtaTransactionManager txManager = new JtaTransactionManager(userTransaction);
	TransactionTemplate txTemplate = new TransactionTemplate(txManager);

	txTemplate.execute(new TransactionCallbackWithoutResult() {

		@Override
		protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {

			assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
			assertThat(transactionStatus.isNewTransaction()).isTrue();
			assertThat(TransactionSynchronizationManager.hasResource(driver)).isFalse();

			Transaction nativeTransaction = retrieveTransaction(driver, databaseName);

			assertThat(nativeTransaction).isNotNull();
			assertThat(TransactionSynchronizationManager.hasResource(driver)).isTrue();
		}
	});

	verify(userTransaction).begin();

	verify(driver).session(any(SessionConfig.class));

	verify(session, times(2)).isOpen();
	verify(session).beginTransaction(any(TransactionConfig.class));
	verify(session).close();

	verify(transaction, times(3)).isOpen();
	verify(transaction).commit();
	verify(transaction).close();
}
 
Example #13
Source File: Neo4jTransactionManagerTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void shouldParticipateInOngoingTransactionWithRollback() throws Exception {

	when(userTransaction.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE,
			Status.STATUS_ACTIVE);

	JtaTransactionManager txManager = new JtaTransactionManager(userTransaction);
	TransactionTemplate txTemplate = new TransactionTemplate(txManager);

	txTemplate.execute(new TransactionCallbackWithoutResult() {

		@Override
		protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {

			assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
			assertThat(transactionStatus.isNewTransaction()).isTrue();
			assertThat(TransactionSynchronizationManager.hasResource(driver)).isFalse();

			Transaction nativeTransaction = retrieveTransaction(driver, databaseName);

			assertThat(nativeTransaction).isNotNull();
			assertThat(TransactionSynchronizationManager.hasResource(driver)).isTrue();

			transactionStatus.setRollbackOnly();
		}
	});

	verify(userTransaction).begin();
	verify(userTransaction).rollback();

	verify(driver).session(any(SessionConfig.class));

	verify(session, times(2)).isOpen();
	verify(session).beginTransaction(any(TransactionConfig.class));
	verify(session).close();

	verify(transaction, times(3)).isOpen();
	verify(transaction).rollback();
	verify(transaction).close();
}
 
Example #14
Source File: ReactiveNeo4jTransactionManagerTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void setUp() {

	when(driver.rxSession(any(SessionConfig.class))).thenReturn(session);
	when(session.beginTransaction(any(TransactionConfig.class))).thenReturn(Mono.just(transaction));
	when(transaction.rollback()).thenReturn(Mono.empty());
	when(transaction.commit()).thenReturn(Mono.empty());
	when(session.close()).thenReturn(Mono.empty());
}
 
Example #15
Source File: ReactiveNeo4jTransactionManagerTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void shouldParticipateInOngoingTransaction() {

	ReactiveNeo4jTransactionManager txManager = new ReactiveNeo4jTransactionManager(driver, ReactiveDatabaseSelectionProvider
		.createStaticDatabaseSelectionProvider(databaseName));
	TransactionalOperator transactionalOperator = TransactionalOperator.create(txManager);

	transactionalOperator
		.execute(outerStatus -> {
			assertThat(outerStatus.isNewTransaction()).isTrue();
			outerStatus.setRollbackOnly();
			return transactionalOperator.execute(innerStatus -> {
				assertThat(innerStatus.isNewTransaction()).isFalse();
				return retrieveReactiveTransaction(driver, databaseName);
			}).then(retrieveReactiveTransaction(driver, databaseName));
		})
		.as(StepVerifier::create)
		.expectNextCount(1L)
		.verifyComplete();

	verify(driver).rxSession(any(SessionConfig.class));

	verify(session).beginTransaction(any(TransactionConfig.class));
	verify(session).close();
	verify(transaction).rollback();
	verify(transaction, never()).commit();
}
 
Example #16
Source File: ReactiveNeo4jTransactionManagerTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void usesBookmarksCorrectly() throws Exception {

	ReactiveNeo4jTransactionManager txManager = new ReactiveNeo4jTransactionManager(driver, ReactiveDatabaseSelectionProvider
		.createStaticDatabaseSelectionProvider(databaseName));

	Neo4jBookmarkManager bookmarkManager = spy(new Neo4jBookmarkManager());
	injectBookmarkManager(txManager, bookmarkManager);

	Bookmark bookmark = new Bookmark() {
		@Override public Set<String> values() {
			return Collections.singleton("blubb");
		}

		@Override public boolean isEmpty() {
			return false;
		}
	};
	when(session.lastBookmark()).thenReturn(bookmark);

	TransactionalOperator transactionalOperator = TransactionalOperator.create(txManager);

	transactionalOperator
		.execute(transactionStatus -> TransactionSynchronizationManager
			.forCurrentTransaction()
			.doOnNext(tsm -> assertThat(tsm.hasResource(driver)).isTrue())
			.then(retrieveReactiveTransaction(driver, databaseName))
		)
		.as(StepVerifier::create)
		.expectNextCount(1L)
		.verifyComplete();

	verify(driver).rxSession(any(SessionConfig.class));
	verify(session).beginTransaction(any(TransactionConfig.class));
	verify(bookmarkManager).getBookmarks();
	verify(session).close();
	verify(transaction).commit();
	verify(bookmarkManager).updateBookmarks(anyCollection(), eq(bookmark));
}
 
Example #17
Source File: Neo4jTransactionManagerTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void usesBookmarksCorrectly() throws Exception {

	when(driver.defaultTypeSystem()).thenReturn(typeSystem);
	when(driver.session(any(SessionConfig.class))).thenReturn(session);
	when(session.beginTransaction(any(TransactionConfig.class))).thenReturn(transaction);
	Bookmark bookmark = new Bookmark() {
		@Override public Set<String> values() {
			return Collections.singleton("blubb");
		}

		@Override public boolean isEmpty() {
			return false;
		}
	};
	when(session.lastBookmark()).thenReturn(bookmark);
	when(transaction.run(anyString(), anyMap())).thenReturn(statementResult);
	when(session.isOpen()).thenReturn(true);
	when(transaction.isOpen()).thenReturn(true, false);

	Neo4jTransactionManager txManager = spy(new Neo4jTransactionManager(driver));
	Neo4jBookmarkManager bookmarkManager = mock(Neo4jBookmarkManager.class);
	injectBookmarkManager(txManager, bookmarkManager);

	TransactionStatus txStatus = txManager.getTransaction(new DefaultTransactionDefinition());

	Neo4jClient client = Neo4jClient.create(driver);
	client.query("RETURN 1").run();

	txManager.commit(txStatus);

	verify(txManager).doBegin(any(), any(TransactionDefinition.class));
	verify(bookmarkManager).getBookmarks();
	verify(txManager).doCommit(any(DefaultTransactionStatus.class));
	verify(bookmarkManager).updateBookmarks(anyCollection(), eq(bookmark));

}
 
Example #18
Source File: TransactionHandlingTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void shouldNotInvokeCloseOnTransaction() {

	AtomicBoolean transactionIsOpen = new AtomicBoolean(true);

	when(driver.session(any(SessionConfig.class))).thenReturn(session);
	when(session.isOpen()).thenReturn(true);
	when(session.beginTransaction(any(TransactionConfig.class))).thenReturn(transaction);
	// Mock closing of the transaction
	doAnswer(invocation -> {
		transactionIsOpen.set(false);
		return null;
	}).when(transaction).close();
	when(transaction.isOpen()).thenAnswer(invocation -> transactionIsOpen.get());

	Neo4jTransactionManager txManager = new Neo4jTransactionManager(driver);
	TransactionTemplate txTemplate = new TransactionTemplate(txManager);

	DefaultNeo4jClient neo4jClient = new DefaultNeo4jClient(driver);
	txTemplate.execute(tx -> {
		try (DefaultNeo4jClient.AutoCloseableQueryRunner s = neo4jClient.getQueryRunner(null)) {
			s.run("MATCH (n) RETURN n");
		}
		return null;
	});

	verify(transaction, times(2)).isOpen();
	verify(transaction).run(anyString());
	// Called by the transaction manager
	verify(transaction).commit();
	verify(transaction).close();
	verify(session).isOpen();
	verify(session).lastBookmark();
	verify(session).close();
	verifyNoMoreInteractions(driver, session, transaction);
}
 
Example #19
Source File: TransactionHandlingTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void shouldNotOpenTransactionsWithoutSubscription() {
	DefaultReactiveNeo4jClient neo4jClient = new DefaultReactiveNeo4jClient(driver);
	neo4jClient.query("RETURN 1").in("aDatabase").fetch().one();

	verify(driver, never()).rxSession(any(SessionConfig.class));
	verifyNoMoreInteractions(driver, session);
}
 
Example #20
Source File: Neo4jClientTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
void verifyDatabaseSelection(String targetDatabase) {

		verify(driver).session(configArgumentCaptor.capture());
		SessionConfig config = configArgumentCaptor.getValue();

		if (targetDatabase != null) {
			assertThat(config.database()).isPresent().contains(targetDatabase);
		} else {
			assertThat(config.database()).isEmpty();
		}
	}
 
Example #21
Source File: ReactiveNeo4jClientTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
void verifyDatabaseSelection(String targetDatabase) {

		verify(driver).rxSession(configArgumentCaptor.capture());
		SessionConfig config = configArgumentCaptor.getValue();

		if (targetDatabase != null) {
			assertThat(config.database()).isPresent().contains(targetDatabase);
		} else {
			assertThat(config.database()).isEmpty();
		}
	}
 
Example #22
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();
        }
    }
}
 
Example #23
Source File: OptimisticLockingIT.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void setup() {
	Session session = driver.session(SessionConfig.defaultConfig());
	Transaction transaction = session.beginTransaction();
	transaction.run("MATCH (n) detach delete n");
	transaction.commit();
	session.close();
}
 
Example #24
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 #25
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 #26
Source File: DataNeo4jTestIT.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void testRepository() {
	ExampleEntity entity = new ExampleEntity("Look, new @DataNeo4jTest!");
	assertThat(entity.getId()).isNull();
	ExampleEntity persistedEntity = this.exampleRepository.save(entity);
	assertThat(persistedEntity.getId()).isNotNull();

	try (Session session = driver
		.session(SessionConfig.builder().withDefaultAccessMode(AccessMode.READ).build())) {
		long cnt = session.run("MATCH (n:ExampleEntity) RETURN count(n) as cnt").single().get("cnt").asLong();
		assertThat(cnt).isEqualTo(1L);
	}
}
 
Example #27
Source File: Neo4jTransactionUtils.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
public static SessionConfig sessionConfig(boolean readOnly, Collection<Bookmark> bookmarks,
	@Nullable String databaseName) {
	SessionConfig.Builder builder = SessionConfig.builder()
		.withDefaultAccessMode(readOnly ? AccessMode.READ : AccessMode.WRITE)
		.withBookmarks(bookmarks);

	if (databaseName != null) {
		builder.withDatabase(databaseName);
	}

	return builder.build();
}
 
Example #28
Source File: RepositoryWithADifferentDatabaseIT.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@BeforeAll
static void createTestDatabase() {

	try (Session session = neo4jConnectionSupport.driverInstance.session(SessionConfig.forDatabase("system"))) {

		session.run("CREATE DATABASE " + TEST_DATABASE_NAME).consume();
	}
}
 
Example #29
Source File: RepositoryWithADifferentDatabaseIT.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@AfterAll
static void dropTestDatabase() {

	try (Session session = neo4jConnectionSupport.driverInstance.session(SessionConfig.forDatabase("system"))) {

		session.run("DROP DATABASE " + TEST_DATABASE_NAME).consume();
	}
}
 
Example #30
Source File: Neo4jTransactionManagerTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void triggerCommitCorrectly() {

	when(driver.defaultTypeSystem()).thenReturn(typeSystem);
	when(driver.session(any(SessionConfig.class))).thenReturn(session);
	when(session.beginTransaction(any(TransactionConfig.class))).thenReturn(transaction);
	when(transaction.run(anyString(), anyMap())).thenReturn(statementResult);
	when(session.isOpen()).thenReturn(true);
	when(transaction.isOpen()).thenReturn(true, false);

	Neo4jTransactionManager txManager = new Neo4jTransactionManager(driver);
	TransactionStatus txStatus = txManager.getTransaction(new DefaultTransactionDefinition());

	Neo4jClient client = Neo4jClient.create(driver);
	client.query("RETURN 1").run();

	txManager.commit(txStatus);

	verify(driver).session(any(SessionConfig.class));

	verify(session).isOpen();
	verify(session).beginTransaction(any(TransactionConfig.class));

	verify(transaction, times(2)).isOpen();
	verify(transaction).commit();
	verify(transaction).close();

	verify(session).close();
}