fix(h2): share one driver classloader across sessions
This commit is contained in:
1 parent
f89759e744
commit
d430a4f057
7 files changed
+607
-29
No files matched your search
@@ -59,38 +59,53 @@ public class H2Agent extends AbstractJdbcAgent {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void loadDriver(ConnectParams params) throws Exception {
|
protected void loadDriver(ConnectParams params) throws Exception {
|
||||||
|
// Selection and physical opens must not race a first connection's file lock.
|
||||||
|
synchronized (H2FileConnections.class) {
|
||||||
|
selectDriver(params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void selectDriver(ConnectParams params) throws Exception {
|
||||||
H2DriverVersion selected = H2DriverVersion.select(params);
|
H2DriverVersion selected = H2DriverVersion.select(params);
|
||||||
if (selected == H2DriverVersion.CUSTOM) {
|
if (selected == H2DriverVersion.CUSTOM) {
|
||||||
H2DriverLoader.LoadedDriver external = H2DriverLoader.loadExternal(
|
loadedDriver = H2DriverLoader.loadExternal(
|
||||||
params.getJdbc_driver_paths(),
|
params.getJdbc_driver_paths(),
|
||||||
params.getJdbc_driver_class()
|
params.getJdbc_driver_class()
|
||||||
);
|
);
|
||||||
if (loadedDriver == null || !loadedDriver.identity().equals(external.identity())) {
|
} else {
|
||||||
replaceLoadedDriver(external);
|
loadedDriver = H2DriverLoader.load(selected);
|
||||||
} else {
|
|
||||||
external.classLoader().close();
|
|
||||||
}
|
|
||||||
} else if (loadedDriver == null || loadedDriver.version() != selected) {
|
|
||||||
replaceLoadedDriver(H2DriverLoader.load(selected));
|
|
||||||
}
|
}
|
||||||
driverVersion = selected;
|
driverVersion = selected;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void replaceLoadedDriver(H2DriverLoader.LoadedDriver replacement) throws Exception {
|
|
||||||
H2DriverLoader.LoadedDriver previous = loadedDriver;
|
|
||||||
if (previous != null) {
|
|
||||||
try {
|
|
||||||
previous.classLoader().close();
|
|
||||||
} catch (Exception error) {
|
|
||||||
replacement.classLoader().close();
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
loadedDriver = replacement;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Connection openConnection(ConnectParams params) throws Exception {
|
protected Connection openConnection(ConnectParams params) throws Exception {
|
||||||
|
String url = buildJdbcUrl(params);
|
||||||
|
if (H2FileFormatDetector.localDatabaseBasePath(url) != null) {
|
||||||
|
synchronized (H2FileConnections.class) {
|
||||||
|
selectDriver(params);
|
||||||
|
H2DriverLoader.LoadedDriver active = H2FileConnections.find(url);
|
||||||
|
if (active != null && !active.identity().equals(loadedDriver.identity())) {
|
||||||
|
throw new SQLException("This H2 file is already open with a different driver; use the same H2 driver profile or disconnect it first");
|
||||||
|
}
|
||||||
|
Connection opened = connectWithDriver(params);
|
||||||
|
try {
|
||||||
|
H2FileConnections.register(url, opened, loadedDriver);
|
||||||
|
} catch (Exception error) {
|
||||||
|
try {
|
||||||
|
opened.close();
|
||||||
|
} catch (Exception closeError) {
|
||||||
|
error.addSuppressed(closeError);
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
return opened;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return connectWithDriver(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection connectWithDriver(ConnectParams params) throws Exception {
|
||||||
if (loadedDriver == null) {
|
if (loadedDriver == null) {
|
||||||
throw new IllegalStateException("H2 JDBC driver was not loaded");
|
throw new IllegalStateException("H2 JDBC driver was not loaded");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.dbx.agent.h2;
|
package com.dbx.agent.h2;
|
||||||
|
|
||||||
|
import com.dbx.agent.ConnectParams;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
@@ -10,18 +11,46 @@ import java.nio.file.StandardCopyOption;
|
|||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.sql.Driver;
|
import java.sql.Driver;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.EnumMap;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HexFormat;
|
import java.util.HexFormat;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
final class H2DriverLoader {
|
final class H2DriverLoader {
|
||||||
private static final Object EXTRACTION_LOCK = new Object();
|
private static final Object EXTRACTION_LOCK = new Object();
|
||||||
private static final Path CACHE_ROOT = Path.of(System.getProperty("java.io.tmpdir"), "dbx-h2-drivers");
|
private static final Path CACHE_ROOT = Path.of(System.getProperty("java.io.tmpdir"), "dbx-h2-drivers");
|
||||||
|
// H2's engine registry is classloader-scoped. Keep one engine per driver for
|
||||||
|
// the Agent process, not one per logical session (which fights for file locks).
|
||||||
|
private static final Map<H2DriverVersion, LoadedDriver> BUNDLED = new EnumMap<>(H2DriverVersion.class);
|
||||||
|
private static final Map<String, LoadedDriver> EXTERNAL = new HashMap<>();
|
||||||
|
|
||||||
private H2DriverLoader() {
|
private H2DriverLoader() {
|
||||||
}
|
}
|
||||||
|
|
||||||
static LoadedDriver load(H2DriverVersion version) throws Exception {
|
static boolean autoServerEnabled(ConnectParams params) throws Exception {
|
||||||
|
// Use H2's parser so escaped INIT statements and duplicate options keep JDBC semantics.
|
||||||
|
Class<?> connectionInfo = load(H2DriverVersion.V3).classLoader().loadClass("org.h2.engine.ConnectionInfo");
|
||||||
|
Properties properties = new Properties();
|
||||||
|
if (params.getUsername() != null) {
|
||||||
|
properties.setProperty("user", params.getUsername());
|
||||||
|
}
|
||||||
|
if (params.getPassword() != null) {
|
||||||
|
properties.setProperty("password", params.getPassword());
|
||||||
|
}
|
||||||
|
Object info = connectionInfo.getConstructor(String.class, Properties.class, String.class, Object.class)
|
||||||
|
.newInstance(H2Agent.buildUrl(params), properties, null, null);
|
||||||
|
return Boolean.TRUE.equals(connectionInfo.getMethod("getProperty", String.class, boolean.class)
|
||||||
|
.invoke(info, "AUTO_SERVER", false));
|
||||||
|
}
|
||||||
|
|
||||||
|
static synchronized LoadedDriver load(H2DriverVersion version) throws Exception {
|
||||||
|
LoadedDriver cached = BUNDLED.get(version);
|
||||||
|
if (cached != null) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
Path jar = extract(version);
|
Path jar = extract(version);
|
||||||
URLClassLoader classLoader = new URLClassLoader(
|
URLClassLoader classLoader = new URLClassLoader(
|
||||||
new URL[]{jar.toUri().toURL()},
|
new URL[]{jar.toUri().toURL()},
|
||||||
@@ -29,7 +58,9 @@ final class H2DriverLoader {
|
|||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
Driver driver = (Driver) Class.forName("org.h2.Driver", true, classLoader).getDeclaredConstructor().newInstance();
|
Driver driver = (Driver) Class.forName("org.h2.Driver", true, classLoader).getDeclaredConstructor().newInstance();
|
||||||
return new LoadedDriver(version, version.version(), driver, classLoader);
|
LoadedDriver loaded = new LoadedDriver(version, version.version(), driver, classLoader);
|
||||||
|
BUNDLED.put(version, loaded);
|
||||||
|
return loaded;
|
||||||
} catch (Exception error) {
|
} catch (Exception error) {
|
||||||
closeAfterFailure(classLoader, error);
|
closeAfterFailure(classLoader, error);
|
||||||
throw error;
|
throw error;
|
||||||
@@ -39,7 +70,7 @@ final class H2DriverLoader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static LoadedDriver loadExternal(List<String> driverPaths, String driverClass) throws Exception {
|
static synchronized LoadedDriver loadExternal(List<String> driverPaths, String driverClass) throws Exception {
|
||||||
if (driverPaths == null || driverPaths.isEmpty()) {
|
if (driverPaths == null || driverPaths.isEmpty()) {
|
||||||
throw new IllegalArgumentException("Custom H2 driver profile requires at least one JDBC JAR path");
|
throw new IllegalArgumentException("Custom H2 driver profile requires at least one JDBC JAR path");
|
||||||
}
|
}
|
||||||
@@ -50,17 +81,26 @@ final class H2DriverLoader {
|
|||||||
if (!Files.isRegularFile(path)) {
|
if (!Files.isRegularFile(path)) {
|
||||||
throw new IOException("Custom H2 JDBC JAR does not exist: " + path);
|
throw new IOException("Custom H2 JDBC JAR does not exist: " + path);
|
||||||
}
|
}
|
||||||
urls.add(path.toUri().toURL());
|
path = path.toRealPath();
|
||||||
identities.add(path + ":" + sha256(path));
|
identities.add(path + ":" + sha256(path));
|
||||||
|
// Keep the original location for relative Manifest Class-Path entries.
|
||||||
|
urls.add(path.toUri().toURL());
|
||||||
}
|
}
|
||||||
String effectiveDriverClass = driverClass == null || driverClass.isBlank() ? "org.h2.Driver" : driverClass.trim();
|
String effectiveDriverClass = driverClass == null || driverClass.isBlank() ? "org.h2.Driver" : driverClass.trim();
|
||||||
|
String identity = effectiveDriverClass + "|" + String.join("|", identities);
|
||||||
|
LoadedDriver cached = EXTERNAL.get(identity);
|
||||||
|
if (cached != null) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
URLClassLoader classLoader = new URLClassLoader(
|
URLClassLoader classLoader = new URLClassLoader(
|
||||||
urls.toArray(new URL[0]),
|
urls.toArray(new URL[0]),
|
||||||
H2DriverLoader.class.getClassLoader()
|
H2DriverLoader.class.getClassLoader()
|
||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
Driver driver = (Driver) Class.forName(effectiveDriverClass, true, classLoader).getDeclaredConstructor().newInstance();
|
Driver driver = (Driver) Class.forName(effectiveDriverClass, true, classLoader).getDeclaredConstructor().newInstance();
|
||||||
return new LoadedDriver(H2DriverVersion.CUSTOM, effectiveDriverClass + "|" + String.join("|", identities), driver, classLoader);
|
LoadedDriver loaded = new LoadedDriver(H2DriverVersion.CUSTOM, identity, driver, classLoader);
|
||||||
|
EXTERNAL.put(identity, loaded);
|
||||||
|
return loaded;
|
||||||
} catch (Exception error) {
|
} catch (Exception error) {
|
||||||
closeAfterFailure(classLoader, error);
|
closeAfterFailure(classLoader, error);
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
@@ -47,8 +47,25 @@ enum H2DriverVersion {
|
|||||||
case "h2-v3" -> V3;
|
case "h2-v3" -> V3;
|
||||||
case "h2-custom" -> CUSTOM;
|
case "h2-custom" -> CUSTOM;
|
||||||
case "", "h2", "h2-auto", "h2_embedded", "h2_server" -> {
|
case "", "h2", "h2-auto", "h2_embedded", "h2_server" -> {
|
||||||
java.util.OptionalInt detected = H2FileFormatDetector.detect(H2Agent.buildUrl(params));
|
String url = H2Agent.buildUrl(params);
|
||||||
yield detected.isPresent() ? fromStorageFormat(detected.getAsInt()) : V3;
|
H2DriverLoader.LoadedDriver active = H2FileConnections.find(url);
|
||||||
|
if (active != null) {
|
||||||
|
if (active.version() == CUSTOM) {
|
||||||
|
throw new IllegalArgumentException("This H2 file is already open with a custom driver; select the same custom H2 driver profile");
|
||||||
|
}
|
||||||
|
yield active.version();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
java.util.OptionalInt detected = H2FileFormatDetector.detect(url);
|
||||||
|
yield detected.isPresent() ? fromStorageFormat(detected.getAsInt()) : V3;
|
||||||
|
} catch (H2FileFormatDetector.HeaderReadException error) {
|
||||||
|
// Another Agent may own the file. H2's AUTO_SERVER path negotiates
|
||||||
|
// the wire protocol and validates the lock/server key itself.
|
||||||
|
if (H2FileFormatDetector.hasAutoServerLock(url) && H2DriverLoader.autoServerEnabled(params)) {
|
||||||
|
yield V3;
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
default -> throw new IllegalArgumentException("Unsupported H2 driver profile: " + profile);
|
default -> throw new IllegalArgumentException("Unsupported H2 driver profile: " + profile);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package com.dbx.agent.h2;
|
||||||
|
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/** Tracks physical connections and embedded engines without extending their lifetimes. */
|
||||||
|
final class H2FileConnections {
|
||||||
|
private static final Map<Path, List<OpenConnection>> CONNECTIONS = new HashMap<>();
|
||||||
|
|
||||||
|
private H2FileConnections() {
|
||||||
|
}
|
||||||
|
|
||||||
|
static synchronized H2DriverLoader.LoadedDriver find(String jdbcUrl) throws Exception {
|
||||||
|
CONNECTIONS.values().forEach(entries -> entries.removeIf(entry -> !entry.isOpen()));
|
||||||
|
CONNECTIONS.values().removeIf(List::isEmpty);
|
||||||
|
List<OpenConnection> entries = CONNECTIONS.get(key(jdbcUrl));
|
||||||
|
return entries == null ? null : entries.get(0).driver();
|
||||||
|
}
|
||||||
|
|
||||||
|
static synchronized void register(String jdbcUrl, Connection connection, H2DriverLoader.LoadedDriver driver) throws Exception {
|
||||||
|
Path key = key(jdbcUrl);
|
||||||
|
if (key != null) {
|
||||||
|
EmbeddedDatabase database = embeddedDatabase(connection);
|
||||||
|
List<OpenConnection> entries = CONNECTIONS.computeIfAbsent(key, ignored -> new ArrayList<>());
|
||||||
|
if (database != null && entries.stream().anyMatch(entry -> entry.database() != null
|
||||||
|
&& entry.database().reference().get() == database.reference().get())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
entries.add(new OpenConnection(new WeakReference<>(connection), database, driver));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static EmbeddedDatabase embeddedDatabase(Connection connection) {
|
||||||
|
try {
|
||||||
|
// Public H2 accessors differ only in their return types across bundled versions.
|
||||||
|
Object session = connection.getClass().getMethod("getSession").invoke(connection);
|
||||||
|
if (session == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Object database = session.getClass().getMethod("getDatabase").invoke(session);
|
||||||
|
if (database == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new EmbeddedDatabase(new WeakReference<>(database), database.getClass().getMethod("isClosing"));
|
||||||
|
} catch (ReflectiveOperationException ignored) {
|
||||||
|
// AUTO_SERVER remote sessions and custom wrappers may expose no embedded engine.
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Path key(String jdbcUrl) throws Exception {
|
||||||
|
Path base = H2FileFormatDetector.localDatabaseBasePath(jdbcUrl);
|
||||||
|
if (base == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
for (String suffix : List.of(".mv.db", ".h2.db")) {
|
||||||
|
Path file = Path.of(base + suffix);
|
||||||
|
if (Files.isRegularFile(file)) {
|
||||||
|
return file.toRealPath();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return base;
|
||||||
|
}
|
||||||
|
|
||||||
|
private record EmbeddedDatabase(WeakReference<Object> reference, Method isClosing) {
|
||||||
|
boolean isOpen() {
|
||||||
|
Object database = reference.get();
|
||||||
|
if (database == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
synchronized (database) {
|
||||||
|
try {
|
||||||
|
return Boolean.FALSE.equals(isClosing.invoke(database));
|
||||||
|
} catch (ReflectiveOperationException ignored) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private record OpenConnection(
|
||||||
|
WeakReference<Connection> reference,
|
||||||
|
EmbeddedDatabase database,
|
||||||
|
H2DriverLoader.LoadedDriver driver
|
||||||
|
) {
|
||||||
|
boolean isOpen() {
|
||||||
|
Connection connection = reference.get();
|
||||||
|
try {
|
||||||
|
if (connection != null && !connection.isClosed()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} catch (SQLException ignored) {
|
||||||
|
}
|
||||||
|
// DB_CLOSE_DELAY can keep the engine and its file lock alive after JDBC close.
|
||||||
|
return database != null && database.isOpen();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.OptionalInt;
|
import java.util.OptionalInt;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
final class H2FileFormatDetector {
|
final class H2FileFormatDetector {
|
||||||
private static final int BLOCK_SIZE = 4096;
|
private static final int BLOCK_SIZE = 4096;
|
||||||
@@ -37,10 +38,33 @@ final class H2FileFormatDetector {
|
|||||||
return OptionalInt.of(readMvStoreFormat(mvStore));
|
return OptionalInt.of(readMvStoreFormat(mvStore));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static boolean hasAutoServerLock(String jdbcUrl) {
|
||||||
|
Path base = localDatabaseBasePath(jdbcUrl);
|
||||||
|
if (base == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Properties lock = new Properties();
|
||||||
|
try (InputStream input = Files.newInputStream(withSuffix(base, ".lock.db"))) {
|
||||||
|
lock.load(input);
|
||||||
|
return !lock.getProperty("server", "").isBlank() && !lock.getProperty("id", "").isBlank();
|
||||||
|
} catch (IOException | IllegalArgumentException ignored) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static final class HeaderReadException extends IOException {
|
||||||
|
HeaderReadException(Path file, IOException cause) {
|
||||||
|
super("Cannot read H2 file headers: " + file + ". " + cause.getMessage()
|
||||||
|
+ ". Check file permissions and close other applications using this database, or connect through its H2 TCP server.", cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static int readMvStoreFormat(Path file) throws IOException {
|
private static int readMvStoreFormat(Path file) throws IOException {
|
||||||
byte[] bytes;
|
byte[] bytes;
|
||||||
try (InputStream input = Files.newInputStream(file)) {
|
try (InputStream input = Files.newInputStream(file)) {
|
||||||
bytes = input.readNBytes(BLOCK_SIZE * 2);
|
bytes = input.readNBytes(BLOCK_SIZE * 2);
|
||||||
|
} catch (IOException error) {
|
||||||
|
throw new HeaderReadException(file, error);
|
||||||
}
|
}
|
||||||
if (bytes.length < BLOCK_SIZE * 2) {
|
if (bytes.length < BLOCK_SIZE * 2) {
|
||||||
throw new IOException("H2 MVStore file is too small to contain valid headers: " + file);
|
throw new IOException("H2 MVStore file is too small to contain valid headers: " + file);
|
||||||
@@ -131,7 +155,7 @@ final class H2FileFormatDetector {
|
|||||||
return (s2 << 16) | s1;
|
return (s2 << 16) | s1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Path localDatabaseBasePath(String jdbcUrl) {
|
static Path localDatabaseBasePath(String jdbcUrl) {
|
||||||
String prefix = "jdbc:h2:";
|
String prefix = "jdbc:h2:";
|
||||||
if (jdbcUrl == null || !jdbcUrl.regionMatches(true, 0, prefix, 0, prefix.length())) {
|
if (jdbcUrl == null || !jdbcUrl.regionMatches(true, 0, prefix, 0, prefix.length())) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -23,8 +23,259 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.Timeout;
|
import org.junit.jupiter.api.Timeout;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
class H2AgentProcessTest {
|
class H2AgentProcessTest {
|
||||||
|
@TempDir
|
||||||
|
Path tempDirectory;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Timeout(90)
|
||||||
|
void fileSessionsShareTheDriverAcrossProfilesAndConnectionTests() throws Exception {
|
||||||
|
try (RpcProcess rpc = new RpcProcess(Path.of(System.getProperty("dbx.h2.agent.jar")))) {
|
||||||
|
for (String profile : List.of("h2-v1", "h2-v2", "h2-v3")) {
|
||||||
|
String database = "file:" + tempDirectory.resolve(profile).toString().replace('\\', '/');
|
||||||
|
JsonObject first = connectParams(profile + "-first", profile);
|
||||||
|
first.addProperty("database", database);
|
||||||
|
rpc.result(rpc.request("open_session", first));
|
||||||
|
rpc.result(rpc.request("execute_query", queryParams(profile + "-first", "CREATE TABLE SHARED_DATA (ID INT PRIMARY KEY)")));
|
||||||
|
rpc.result(rpc.request("execute_query", queryParams(profile + "-first", "INSERT INTO SHARED_DATA VALUES (42)")));
|
||||||
|
|
||||||
|
JsonObject second = connectParams(profile + "-second", profile);
|
||||||
|
second.addProperty("database", database + ";IFEXISTS=TRUE");
|
||||||
|
second.addProperty("sessionRole", "metadata");
|
||||||
|
rpc.result(rpc.request("open_session", second));
|
||||||
|
Assertions.assertEquals("42", firstCell(rpc.result(rpc.request("execute_query", queryParams(profile + "-second", "SELECT ID FROM SHARED_DATA")))));
|
||||||
|
|
||||||
|
JsonObject auto = connectParams(profile + "-auto", "h2");
|
||||||
|
auto.addProperty("database", database + ";IFEXISTS=TRUE");
|
||||||
|
rpc.result(rpc.request("open_session", auto));
|
||||||
|
Assertions.assertTrue(rpc.result(rpc.request("test_connection", auto)).get("ok").getAsBoolean());
|
||||||
|
rpc.result(rpc.request("close_session", sessionParams(profile + "-first")));
|
||||||
|
Assertions.assertEquals("42", firstCell(rpc.result(rpc.request("execute_query", queryParams(profile + "-auto", "SELECT ID FROM SHARED_DATA")))));
|
||||||
|
rpc.result(rpc.request("close_session", sessionParams(profile + "-second")));
|
||||||
|
rpc.result(rpc.request("close_session", sessionParams(profile + "-auto")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Timeout(60)
|
||||||
|
void autoServerFileSessionsSupportConnectionTestsAndDatabaseSelection() throws Exception {
|
||||||
|
try (RpcProcess rpc = new RpcProcess(Path.of(System.getProperty("dbx.h2.agent.jar")))) {
|
||||||
|
JsonObject params = connectParams("file-metadata", "h2");
|
||||||
|
String database = "file:" + tempDirectory.resolve("auto-server") + ";AUTO_SERVER=TRUE";
|
||||||
|
params.addProperty("database", database);
|
||||||
|
params.addProperty("connection_string", "jdbc:h2:" + database);
|
||||||
|
Assertions.assertTrue(rpc.result(rpc.request("test_connection", params)).get("ok").getAsBoolean());
|
||||||
|
params.addProperty("sessionRole", "metadata");
|
||||||
|
rpc.result(rpc.request("open_session", params));
|
||||||
|
rpc.resultArray(rpc.request("list_databases", sessionParams("file-metadata")));
|
||||||
|
JsonObject workload = params.deepCopy();
|
||||||
|
workload.addProperty("agentSessionId", "file-workload");
|
||||||
|
workload.addProperty("sessionRole", "workload");
|
||||||
|
rpc.result(rpc.request("open_session", workload));
|
||||||
|
Assertions.assertEquals("1", firstCell(rpc.result(rpc.request("execute_query", queryParams("file-workload", "SELECT 1")))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Timeout(60)
|
||||||
|
void autoFileSessionReopensAfterConnectionTestWithDelayedClose() throws Exception {
|
||||||
|
Path jar = Path.of(System.getProperty("dbx.h2.agent.jar"));
|
||||||
|
for (String profile : List.of("h2-v1", "h2-v2", "h2-v3")) {
|
||||||
|
String database = "file:" + tempDirectory.resolve("delayed-" + profile) + ";AUTO_SERVER=TRUE";
|
||||||
|
JsonObject params = connectParams("delayed-close", profile);
|
||||||
|
params.addProperty("database", database);
|
||||||
|
// Persist the setting, then start a fresh Agent just like a client restart.
|
||||||
|
try (RpcProcess creator = new RpcProcess(jar)) {
|
||||||
|
creator.result(creator.request("open_session", params));
|
||||||
|
creator.result(creator.request("execute_query", queryParams("delayed-close", "SET DB_CLOSE_DELAY -1")));
|
||||||
|
}
|
||||||
|
params.addProperty("driver_profile", "h2");
|
||||||
|
try (RpcProcess rpc = new RpcProcess(jar)) {
|
||||||
|
Assertions.assertTrue(rpc.result(rpc.request("test_connection", params)).get("ok").getAsBoolean());
|
||||||
|
Assertions.assertTrue(rpc.result(rpc.request("test_connection", params)).get("ok").getAsBoolean());
|
||||||
|
rpc.result(rpc.request("open_session", params));
|
||||||
|
Assertions.assertEquals("1", firstCell(rpc.result(rpc.request("execute_query", queryParams("delayed-close", "SELECT 1")))));
|
||||||
|
rpc.result(rpc.request("close_session", sessionParams("delayed-close")));
|
||||||
|
rpc.result(rpc.request("open_session", params));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Timeout(60)
|
||||||
|
void failedFirstAutoFileOpenDoesNotMaskAuthenticationError() throws Exception {
|
||||||
|
Path jar = Path.of(System.getProperty("dbx.h2.agent.jar"));
|
||||||
|
for (String profile : List.of("h2-v1", "h2-v2", "h2-v3")) {
|
||||||
|
JsonObject params = connectParams("failed-first", profile);
|
||||||
|
params.addProperty("database", "file:" + tempDirectory.resolve("failed-first-" + profile)
|
||||||
|
+ ";AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1");
|
||||||
|
params.addProperty("password", "owner-secret");
|
||||||
|
try (RpcProcess creator = new RpcProcess(jar)) {
|
||||||
|
creator.result(creator.request("open_session", params));
|
||||||
|
}
|
||||||
|
params.addProperty("driver_profile", "h2");
|
||||||
|
try (RpcProcess rpc = new RpcProcess(jar)) {
|
||||||
|
JsonObject denied = params.deepCopy();
|
||||||
|
denied.addProperty("password", "wrong-secret");
|
||||||
|
for (int attempt = 0; attempt < 2; attempt++) {
|
||||||
|
JsonObject response = rpc.request("test_connection", denied);
|
||||||
|
Assertions.assertTrue(response.has("error"));
|
||||||
|
JsonObject errorData = response.getAsJsonObject("error").getAsJsonObject("data");
|
||||||
|
Assertions.assertTrue(errorData.has("sqlState"), response.toString());
|
||||||
|
Assertions.assertEquals("28000", errorData.get("sqlState").getAsString(), response.toString());
|
||||||
|
}
|
||||||
|
rpc.result(rpc.request("open_session", params));
|
||||||
|
Assertions.assertEquals("1", firstCell(rpc.result(rpc.request("execute_query", queryParams("failed-first", "SELECT 1")))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Timeout(60)
|
||||||
|
void delayedCloseEngineStillChecksProfileAndCredentials() throws Exception {
|
||||||
|
try (RpcProcess rpc = new RpcProcess(Path.of(System.getProperty("dbx.h2.agent.jar")))) {
|
||||||
|
JsonObject owner = connectParams("delayed-owner", "h2-v1");
|
||||||
|
owner.addProperty("database", "file:" + tempDirectory.resolve("protected-delayed") + ";DB_CLOSE_DELAY=-1");
|
||||||
|
owner.addProperty("password", "owner-secret");
|
||||||
|
Assertions.assertTrue(rpc.result(rpc.request("test_connection", owner)).get("ok").getAsBoolean());
|
||||||
|
JsonObject other = owner.deepCopy();
|
||||||
|
other.addProperty("driver_profile", "h2-v3");
|
||||||
|
JsonObject mismatch = rpc.request("test_connection", other);
|
||||||
|
Assertions.assertTrue(mismatch.has("error"));
|
||||||
|
Assertions.assertTrue(mismatch.getAsJsonObject("error").get("message").getAsString().contains("different driver"));
|
||||||
|
other.addProperty("driver_profile", "h2");
|
||||||
|
other.addProperty("password", "wrong-secret");
|
||||||
|
Assertions.assertTrue(rpc.request("test_connection", other).has("error"));
|
||||||
|
owner.addProperty("driver_profile", "h2");
|
||||||
|
Assertions.assertTrue(rpc.result(rpc.request("test_connection", owner)).get("ok").getAsBoolean());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Timeout(60)
|
||||||
|
void shutdownReleasesRetainedEngineAndAllowsFormatRedetection() throws Exception {
|
||||||
|
try (RpcProcess rpc = new RpcProcess(Path.of(System.getProperty("dbx.h2.agent.jar")))) {
|
||||||
|
Path base = tempDirectory.resolve("shutdown-delayed");
|
||||||
|
JsonObject params = connectParams("before-shutdown", "h2-v1");
|
||||||
|
params.addProperty("database", "file:" + base + ";DB_CLOSE_DELAY=60");
|
||||||
|
Assertions.assertTrue(rpc.result(rpc.request("test_connection", params)).get("ok").getAsBoolean());
|
||||||
|
params.addProperty("driver_profile", "h2");
|
||||||
|
rpc.result(rpc.request("open_session", params));
|
||||||
|
JsonObject shutdown = rpc.request("execute_query", queryParams("before-shutdown", "SHUTDOWN"));
|
||||||
|
if (shutdown.has("error")) {
|
||||||
|
// H2 1.x can report database-closed when the executor inspects SHUTDOWN's result.
|
||||||
|
Assertions.assertEquals(90121, shutdown.getAsJsonObject("error").getAsJsonObject("data").get("vendorCode").getAsInt());
|
||||||
|
} else {
|
||||||
|
rpc.result(shutdown);
|
||||||
|
}
|
||||||
|
rpc.result(rpc.request("close_session", sessionParams("before-shutdown")));
|
||||||
|
Files.delete(Path.of(base + ".mv.db"));
|
||||||
|
|
||||||
|
params.addProperty("database", "file:" + base);
|
||||||
|
params.addProperty("driver_profile", "h2-v2");
|
||||||
|
Assertions.assertTrue(rpc.result(rpc.request("test_connection", params)).get("ok").getAsBoolean());
|
||||||
|
params.addProperty("driver_profile", "h2");
|
||||||
|
JsonObject reopened = rpc.result(rpc.request("test_connection", params));
|
||||||
|
Assertions.assertTrue(reopened.getAsJsonObject("databaseInfo").get("driverVersion").getAsString().startsWith("2.1.214"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Timeout(60)
|
||||||
|
void autoFileSessionCanBeOpenedAgainWhileThePoolKeepsItOpen() throws Exception {
|
||||||
|
try (RpcProcess rpc = new RpcProcess(Path.of(System.getProperty("dbx.h2.agent.jar")))) {
|
||||||
|
JsonObject first = connectParams("auto-first", "h2");
|
||||||
|
first.addProperty("database", "file:" + tempDirectory.resolve("auto").toString().replace('\\', '/'));
|
||||||
|
rpc.result(rpc.request("open_session", first));
|
||||||
|
rpc.result(rpc.request("close_session", sessionParams("auto-first")));
|
||||||
|
JsonObject second = first.deepCopy();
|
||||||
|
second.addProperty("agentSessionId", "auto-second");
|
||||||
|
rpc.result(rpc.request("open_session", second));
|
||||||
|
Assertions.assertTrue(rpc.result(rpc.request("test_connection", second)).get("ok").getAsBoolean());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Timeout(60)
|
||||||
|
void concurrentAutoFileSessionsShareOneEngine() throws Exception {
|
||||||
|
try (RpcProcess rpc = new RpcProcess(Path.of(System.getProperty("dbx.h2.agent.jar")))) {
|
||||||
|
List<Integer> requests = new java.util.ArrayList<>();
|
||||||
|
for (int index = 0; index < 6; index++) {
|
||||||
|
JsonObject params = connectParams("concurrent-" + index, "h2");
|
||||||
|
params.addProperty("database", "file:" + tempDirectory.resolve("concurrent").toString().replace('\\', '/')
|
||||||
|
+ ";LOCK_TIMEOUT=" + (1000 + index));
|
||||||
|
requests.add(rpc.send("open_session", params));
|
||||||
|
}
|
||||||
|
for (int request : requests) {
|
||||||
|
rpc.result(rpc.await(request, Duration.ofSeconds(20)));
|
||||||
|
}
|
||||||
|
rpc.result(rpc.request("execute_query", queryParams("concurrent-0", "CREATE TABLE SHARED_VALUE (ID INT)")));
|
||||||
|
rpc.result(rpc.request("execute_query", queryParams("concurrent-0", "INSERT INTO SHARED_VALUE VALUES (7)")));
|
||||||
|
for (int index = 1; index < 6; index++) {
|
||||||
|
Assertions.assertEquals("7", firstCell(rpc.result(rpc.request("execute_query", queryParams("concurrent-" + index, "SELECT ID FROM SHARED_VALUE")))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Timeout(60)
|
||||||
|
void autoServerFileCanBeOpenedFromAnotherAgentProcess() throws Exception {
|
||||||
|
Path jar = Path.of(System.getProperty("dbx.h2.agent.jar"));
|
||||||
|
for (String profile : List.of("h2-v1", "h2-v2", "h2-v3")) {
|
||||||
|
try (RpcProcess owner = new RpcProcess(jar); RpcProcess other = new RpcProcess(jar)) {
|
||||||
|
JsonObject params = connectParams("auto-server-owner", profile);
|
||||||
|
String database = "file:" + tempDirectory.resolve("shared-server-" + profile);
|
||||||
|
params.addProperty("database", database + ";AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1");
|
||||||
|
params.addProperty("password", "owner-secret");
|
||||||
|
owner.result(owner.request("open_session", params));
|
||||||
|
owner.result(owner.request("execute_query", queryParams("auto-server-owner", "CREATE TABLE OWNER_DATA (ID INT)")));
|
||||||
|
owner.result(owner.request("execute_query", queryParams("auto-server-owner", "INSERT INTO OWNER_DATA VALUES (99)")));
|
||||||
|
|
||||||
|
JsonObject test = params.deepCopy();
|
||||||
|
test.addProperty("driver_profile", "h2-v3");
|
||||||
|
Assertions.assertTrue(other.result(other.request("test_connection", test)).get("ok").getAsBoolean());
|
||||||
|
test.addProperty("driver_profile", "h2");
|
||||||
|
test.addProperty("agentSessionId", "auto-server-guest");
|
||||||
|
JsonObject denied = test.deepCopy();
|
||||||
|
denied.addProperty("database", database);
|
||||||
|
Assertions.assertTrue(other.request("test_connection", denied).has("error"));
|
||||||
|
denied.addProperty("database", database + ";AUTO_SERVER=FALSE");
|
||||||
|
Assertions.assertTrue(other.request("test_connection", denied).has("error"));
|
||||||
|
denied = test.deepCopy();
|
||||||
|
denied.addProperty("password", "wrong-secret");
|
||||||
|
Assertions.assertTrue(other.request("test_connection", denied).has("error"));
|
||||||
|
Assertions.assertTrue(other.result(other.request("test_connection", test)).get("ok").getAsBoolean());
|
||||||
|
other.result(other.request("open_session", test));
|
||||||
|
Assertions.assertEquals("99", firstCell(other.result(other.request("execute_query", queryParams("auto-server-guest", "SELECT ID FROM OWNER_DATA")))));
|
||||||
|
Assertions.assertTrue(other.resultArray(other.request("list_objects", metadataParams("auto-server-guest", "PUBLIC")))
|
||||||
|
.asList().stream().anyMatch(value -> "OWNER_DATA".equals(value.getAsJsonObject().get("name").getAsString())));
|
||||||
|
Assertions.assertEquals("99", firstCell(owner.result(owner.request("execute_query", queryParams("auto-server-owner", "SELECT ID FROM OWNER_DATA")))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Timeout(60)
|
||||||
|
void externalProcessLockIsNotBypassedAndOwnerKeepsWorking() throws Exception {
|
||||||
|
Path jar = Path.of(System.getProperty("dbx.h2.agent.jar"));
|
||||||
|
try (RpcProcess owner = new RpcProcess(jar); RpcProcess other = new RpcProcess(jar)) {
|
||||||
|
JsonObject params = connectParams("owner", "h2-v3");
|
||||||
|
params.addProperty("database", "file:" + tempDirectory.resolve("externally-locked").toString().replace('\\', '/'));
|
||||||
|
owner.result(owner.request("open_session", params));
|
||||||
|
owner.result(owner.request("execute_query", queryParams("owner", "CREATE TABLE OWNER_DATA (ID INT)")));
|
||||||
|
owner.result(owner.request("execute_query", queryParams("owner", "INSERT INTO OWNER_DATA VALUES (99)")));
|
||||||
|
JsonObject test = params.deepCopy();
|
||||||
|
test.addProperty("driver_profile", "h2");
|
||||||
|
Assertions.assertTrue(other.request("test_connection", test).has("error"));
|
||||||
|
test.addProperty("driver_profile", "h2-v3");
|
||||||
|
Assertions.assertTrue(other.request("test_connection", test).has("error"));
|
||||||
|
Assertions.assertEquals("99", firstCell(owner.result(owner.request("execute_query", queryParams("owner", "SELECT ID FROM OWNER_DATA")))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Timeout(45)
|
@Timeout(45)
|
||||||
void shadedAgentSupportsIsolatedMultiVersionSessions() throws Exception {
|
void shadedAgentSupportsIsolatedMultiVersionSessions() throws Exception {
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import java.nio.file.attribute.FileTime;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.jar.JarOutputStream;
|
import java.util.jar.JarOutputStream;
|
||||||
|
import java.util.jar.Manifest;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.io.TempDir;
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
@@ -127,6 +128,89 @@ class H2AgentMigrationTest {
|
|||||||
Assertions.assertEquals(3, H2FileFormatDetector.detect("jdbc:h2:file:" + base).orElseThrow());
|
Assertions.assertEquals(3, H2FileFormatDetector.detect("jdbc:h2:file:" + base).orElseThrow());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void closedFileConnectionsDoNotPinTheDetectedVersion() throws Exception {
|
||||||
|
Path base = tempDirectory.resolve("replaceable");
|
||||||
|
H2Agent first = new H2Agent();
|
||||||
|
first.connect(profileParams("h2", "file:" + base));
|
||||||
|
first.disconnect();
|
||||||
|
Files.delete(Path.of(base + ".mv.db"));
|
||||||
|
H2Agent older = new H2Agent();
|
||||||
|
older.connect(profileParams("h2-v2", "file:" + base));
|
||||||
|
older.disconnect();
|
||||||
|
H2Agent detected = new H2Agent();
|
||||||
|
try {
|
||||||
|
detected.connect(profileParams("h2", "file:" + base + ";IFEXISTS=TRUE"));
|
||||||
|
Assertions.assertEquals(H2DriverVersion.V2, detected.driverVersion());
|
||||||
|
} finally {
|
||||||
|
detected.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void activeFileRejectsDifferentDriverAndWrongCredentials() {
|
||||||
|
Path base = tempDirectory.resolve("protected");
|
||||||
|
H2Agent owner = new H2Agent();
|
||||||
|
H2Agent other = new H2Agent();
|
||||||
|
try {
|
||||||
|
ConnectParams params = profileParams("h2-v3", "file:" + base);
|
||||||
|
params.setPassword("owner-secret");
|
||||||
|
owner.connect(params);
|
||||||
|
RuntimeException mismatch = Assertions.assertThrows(RuntimeException.class,
|
||||||
|
() -> other.connect(profileParams("h2-v2", "file:" + base)));
|
||||||
|
Assertions.assertTrue(hasCauseMessage(mismatch, "different driver"));
|
||||||
|
Assertions.assertThrows(RuntimeException.class,
|
||||||
|
() -> other.testConnectionWithInfo(profileParams("h2", "file:" + base)));
|
||||||
|
Assertions.assertEquals(List.of(List.of(1)), owner.executeQuery("SELECT 1", null, new ExecuteQueryOptions()).getRows());
|
||||||
|
} finally {
|
||||||
|
other.disconnect();
|
||||||
|
owner.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void autoServerOptionsUseH2ParsingRules() throws Exception {
|
||||||
|
String base = "file:" + tempDirectory.resolve("options");
|
||||||
|
Assertions.assertFalse(H2DriverLoader.autoServerEnabled(profileParams("h2", base)));
|
||||||
|
Assertions.assertFalse(H2DriverLoader.autoServerEnabled(profileParams("h2", base + ";AUTO_SERVER=FALSE")));
|
||||||
|
Assertions.assertTrue(H2DriverLoader.autoServerEnabled(profileParams("h2", base + ";auto_server=TRUE")));
|
||||||
|
Assertions.assertTrue(H2DriverLoader.autoServerEnabled(profileParams("h2", base + ";AUTO_SERVER=1")));
|
||||||
|
Assertions.assertFalse(H2DriverLoader.autoServerEnabled(profileParams("h2", base
|
||||||
|
+ ";INIT=SET @NOTE 'x\\;AUTO_SERVER=TRUE'")));
|
||||||
|
Assertions.assertThrows(Exception.class, () -> H2DriverLoader.autoServerEnabled(profileParams("h2", base
|
||||||
|
+ ";AUTO_SERVER=FALSE;AUTO_SERVER=TRUE")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void autoServerLockDoesNotBypassCorruptHeaderValidation() throws Exception {
|
||||||
|
Path base = tempDirectory.resolve("corrupt-auto-server");
|
||||||
|
Path file = Path.of(base + ".mv.db");
|
||||||
|
byte[] corrupt = new byte[8192];
|
||||||
|
java.util.Arrays.fill(corrupt, (byte) 0x5a);
|
||||||
|
Files.write(file, corrupt);
|
||||||
|
Files.writeString(Path.of(base + ".lock.db"), "server=127.0.0.1:1\nid=test-key\n");
|
||||||
|
String url = "jdbc:h2:file:" + base + ";AUTO_SERVER=TRUE";
|
||||||
|
Assertions.assertTrue(H2FileFormatDetector.hasAutoServerLock(url));
|
||||||
|
RuntimeException error = Assertions.assertThrows(RuntimeException.class,
|
||||||
|
() -> new H2Agent().connect(profileParams("h2", "file:" + base + ";AUTO_SERVER=TRUE")));
|
||||||
|
Assertions.assertTrue(hasCauseMessage(error, "Cannot determine the H2 MVStore format"));
|
||||||
|
Assertions.assertArrayEquals(corrupt, Files.readAllBytes(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void autoServerLockRequiresValidServerAndKeyProperties() throws Exception {
|
||||||
|
Path base = tempDirectory.resolve("invalid-lock");
|
||||||
|
Path lock = Path.of(base + ".lock.db");
|
||||||
|
String url = "jdbc:h2:file:" + base;
|
||||||
|
Assertions.assertFalse(H2FileFormatDetector.hasAutoServerLock(url));
|
||||||
|
Files.writeString(lock, "server=127.0.0.1:1\n");
|
||||||
|
Assertions.assertFalse(H2FileFormatDetector.hasAutoServerLock(url));
|
||||||
|
Files.writeString(lock, "id=test-key\n");
|
||||||
|
Assertions.assertFalse(H2FileFormatDetector.hasAutoServerLock(url));
|
||||||
|
Files.writeString(lock, "server=\\uZZZZ\nid=test-key\n");
|
||||||
|
Assertions.assertFalse(H2FileFormatDetector.hasAutoServerLock(url));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void autoRejectsCorruptMvStoreWithoutModifyingIt() throws Exception {
|
void autoRejectsCorruptMvStoreWithoutModifyingIt() throws Exception {
|
||||||
Path base = tempDirectory.resolve("corrupt");
|
Path base = tempDirectory.resolve("corrupt");
|
||||||
@@ -231,6 +315,23 @@ class H2AgentMigrationTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customDriverPreservesManifestRelativeClasspath() throws Exception {
|
||||||
|
Path driverJar = copyBundledDriver("h2-2.1.214.jar");
|
||||||
|
Path entryJar = tempDirectory.resolve("driver-entry.jar");
|
||||||
|
Manifest manifest = new Manifest();
|
||||||
|
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
|
||||||
|
manifest.getMainAttributes().putValue("Class-Path", driverJar.getFileName().toString());
|
||||||
|
try (JarOutputStream ignored = new JarOutputStream(Files.newOutputStream(entryJar), manifest)) {
|
||||||
|
}
|
||||||
|
H2DriverLoader.LoadedDriver driver = H2DriverLoader.loadExternal(List.of(entryJar.toString()), "org.h2.Driver");
|
||||||
|
try (java.sql.Connection connection = driver.driver().connect("jdbc:h2:mem:manifest-classpath", new java.util.Properties())) {
|
||||||
|
Assertions.assertTrue(connection.getMetaData().getDriverVersion().startsWith("2.1.214"));
|
||||||
|
} finally {
|
||||||
|
driver.classLoader().close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void loadsCustomH2DriverFromExternalClasspath() throws Exception {
|
void loadsCustomH2DriverFromExternalClasspath() throws Exception {
|
||||||
Path helperJar = tempDirectory.resolve("helper.jar");
|
Path helperJar = tempDirectory.resolve("helper.jar");
|
||||||
@@ -250,6 +351,30 @@ class H2AgentMigrationTest {
|
|||||||
Assertions.assertEquals(List.of("CUSTOM_DRIVER_PROBE"), agent.listTables("PUBLIC").stream().map(TableInfo::getName).toList());
|
Assertions.assertEquals(List.of("CUSTOM_DRIVER_PROBE"), agent.listTables("PUBLIC").stream().map(TableInfo::getName).toList());
|
||||||
} finally {
|
} finally {
|
||||||
agent.disconnect();
|
agent.disconnect();
|
||||||
|
H2DriverLoader.loadExternal(params.getJdbc_driver_paths(), params.getJdbc_driver_class()).classLoader().close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void customFileConnectionsShareDriver() throws Exception {
|
||||||
|
Path jar = copyBundledDriver("h2-2.1.214.jar");
|
||||||
|
ConnectParams params = profileParams("h2-custom", "file:" + tempDirectory.resolve("custom-shared"));
|
||||||
|
params.setJdbc_driver_paths(List.of(jar.toString()));
|
||||||
|
params.setJdbc_driver_class("org.h2.Driver");
|
||||||
|
H2Agent first = new H2Agent();
|
||||||
|
H2Agent second = new H2Agent();
|
||||||
|
try {
|
||||||
|
first.connect(params);
|
||||||
|
first.executeQuery("CREATE TABLE SHARED_CUSTOM (ID INT)", null, new ExecuteQueryOptions());
|
||||||
|
first.executeQuery("INSERT INTO SHARED_CUSTOM VALUES (42)", null, new ExecuteQueryOptions());
|
||||||
|
second.connect(params);
|
||||||
|
Assertions.assertEquals(List.of(List.of(42)), second.executeQuery("SELECT ID FROM SHARED_CUSTOM", null, new ExecuteQueryOptions()).getRows());
|
||||||
|
first.disconnect();
|
||||||
|
Assertions.assertEquals(List.of(List.of(42)), second.executeQuery("SELECT ID FROM SHARED_CUSTOM", null, new ExecuteQueryOptions()).getRows());
|
||||||
|
} finally {
|
||||||
|
first.disconnect();
|
||||||
|
second.disconnect();
|
||||||
|
H2DriverLoader.loadExternal(params.getJdbc_driver_paths(), params.getJdbc_driver_class()).classLoader().close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user