Skip to content

Commit 9bfcd1c

Browse files
author
Zhen
committed
Add examples for 1.5 driver documentation
1 parent 62b25a9 commit 9bfcd1c

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2002-2017 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.docs.driver;
20+
21+
import java.util.concurrent.TimeUnit;
22+
23+
import org.neo4j.driver.v1.AuthTokens;
24+
import org.neo4j.driver.v1.Config;
25+
import org.neo4j.driver.v1.Driver;
26+
import org.neo4j.driver.v1.GraphDatabase;
27+
import org.neo4j.driver.v1.StatementResult;
28+
29+
public class ConfigConnectionPoolExample implements AutoCloseable
30+
{
31+
private final Driver driver;
32+
33+
// tag::config-connection-pool[]
34+
public ConfigConnectionPoolExample( String uri, String user, String password )
35+
{
36+
driver = GraphDatabase.driver( uri, AuthTokens.basic( user, password ), Config.build()
37+
.withMaxConnectionLifetime( 30, TimeUnit.MINUTES )
38+
.withMaxConnectionPoolSize( 50 )
39+
.withConnectionAcquisitionTimeout( 2, TimeUnit.MINUTES )
40+
.toConfig() );
41+
}
42+
// end::config-connection-pool[]
43+
44+
@Override
45+
public void close() throws Exception
46+
{
47+
driver.close();
48+
}
49+
50+
public boolean canConnect()
51+
{
52+
StatementResult result = driver.session().run( "RETURN 1" );
53+
return result.single().get( 0 ).asInt() == 1;
54+
}
55+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2002-2017 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.docs.driver;
20+
21+
import org.neo4j.driver.v1.AuthTokens;
22+
import org.neo4j.driver.v1.Config;
23+
import org.neo4j.driver.v1.Driver;
24+
import org.neo4j.driver.v1.GraphDatabase;
25+
import org.neo4j.driver.v1.StatementResult;
26+
27+
public class ConfigLoadBalancingStrategyExample implements AutoCloseable
28+
{
29+
private final Driver driver;
30+
31+
// tag::config-load-balancing-strategy[]
32+
public ConfigLoadBalancingStrategyExample( String uri, String user, String password )
33+
{
34+
driver = GraphDatabase.driver( uri, AuthTokens.basic( user, password ), Config.build()
35+
.withLoadBalancingStrategy( Config.LoadBalancingStrategy.LEAST_CONNECTED )
36+
.toConfig() );
37+
}
38+
// end::config-load-balancing-strategy[]
39+
40+
@Override
41+
public void close() throws Exception
42+
{
43+
driver.close();
44+
}
45+
46+
public boolean canConnect()
47+
{
48+
StatementResult result = driver.session().run( "RETURN 1" );
49+
return result.single().get( 0 ).asInt() == 1;
50+
}
51+
}

examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,29 @@ public void testShouldRunAutocommitTransactionExample() throws Exception
122122
}
123123
}
124124

125+
@Test
126+
public void testShouldRunConfigConnectionPoolExample() throws Exception
127+
{
128+
// Given
129+
try ( ConfigConnectionPoolExample example = new ConfigConnectionPoolExample( uri, USER, PASSWORD ) )
130+
{
131+
// Then
132+
assertTrue( example.canConnect() );
133+
}
134+
}
135+
136+
@Test
137+
public void testShouldRunConfigLoadBalancingStrategyExample() throws Exception
138+
{
139+
// Given
140+
try ( ConfigLoadBalancingStrategyExample example =
141+
new ConfigLoadBalancingStrategyExample( uri, USER, PASSWORD ) )
142+
{
143+
// Then
144+
assertTrue( example.canConnect() );
145+
}
146+
}
147+
125148
@Test
126149
public void testShouldRunBasicAuthExample() throws Exception
127150
{

0 commit comments

Comments
 (0)