diff --git a/examples/src/main/java/org/neo4j/docs/driver/TransactionMetadataConfigExample.java b/examples/src/main/java/org/neo4j/docs/driver/TransactionMetadataConfigExample.java new file mode 100644 index 0000000000..1c13f650f3 --- /dev/null +++ b/examples/src/main/java/org/neo4j/docs/driver/TransactionMetadataConfigExample.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) "Neo4j" + * Neo4j Sweden AB [http://neo4j.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.HashMap; +import java.util.Map; + +import org.neo4j.driver.Session; +import org.neo4j.driver.TransactionConfig; + +import static org.neo4j.driver.Values.parameters; +import static org.neo4j.driver.Values.value; + +public class TransactionMetadataConfigExample extends BaseApplication +{ + public TransactionMetadataConfigExample( String uri, String user, String password ) + { + super( uri, user, password ); + } + + // tag::transaction-metadata-config[] + public void addPerson( final String name ) + { + try ( Session session = driver.session() ) + { + Map transactionMetadata = new HashMap<>(); + transactionMetadata.put( "applicationId", value( "123" ) ); + + TransactionConfig txConfig = TransactionConfig.builder() + .withMetadata( transactionMetadata ).build(); + session.writeTransaction( tx -> + { + tx.run( "CREATE (a:Person {name: $name})", parameters( "name", name ) ); + return 1; + }, txConfig ); + } + } + // end::transaction-metadata-config[] +} diff --git a/examples/src/main/java/org/neo4j/docs/driver/TransactionTimeoutConfigExample.java b/examples/src/main/java/org/neo4j/docs/driver/TransactionTimeoutConfigExample.java new file mode 100644 index 0000000000..9d7b4fa3e4 --- /dev/null +++ b/examples/src/main/java/org/neo4j/docs/driver/TransactionTimeoutConfigExample.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) "Neo4j" + * Neo4j Sweden AB [http://neo4j.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.time.Duration; + +import org.neo4j.driver.Session; +import org.neo4j.driver.TransactionConfig; + +import static org.neo4j.driver.Values.parameters; + +public class TransactionTimeoutConfigExample extends BaseApplication +{ + public TransactionTimeoutConfigExample( String uri, String user, String password ) + { + super( uri, user, password ); + } + + // tag::transaction-timeout-config[] + public void addPerson( final String name ) + { + try ( Session session = driver.session() ) + { + TransactionConfig txConfig = TransactionConfig.builder() + .withTimeout( Duration.ofSeconds( 5 ) ).build(); + session.writeTransaction( tx -> + { + tx.run( "CREATE (a:Person {name: $name})", parameters( "name", name ) ); + return 1; + }, txConfig ); + } + } + // end::transaction-timeout-config[] +} diff --git a/examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java b/examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java index e458c97b17..f584a7ff79 100644 --- a/examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java +++ b/examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java @@ -442,6 +442,34 @@ void testShouldRunTransactionFunctionExample() throws Exception } } + @Test + void testShouldConfigureTransactionTimeoutExample() throws Exception + { + // Given + try ( TransactionTimeoutConfigExample example = new TransactionTimeoutConfigExample( uri, USER, PASSWORD ) ) + { + // When + example.addPerson( "Alice" ); + + // Then + assertThat( personCount( "Alice" ), greaterThan( 0 ) ); + } + } + + @Test + void testShouldConfigureTransactionMetadataExample() throws Exception + { + // Given + try ( TransactionMetadataConfigExample example = new TransactionMetadataConfigExample( uri, USER, PASSWORD ) ) + { + // When + example.addPerson( "Alice" ); + + // Then + assertThat( personCount( "Alice" ), greaterThan( 0 ) ); + } + } + @Test void testShouldRunAsyncTransactionFunctionExample() throws Exception {