Skip to content

Add examples for 1.5 driver documentation #412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.docs.driver;

import java.util.concurrent.TimeUnit;

import org.neo4j.driver.v1.AuthTokens;
import org.neo4j.driver.v1.Config;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.StatementResult;

public class ConfigConnectionPoolExample implements AutoCloseable
{
private final Driver driver;

// tag::config-connection-pool[]
public ConfigConnectionPoolExample( String uri, String user, String password )
{
driver = GraphDatabase.driver( uri, AuthTokens.basic( user, password ), Config.build()
.withMaxConnectionLifetime( 30, TimeUnit.MINUTES )
.withMaxConnectionPoolSize( 50 )
.withConnectionAcquisitionTimeout( 2, TimeUnit.MINUTES )
.toConfig() );
}
// end::config-connection-pool[]

@Override
public void close() throws Exception
{
driver.close();
}

public boolean canConnect()
{
StatementResult result = driver.session().run( "RETURN 1" );
return result.single().get( 0 ).asInt() == 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.docs.driver;

import org.neo4j.driver.v1.AuthTokens;
import org.neo4j.driver.v1.Config;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.StatementResult;

public class ConfigLoadBalancingStrategyExample implements AutoCloseable
{
private final Driver driver;

// tag::config-load-balancing-strategy[]
public ConfigLoadBalancingStrategyExample( String uri, String user, String password )
{
driver = GraphDatabase.driver( uri, AuthTokens.basic( user, password ), Config.build()
.withLoadBalancingStrategy( Config.LoadBalancingStrategy.LEAST_CONNECTED )
.toConfig() );
}
// end::config-load-balancing-strategy[]

@Override
public void close() throws Exception
{
driver.close();
}

public boolean canConnect()
{
StatementResult result = driver.session().run( "RETURN 1" );
return result.single().get( 0 ).asInt() == 1;
}
}
23 changes: 23 additions & 0 deletions examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,29 @@ public void testShouldRunAutocommitTransactionExample() throws Exception
}
}

@Test
public void testShouldRunConfigConnectionPoolExample() throws Exception
{
// Given
try ( ConfigConnectionPoolExample example = new ConfigConnectionPoolExample( uri, USER, PASSWORD ) )
{
// Then
assertTrue( example.canConnect() );
}
}

@Test
public void testShouldRunConfigLoadBalancingStrategyExample() throws Exception
{
// Given
try ( ConfigLoadBalancingStrategyExample example =
new ConfigLoadBalancingStrategyExample( uri, USER, PASSWORD ) )
{
// Then
assertTrue( example.canConnect() );
}
}

@Test
public void testShouldRunBasicAuthExample() throws Exception
{
Expand Down