· 6 years ago · Jun 04, 2019, 10:46 PM
1package com.github.jcfandino;
2
3import org.h2.tools.CreateCluster;
4import org.h2.tools.Server;
5import org.h2gis.functions.factory.H2GISFunctions;
6
7import java.nio.file.Files;
8import java.nio.file.Path;
9import java.sql.DriverManager;
10
11public class TestH2GisApplication {
12
13 public static void main(String[] args) {
14 try {
15 // Cleanup
16 Files.deleteIfExists(Path.of("db1/db.mv.db"));
17 Files.deleteIfExists(Path.of("db2/db.mv.db"));
18 // Start server 1
19 Server.createTcpServer("-baseDir", "db1", "-tcp", "-tcpAllowOthers", "-tcpPort", "9091").start();
20 // Start server 2
21 Server.createTcpServer("-baseDir", "db2", "-tcp", "-tcpAllowOthers", "-tcpPort", "9092").start();
22 // Connect to server 1 and init database.
23 try (var con = DriverManager.getConnection("jdbc:h2:tcp://localhost:9091/./db")) {
24 H2GISFunctions.load(con); // <-- comment this line and the example works
25 con.createStatement().execute("create table if not exists geometries(id serial, geo geometry);");
26 con.createStatement().execute("insert into geometries(geo) values ('POINT(10 10)');");
27 // Query server 1
28 try (var results = con.createStatement().executeQuery("select * from geometries;")) {
29 results.next();
30 System.out.println("So far so good: " + results.getString(2));
31 }
32 }
33 // Create cluster between both servers (this is where it fails)
34 new CreateCluster().runTool(
35 "-urlSource", "jdbc:h2:tcp://localhost:9091/./db",
36 "-urlTarget", "jdbc:h2:tcp://localhost:9092/./db",
37 "-serverList", "localhost:9091,localhost:9092");
38 // Query cluster
39 try (var con = DriverManager.getConnection("jdbc:h2:tcp://localhost:9091,localhost:9092/./db");
40 var results = con.createStatement().executeQuery("select * from geometries;")) {
41 results.next();
42 System.out.println("My wish: " + results.getString(2));
43 }
44 } catch (Exception e) {
45 e.printStackTrace();
46 System.exit(1);
47 }
48 System.exit(0);
49 }
50
51}