Skip to content

Commit

Permalink
feat: Implement Connection Network Timeout. (#564)
Browse files Browse the repository at this point in the history
The timeout value applies to individual server responses, not the JDBC API calls. Any response from the server, including the `NOOP` chunk, interrupts the idle period.

When initializing a new Bolt connection, the Neo4j server may supply a [connection.recv_timeout_seconds](https://neo4j.com/docs/bolt/current/appendix/connection-hints/#hint-recv-timeout-seconds) connection hint that defines the amount of time for which the connection may remain idle. When the network timeout value is set to `0` and the hint value is available, the latter is used as a default.

This update also fixes an issue when database name would not be supplied in the `BEGIN` message for explicit transaction. Also, the commit handler has been updated to handle a missing bookmark.

Closes #233.
  • Loading branch information
injectives authored Mar 13, 2024
1 parent d85a767 commit 39549b4
Show file tree
Hide file tree
Showing 43 changed files with 1,295 additions and 94 deletions.
109 changes: 109 additions & 0 deletions neo4j-jdbc-it/neo4j-jdbc-stub-it/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2023-2024 "Neo4j,"
Neo4j Sweden AB [https://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
https://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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-it</artifactId>
<version>6.0.0-SNAPSHOT</version>
</parent>
<artifactId>neo4j-jdbc-stub-it</artifactId>

<name>Neo4j JDBC Driver Stub IT</name>
<description>Integration tests using the stub server.</description>

<properties>
<sonar.coverage.jacoco.xmlReportPaths>${basedir}/../../${aggregate.report.dir}</sonar.coverage.jacoco.xmlReportPaths>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-translator-impl</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>neo4j</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>native-image</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>${native-maven-plugin.version}</version>
<extensions>true</extensions>
<configuration>
<metadataRepository>
<enabled>true</enabled>
</metadataRepository>
</configuration>
<executions>
<execution>
<id>test-native</id>
<goals>
<goal>test</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
/*
* Copyright (c) 2023-2024 "Neo4j,"
* Neo4j Sweden AB [https://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
*
* https://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.jdbc.stub.it;

import java.sql.SQLException;

import org.junit.jupiter.api.Test;
import org.neo4j.jdbc.stub.it.server.IntegrationTestBase;
import org.neo4j.jdbc.stub.it.server.StubScript;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class ConnectionIT extends IntegrationTestBase {

@Test
@StubScript(path = "reader_tx_with_mode.script")
void shouldSendReadMode() throws SQLException {
try (var connection = getConnection(); var statement = connection.createStatement()) {
connection.setReadOnly(true);
var result = statement.executeQuery("RETURN 1 as n");
while (result.next()) {
assertThat(result.getInt(1)).isEqualTo(1);
}
}

verifyStubServer();
}

@Test
@StubScript(path = "writer_tx_with_begin_1_delay.script")
void shouldThrowOnBeginTimeout() throws SQLException {
try (var connection = getConnection(); var statement = connection.createStatement()) {
connection.setNetworkTimeout(null, 100);
assertThatThrownBy(() -> statement.executeQuery("RETURN 1 as n")).isExactlyInstanceOf(SQLException.class);
}

verifyStubServer();
}

@Test
@StubScript(path = "writer_tx_with_run_1_delay.script")
void shouldThrowOnRunTimeout() throws SQLException {
try (var connection = getConnection(); var statement = connection.createStatement()) {
connection.setNetworkTimeout(null, 100);
assertThatThrownBy(() -> statement.executeQuery("RETURN 1 as n")).isExactlyInstanceOf(SQLException.class);
}

verifyStubServer();
}

@Test
@StubScript(path = "writer_tx_with_pull_1_delay.script")
void shouldThrowOnPullTimeout() throws SQLException {
try (var connection = getConnection(); var statement = connection.createStatement()) {
connection.setNetworkTimeout(null, 100);
assertThatThrownBy(() -> statement.executeQuery("RETURN 1 as n")).isExactlyInstanceOf(SQLException.class);
}

verifyStubServer();
}

@Test
@StubScript(path = "writer_tx_with_commit_1_delay.script")
void shouldThrowOnCommitTimeout() throws SQLException {
try (var connection = getConnection()) {
var statement = connection.createStatement();
connection.setNetworkTimeout(null, 100);
var result = statement.executeQuery("RETURN 1 as n");
while (result.next()) {
assertThat(result.getInt(1)).isEqualTo(1);
}
assertThatThrownBy(statement::close).isExactlyInstanceOf(SQLException.class);
}

verifyStubServer();
}

@Test
@StubScript(path = "writer_tx_with_rollback_1_delay.script")
void shouldThrowOnRollbackTimeout() throws SQLException {
try (var connection = getConnection(); var statement = connection.createStatement()) {
connection.setAutoCommit(false);
connection.setNetworkTimeout(null, 100);
var result = statement.executeQuery("RETURN 1 as n");
while (result.next()) {
assertThat(result.getInt(1)).isEqualTo(1);
}
assertThatThrownBy(connection::rollback).isExactlyInstanceOf(SQLException.class);
}

verifyStubServer();
}

@Test
@StubScript(path = "write_tx_with_1_delays.script")
void shouldExecuteWithDelays() throws SQLException {
try (var connection = getConnection(); var statement = connection.createStatement()) {
connection.setNetworkTimeout(null, 2000);
var result = statement.executeQuery("RETURN 1 as n");
while (result.next()) {
assertThat(result.getInt(1)).isEqualTo(1);
}
}

verifyStubServer();
}

@Test
@StubScript(path = "hint/timeout/writer_tx_with_begin_2_delay.script")
void shouldThrowOnBeginTimeoutWithHint() throws SQLException {
try (var connection = getConnection(); var statement = connection.createStatement()) {
assertThatThrownBy(() -> statement.executeQuery("RETURN 1 as n")).isExactlyInstanceOf(SQLException.class);
}

verifyStubServer();
}

@Test
@StubScript(path = "hint/timeout/writer_tx_with_run_2_delay.script")
void shouldThrowOnRunTimeoutWithHint() throws SQLException {
try (var connection = getConnection(); var statement = connection.createStatement()) {
assertThatThrownBy(() -> statement.executeQuery("RETURN 1 as n")).isExactlyInstanceOf(SQLException.class);
}

verifyStubServer();
}

@Test
@StubScript(path = "hint/timeout/writer_tx_with_pull_2_delay.script")
void shouldThrowOnPullTimeoutWithHint() throws SQLException {
try (var connection = getConnection(); var statement = connection.createStatement()) {
assertThatThrownBy(() -> statement.executeQuery("RETURN 1 as n")).isExactlyInstanceOf(SQLException.class);
}

verifyStubServer();
}

@Test
@StubScript(path = "hint/timeout/writer_tx_with_commit_2_delay.script")
void shouldThrowOnCommitTimeoutWithHint() throws SQLException {
try (var connection = getConnection()) {
var statement = connection.createStatement();
var result = statement.executeQuery("RETURN 1 as n");
while (result.next()) {
assertThat(result.getInt(1)).isEqualTo(1);
}
assertThatThrownBy(statement::close).isExactlyInstanceOf(SQLException.class);
}

verifyStubServer();
}

@Test
@StubScript(path = "hint/timeout/writer_tx_with_rollback_2_delay.script")
void shouldThrowOnRollbackTimeoutWithHint() throws SQLException {
try (var connection = getConnection(); var statement = connection.createStatement()) {
connection.setAutoCommit(false);
var result = statement.executeQuery("RETURN 1 as n");
while (result.next()) {
assertThat(result.getInt(1)).isEqualTo(1);
}
assertThatThrownBy(connection::rollback).isExactlyInstanceOf(SQLException.class);
}

verifyStubServer();
}

@Test
@StubScript(path = "hint/timeout/write_tx_with_1_delays.script")
void shouldExecuteWithDelaysWithHint() throws SQLException {
try (var connection = getConnection(); var statement = connection.createStatement()) {
var result = statement.executeQuery("RETURN 1 as n");
while (result.next()) {
assertThat(result.getInt(1)).isEqualTo(1);
}
}

verifyStubServer();
}

@Test
@StubScript(path = "hint/timeout/writer_tx_with_second_begin_timeout.script")
void shouldApplyDefaultThrowOnBeginTimeouttest() throws SQLException {
try (var connection = getConnection(); var statement = connection.createStatement()) {
connection.setAutoCommit(false);
connection.setNetworkTimeout(null, 4000);
var result = statement.executeQuery("RETURN 1 as n");
while (result.next()) {
assertThat(result.getInt(1)).isEqualTo(1);
}
connection.setNetworkTimeout(null, 0);
assertThatThrownBy(() -> statement.executeQuery("RETURN 1 as n")).isExactlyInstanceOf(SQLException.class);
}

verifyStubServer();
}

@Test
@StubScript(path = "writer_tx_with_2nd_pull_1_delay.script")
void shouldCloseOpenAutoClosablesOnTimeout() throws SQLException {
try (var connection = getConnection();
var statement = connection.createStatement();
var preparedStatement = connection.prepareStatement("RETURN 1");
var callableStatement = connection.prepareCall("RETURN pi()")) {
assertThat(preparedStatement.isClosed()).isFalse();
assertThat(callableStatement.isClosed()).isFalse();
connection.setNetworkTimeout(null, 100);
statement.setFetchSize(1);
statement.closeOnCompletion();
var result = statement.executeQuery("RETURN 1 as n");
result.next();
assertThat(result.getInt(1)).isEqualTo(1);

assertThatThrownBy(result::next).isExactlyInstanceOf(SQLException.class);
assertThat(result.isClosed()).isTrue();
assertThat(statement.isClosed()).isTrue();
assertThat(preparedStatement.isClosed()).isTrue();
assertThat(callableStatement.isClosed()).isTrue();
assertThat(connection.isClosed()).isTrue();
}

verifyStubServer();
}

@Test
@StubScript(path = "server_with_reset_1_delay.script")
void shouldTimeoutOnIsValidWithNetworkTimeoutWithoutTransacton() throws SQLException {
try (var connection = getConnection()) {
connection.setNetworkTimeout(null, 100);
assertThat(connection.isValid(0)).isFalse();
assertThat(connection.isClosed()).isTrue();
}

verifyStubServer();
}

@Test
@StubScript(path = "writer_tx_with_2nd_run_1_delay.script")
void shouldTimeoutOnIsValidOnNetworkTimeoutWithTransacton() throws SQLException {
try (var connection = getConnection(); var statement = connection.createStatement()) {
connection.setAutoCommit(false);
connection.setNetworkTimeout(null, 100);
statement.setFetchSize(1);
var result = statement.executeQuery("RETURN 1 as n");
assertThat(result.next()).isTrue();
assertThat(result.getInt(1)).isEqualTo(1);

assertThat(connection.isValid(0)).isFalse();

assertThat(result.isClosed()).isTrue();
assertThat(statement.isClosed()).isTrue();
assertThat(connection.isClosed()).isTrue();
}

verifyStubServer();
}

}
Loading

0 comments on commit 39549b4

Please sign in to comment.