Skip to content

Commit 03e2b67

Browse files
authored
Add transaction configuration examples (#909) (#910)
1 parent eff6fde commit 03e2b67

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-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) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.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.HashMap;
22+
import java.util.Map;
23+
24+
import org.neo4j.driver.Session;
25+
import org.neo4j.driver.TransactionConfig;
26+
27+
import static org.neo4j.driver.Values.parameters;
28+
import static org.neo4j.driver.Values.value;
29+
30+
public class TransactionMetadataConfigExample extends BaseApplication
31+
{
32+
public TransactionMetadataConfigExample( String uri, String user, String password )
33+
{
34+
super( uri, user, password );
35+
}
36+
37+
// tag::transaction-metadata-config[]
38+
public void addPerson( final String name )
39+
{
40+
try ( Session session = driver.session() )
41+
{
42+
Map<String,Object> transactionMetadata = new HashMap<>();
43+
transactionMetadata.put( "applicationId", value( "123" ) );
44+
45+
TransactionConfig txConfig = TransactionConfig.builder()
46+
.withMetadata( transactionMetadata ).build();
47+
session.writeTransaction( tx ->
48+
{
49+
tx.run( "CREATE (a:Person {name: $name})", parameters( "name", name ) );
50+
return 1;
51+
}, txConfig );
52+
}
53+
}
54+
// end::transaction-metadata-config[]
55+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.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+
20+
package org.neo4j.docs.driver;
21+
22+
import java.time.Duration;
23+
24+
import org.neo4j.driver.Session;
25+
import org.neo4j.driver.TransactionConfig;
26+
27+
import static org.neo4j.driver.Values.parameters;
28+
29+
public class TransactionTimeoutConfigExample extends BaseApplication
30+
{
31+
public TransactionTimeoutConfigExample( String uri, String user, String password )
32+
{
33+
super( uri, user, password );
34+
}
35+
36+
// tag::transaction-timeout-config[]
37+
public void addPerson( final String name )
38+
{
39+
try ( Session session = driver.session() )
40+
{
41+
TransactionConfig txConfig = TransactionConfig.builder()
42+
.withTimeout( Duration.ofSeconds( 5 ) ).build();
43+
session.writeTransaction( tx ->
44+
{
45+
tx.run( "CREATE (a:Person {name: $name})", parameters( "name", name ) );
46+
return 1;
47+
}, txConfig );
48+
}
49+
}
50+
// end::transaction-timeout-config[]
51+
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,34 @@ void testShouldRunTransactionFunctionExample() throws Exception
442442
}
443443
}
444444

445+
@Test
446+
void testShouldConfigureTransactionTimeoutExample() throws Exception
447+
{
448+
// Given
449+
try ( TransactionTimeoutConfigExample example = new TransactionTimeoutConfigExample( uri, USER, PASSWORD ) )
450+
{
451+
// When
452+
example.addPerson( "Alice" );
453+
454+
// Then
455+
assertThat( personCount( "Alice" ), greaterThan( 0 ) );
456+
}
457+
}
458+
459+
@Test
460+
void testShouldConfigureTransactionMetadataExample() throws Exception
461+
{
462+
// Given
463+
try ( TransactionMetadataConfigExample example = new TransactionMetadataConfigExample( uri, USER, PASSWORD ) )
464+
{
465+
// When
466+
example.addPerson( "Alice" );
467+
468+
// Then
469+
assertThat( personCount( "Alice" ), greaterThan( 0 ) );
470+
}
471+
}
472+
445473
@Test
446474
void testShouldRunAsyncTransactionFunctionExample() throws Exception
447475
{

0 commit comments

Comments
 (0)