Skip to content

Commit

Permalink
Add support for TCP connections to remote H2 instances.
Browse files Browse the repository at this point in the history
Resolves #86.
  • Loading branch information
gregturn committed Aug 14, 2019
1 parent 2dc8cac commit bc43c0c
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/main/java/io/r2dbc/h2/H2ConnectionConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,29 @@ public Builder inMemory(String name) {
return url(String.format("mem:%s", name));
}

/**
* Configure a TCP connection to a remote database.
*
* @param host - hostname of the remote database (can be {@code localhost})
* @param path - path of the database, e.g. {@code ~/test} resolves to {user.home}/test.db
* @return this {@link Builder}
*/
public Builder tcp(String host, String path) {
return url(String.format("tcp://%s/%s", host, path));
}

/**
* Configure a TCP connection to a remote database
*
* @param host - hostname of the remote database (can be {@code localhost})
* @param port - port the database is serving from
* @param path - path of the database, e.g. {@code ~/test} resolves to {user.home}/test.db
* @return this {@link Builder}
*/
public Builder tcp(String host, String port, String path) {
return url(String.format("tcp://%s:%s/%s", host, port, path));
}

/**
* Configure an option that is appended at the end, e.g. {@code DB_CLOSE_DELAY=10}, prefixed with ";".
*
Expand Down
112 changes: 112 additions & 0 deletions src/test/java/io/r2dbc/h2/H2RemoteAccessTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2019 the original author or authors.
*
* 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 io.r2dbc.h2;

import org.h2.tools.Server;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.util.FileSystemUtils;
import reactor.test.StepVerifier;

import java.io.IOException;
import java.nio.file.Paths;
import java.sql.SQLException;

final class H2RemoteAccessTest {

Server server;

@BeforeEach
void startupRemoteDatabase() throws SQLException {
server = Server.createTcpServer("-tcpPort", "9123", "-ifNotExists").start();
}

@AfterEach
void shutdownRemoteDatabase() {
server.stop();
}

@Test
void tcpByUrlWorks() throws IOException {
FileSystemUtils.deleteRecursively(Paths.get(System.getProperty("user.home"), "test.mv.db"));
FileSystemUtils.deleteRecursively(Paths.get(System.getProperty("user.home"), "test.trace.db"));

H2ConnectionConfiguration configuration = H2ConnectionConfiguration.builder()
.url("tcp://localhost:9123/~/test")
.username("sa")
.password("")
.build();

new H2ConnectionFactory(configuration).create()
.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete();

FileSystemUtils.deleteRecursively(Paths.get(System.getProperty("user.home"), "test.mv.db"));
FileSystemUtils.deleteRecursively(Paths.get(System.getProperty("user.home"), "test.trace.db"));
}

@Test
void tcpWithHostnameAndPortWorks() throws IOException {
FileSystemUtils.deleteRecursively(Paths.get(System.getProperty("user.home"), "test.mv.db"));
FileSystemUtils.deleteRecursively(Paths.get(System.getProperty("user.home"), "test.trace.db"));

H2ConnectionConfiguration configuration = H2ConnectionConfiguration.builder()
.tcp("localhost", "9123", "~/test")
.username("sa")
.password("")
.build();

new H2ConnectionFactory(configuration).create()
.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete();

FileSystemUtils.deleteRecursively(Paths.get(System.getProperty("user.home"), "test.mv.db"));
FileSystemUtils.deleteRecursively(Paths.get(System.getProperty("user.home"), "test.trace.db"));
}

@Test
void tcpWithHostnameButNoPortWorks() throws IOException, SQLException {

// Stopping the default one...
server.stop();

// ...to launch another one with the default port
server = Server.createTcpServer("-ifNotExists").start();

FileSystemUtils.deleteRecursively(Paths.get(System.getProperty("user.home"), "test.mv.db"));
FileSystemUtils.deleteRecursively(Paths.get(System.getProperty("user.home"), "test.trace.db"));



H2ConnectionConfiguration configuration = H2ConnectionConfiguration.builder()
.tcp("localhost", "~/test")
.username("sa")
.password("")
.build();

new H2ConnectionFactory(configuration).create()
.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete();

FileSystemUtils.deleteRecursively(Paths.get(System.getProperty("user.home"), "test.mv.db"));
FileSystemUtils.deleteRecursively(Paths.get(System.getProperty("user.home"), "test.trace.db"));
}
}

0 comments on commit bc43c0c

Please sign in to comment.