Skip to content

Add transaction configuration examples #909

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
Jun 8, 2021
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) "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<String,Object> 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[]
}
Original file line number Diff line number Diff line change
@@ -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[]
}
28 changes: 28 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 @@ -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
{
Expand Down