use of org.h2.tools.Server in project h2database by h2database.
the class TestCluster method testCreateClusterAtRuntime.
private void testCreateClusterAtRuntime() throws SQLException {
if (config.memory || config.networked || config.cipher != null) {
return;
}
deleteFiles();
org.h2.Driver.load();
String user = getUser(), password = getPassword();
Connection conn;
Statement stat;
int len = 10;
// initialize the database
Server n1 = org.h2.tools.Server.createTcpServer("-baseDir", getBaseDir() + "/node1").start();
int port1 = n1.getPort();
String url1 = getURL("jdbc:h2:tcp://localhost:" + port1 + "/test", false);
conn = getConnection(url1, user, password);
stat = conn.createStatement();
stat.execute("create table test(id int primary key, name varchar) as " + "select x, 'Data' || x from system_range(0, " + (len - 1) + ")");
stat.execute("create user test password 'test'");
stat.execute("grant all on test to test");
// start the second server
Server n2 = org.h2.tools.Server.createTcpServer("-baseDir", getBaseDir() + "/node2").start();
int port2 = n2.getPort();
String url2 = getURL("jdbc:h2:tcp://localhost:" + port2 + "/test", false);
// copy the database and initialize the cluster
String serverList = "localhost:" + port1 + ",localhost:" + port2;
CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList", serverList);
// check the original connection is closed
assertThrows(ErrorCode.CONNECTION_BROKEN_1, stat).execute("select * from test");
JdbcUtils.closeSilently(conn);
// test the cluster connection
String urlCluster = getURL("jdbc:h2:tcp://" + serverList + "/test", false);
Connection connApp = getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user, password);
check(connApp, len, "'" + serverList + "'");
// delete the rows, but don't commit
connApp.setAutoCommit(false);
connApp.createStatement().execute("delete from test");
// stop server 2, and test if only one server is available
n2.stop();
// rollback the transaction
connApp.createStatement().executeQuery("select count(*) from test");
connApp.rollback();
check(connApp, len, "''");
connApp.setAutoCommit(true);
// re-create the cluster
n2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2, "-baseDir", getBaseDir() + "/node2").start();
CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList", serverList);
// test the cluster connection
check(connApp, len, "'" + serverList + "'");
connApp.close();
// test a non-admin user
String user2 = "test", password2 = getPassword("test");
connApp = getConnection(urlCluster, user2, password2);
check(connApp, len, "'" + serverList + "'");
connApp.close();
n1.stop();
// test non-admin cluster connection if only one server runs
Connection connApp2 = getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user2, password2);
check(connApp2, len, "''");
connApp2.close();
// test non-admin cluster connection if only one server runs
connApp2 = getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user2, password2);
check(connApp2, len, "''");
connApp2.close();
n2.stop();
deleteFiles();
}
use of org.h2.tools.Server in project h2database by h2database.
the class TestCluster method testStartStopCluster.
private void testStartStopCluster() throws SQLException {
if (config.memory || config.networked || config.cipher != null) {
return;
}
int port1 = 9193, port2 = 9194;
String serverList = "localhost:" + port1 + ",localhost:" + port2;
deleteFiles();
// initialize the database
Connection conn;
org.h2.Driver.load();
String urlNode1 = getURL("node1/test", true);
String urlNode2 = getURL("node2/test", true);
String user = getUser(), password = getPassword();
conn = getConnection(urlNode1, user, password);
Statement stat;
stat = conn.createStatement();
stat.execute("DROP TABLE IF EXISTS TEST");
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?)");
int len = getSize(10, 1000);
for (int i = 0; i < len; i++) {
prep.setInt(1, i);
prep.setString(2, "Data" + i);
prep.executeUpdate();
}
check(conn, len, "''");
conn.close();
// copy the database and initialize the cluster
CreateCluster.main("-urlSource", urlNode1, "-urlTarget", urlNode2, "-user", user, "-password", password, "-serverList", serverList);
// start both servers
Server n1 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1").start();
Server n2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2, "-baseDir", getBaseDir() + "/node2").start();
// try to connect in standalone mode - should fail
// should not be able to connect in standalone mode
assertThrows(ErrorCode.CLUSTER_ERROR_DATABASE_RUNS_CLUSTERED_1, this).getConnection("jdbc:h2:tcp://localhost:" + port1 + "/test", user, password);
assertThrows(ErrorCode.CLUSTER_ERROR_DATABASE_RUNS_CLUSTERED_1, this).getConnection("jdbc:h2:tcp://localhost:" + port2 + "/test", user, password);
// test a cluster connection
conn = getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password);
check(conn, len, "'" + serverList + "'");
conn.close();
// stop server 2, and test if only one server is available
n2.stop();
conn = getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password);
check(conn, len, "''");
conn.close();
conn = getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password);
check(conn, len, "''");
conn.close();
// disable the cluster
conn = getConnection("jdbc:h2:tcp://localhost:" + port1 + "/test;CLUSTER=''", user, password);
conn.close();
n1.stop();
// re-create the cluster
DeleteDbFiles.main("-dir", getBaseDir() + "/node2", "-quiet");
CreateCluster.main("-urlSource", urlNode1, "-urlTarget", urlNode2, "-user", user, "-password", password, "-serverList", serverList);
n1 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1").start();
n2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2, "-baseDir", getBaseDir() + "/node2").start();
conn = getConnection("jdbc:h2:tcp://" + serverList + "/test", user, password);
stat = conn.createStatement();
stat.execute("CREATE TABLE BOTH(ID INT)");
n1.stop();
stat.execute("CREATE TABLE A(ID INT)");
conn.close();
n2.stop();
n1 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1").start();
conn = getConnection("jdbc:h2:tcp://localhost:" + port1 + "/test;CLUSTER=''", user, password);
check(conn, len, "''");
conn.close();
n1.stop();
n2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2, "-baseDir", getBaseDir() + "/node2").start();
conn = getConnection("jdbc:h2:tcp://localhost:" + port2 + "/test;CLUSTER=''", user, password);
check(conn, len, "''");
conn.createStatement().execute("SELECT * FROM A");
conn.close();
n2.stop();
deleteFiles();
}
use of org.h2.tools.Server in project h2database by h2database.
the class TestCluster method testRollback.
private void testRollback() throws SQLException {
if (config.memory || config.networked || config.cipher != null) {
return;
}
deleteFiles();
org.h2.Driver.load();
String user = getUser(), password = getPassword();
Connection conn;
Statement stat;
ResultSet rs;
Server n1 = org.h2.tools.Server.createTcpServer("-baseDir", getBaseDir() + "/node1").start();
int port1 = n1.getPort();
Server n2 = org.h2.tools.Server.createTcpServer("-baseDir", getBaseDir() + "/node2").start();
int port2 = n2.getPort();
String url1 = getURL("jdbc:h2:tcp://localhost:" + port1 + "/test", true);
String url2 = getURL("jdbc:h2:tcp://localhost:" + port2 + "/test", true);
String serverList = "localhost:" + port1 + ",localhost:" + port2;
String urlCluster = getURL("jdbc:h2:tcp://" + serverList + "/test", true);
CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList", serverList);
conn = getConnection(urlCluster, user, password);
stat = conn.createStatement();
assertTrue(conn.getAutoCommit());
stat.execute("create table test(id int, name varchar)");
assertTrue(conn.getAutoCommit());
stat.execute("set autocommit false");
// issue 259
// assertFalse(conn.getAutoCommit());
conn.setAutoCommit(false);
assertFalse(conn.getAutoCommit());
stat.execute("insert into test values(1, 'Hello')");
stat.execute("rollback");
rs = stat.executeQuery("select * from test order by id");
assertFalse(rs.next());
conn.close();
n1.stop();
n2.stop();
deleteFiles();
}
use of org.h2.tools.Server in project h2database by h2database.
the class TestNetUtils method testAnonymousTlsSession.
/**
* With default settings, H2 client SSL socket should be able to connect
* to an H2 server SSL socket using an anonymous cipher suite
* (no SSL certificate is needed).
*/
private void testAnonymousTlsSession() throws Exception {
assertTrue("Failed assumption: the default value of ENABLE_ANONYMOUS_TLS" + " property should be true", SysProperties.ENABLE_ANONYMOUS_TLS);
boolean ssl = true;
Task task = null;
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = NetUtils.createServerSocket(PORT, ssl);
serverSocket.setSoTimeout(WAIT_LONGER_MILLIS);
task = createServerSocketTask(serverSocket);
task.execute(TASK_PREFIX + "AnonEnabled");
Thread.sleep(WAIT_MILLIS);
socket = NetUtils.createLoopbackSocket(PORT, ssl);
assertTrue("loopback anon socket should be connected", socket.isConnected());
SSLSession session = ((SSLSocket) socket).getSession();
assertTrue("TLS session should be valid when anonymous TLS is enabled", session.isValid());
// in case of handshake failure:
// the cipher suite is the pre-handshake SSL_NULL_WITH_NULL_NULL
assertContains(session.getCipherSuite(), "_anon_");
} finally {
closeSilently(socket);
closeSilently(serverSocket);
if (task != null) {
// SSL server socket should succeed using an anonymous cipher
// suite, and not throw javax.net.ssl.SSLHandshakeException
assertNull(task.getException());
task.join();
}
}
}
use of org.h2.tools.Server in project h2database by h2database.
the class TestNetUtils method testTlsSessionWithServerSideAnonymousDisabled.
/**
* TLS connections (without trusted certificates) should fail if the server
* does not allow anonymous TLS.
* The global property ENABLE_ANONYMOUS_TLS cannot be modified for the test;
* instead, the server socket is altered.
*/
private void testTlsSessionWithServerSideAnonymousDisabled() throws Exception {
boolean ssl = true;
Task task = null;
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = NetUtils.createServerSocket(PORT, ssl);
serverSocket.setSoTimeout(WAIT_LONGER_MILLIS);
// emulate the situation ENABLE_ANONYMOUS_TLS=false on server side
String[] defaultCipherSuites = SSLContext.getDefault().getServerSocketFactory().getDefaultCipherSuites();
((SSLServerSocket) serverSocket).setEnabledCipherSuites(defaultCipherSuites);
task = createServerSocketTask(serverSocket);
task.execute(TASK_PREFIX + "AnonDisabled");
Thread.sleep(WAIT_MILLIS);
socket = NetUtils.createLoopbackSocket(PORT, ssl);
assertTrue("loopback socket should be connected", socket.isConnected());
// Java 6 API does not have getHandshakeSession() which could
// reveal the actual cipher selected in the attempted handshake
SSLSession session = ((SSLSocket) socket).getSession();
assertFalse("TLS session should be invalid when the server" + "disables anonymous TLS", session.isValid());
// the SSL handshake should fail, because non-anon ciphers require
// a trusted certificate
assertEquals("SSL_NULL_WITH_NULL_NULL", session.getCipherSuite());
} finally {
closeSilently(socket);
closeSilently(serverSocket);
if (task != null) {
assertNotNull(task.getException());
assertEquals(javax.net.ssl.SSLHandshakeException.class.getName(), task.getException().getClass().getName());
assertContains(task.getException().getMessage(), "certificate_unknown");
task.join();
}
}
}
Aggregations