use of herddb.server.Server in project herddb by diennea.
the class MaxRowsTest method test.
@Test
public void test() throws Exception {
try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
Connection con = dataSource.getConnection();
Statement statement = con.createStatement()) {
statement.execute("CREATE TABLE mytable (key string primary key, name string)");
assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k1','name1')"));
assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k2','name2')"));
assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k3','name3')"));
try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(3, count);
}
statement.setMaxRows(2);
try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(2, count);
}
statement.setMaxRows(0);
try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(3, count);
}
statement.setMaxRows(Integer.MAX_VALUE);
try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(3, count);
}
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM mytable")) {
try (ResultSet rs = ps.executeQuery()) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(3, count);
}
ps.setMaxRows(2);
try (ResultSet rs = ps.executeQuery()) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(2, count);
}
ps.setMaxRows(0);
try (ResultSet rs = ps.executeQuery()) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(3, count);
}
ps.setMaxRows(Integer.MAX_VALUE);
try (ResultSet rs = ps.executeQuery()) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(3, count);
}
}
}
}
}
}
use of herddb.server.Server in project herddb by diennea.
the class MultipleDataSourcesSameJVMJMXTest method test.
@Test
public void test() throws Exception {
try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
BasicHerdDBDataSource dataSource_2 = new BasicHerdDBDataSource(client);
Connection con = dataSource.getConnection();
Connection con_2 = dataSource_2.getConnection();
Statement statement = con.createStatement();
Statement statement_2 = con.createStatement()) {
statement.execute("CREATE TABLE mytable (key string primary key, name string)");
assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k1','name1')"));
assertEquals(1, statement_2.executeUpdate("INSERT INTO mytable (key,name) values('k2','name2')"));
assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k3','name3')"));
try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable a" + " INNER JOIN mytable b ON 1=1")) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(9, count);
}
MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer();
Object attributeValue = jmxServer.getAttribute(new ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name=HerdDBClient"), "BorrowedCount");
assertEquals(1L, attributeValue);
Object attributeValue2 = jmxServer.getAttribute(new ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name=HerdDBClient2"), "BorrowedCount");
assertEquals(1L, attributeValue2);
}
}
}
}
use of herddb.server.Server in project herddb by diennea.
the class HerdDBEmbeddedDataSource method startEmbeddedServer.
private void startEmbeddedServer() throws SQLException {
if (!serverInitialized) {
ServerConfiguration serverConfiguration = new ServerConfiguration(properties);
serverConfiguration.readJdbcUrl(url);
String mode = serverConfiguration.getString(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_LOCAL);
if (ServerConfiguration.PROPERTY_MODE_LOCAL.equals(mode) || (ServerConfiguration.PROPERTY_MODE_STANDALONE.equals(mode) && startServer)) {
LOGGER.log(Level.INFO, "Booting Local Embedded HerdDB mode, url:" + url + ", properties:" + serverConfiguration);
server = new Server(serverConfiguration);
try {
server.start();
int waitForTableSpaceTimeout = getWaitForTableSpaceTimeout();
if (waitForTableSpaceTimeout > 0) {
server.waitForBootOfLocalTablespaces(waitForTableSpaceTimeout);
}
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
} catch (Exception ex) {
throw new SQLException("Cannot boot embedded server " + ex, ex);
}
}
serverInitialized = true;
}
}
use of herddb.server.Server in project herddb by diennea.
the class GetConnectionTest method test.
@Test
public void test() throws Exception {
try (HerdDBEmbeddedDataSource dataSource = new HerdDBEmbeddedDataSource()) {
dataSource.getProperties().setProperty(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().getAbsolutePath());
dataSource.getProperties().setProperty(ClientConfiguration.PROPERTY_BASEDIR, folder.newFolder().getAbsolutePath());
try (Connection con = dataSource.getConnection();
Statement statement = con.createStatement()) {
statement.execute("CREATE TABLE mytable (key string primary key, name string)");
}
for (int i = 0; i < 2; i++) {
try (Connection con = dataSource.getConnection();
Statement statement = con.createStatement()) {
assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k1" + i + "','name1')"));
}
}
Server server = dataSource.getServer();
assertEquals(1, server.getConnectionCount());
Connection _con;
try (Connection con = dataSource.getConnection()) {
_con = con;
assertFalse(con.isClosed());
// double close MUST be allowed
con.close();
assertTrue(con.isClosed());
}
assertTrue(_con.isClosed());
}
}
use of herddb.server.Server in project herddb by diennea.
the class JdbcDriverZookeeperTest method test.
@Test
public void test() throws Exception {
ServerConfiguration serverconfig_1 = new ServerConfiguration(folder.newFolder().toPath());
serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
serverconfig_1.set(ServerConfiguration.PROPERTY_PORT, 7867);
serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
try (Server server = new Server(serverconfig_1)) {
server.start();
server.waitForStandaloneBoot();
try (Connection connection = DriverManager.getConnection("jdbc:herddb:zookeeper:" + testEnv.getAddress() + "" + testEnv.getPath());
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM SYSTABLES")) {
int count = 0;
while (rs.next()) {
System.out.println("table: " + rs.getString(1));
count++;
}
assertTrue(count > 0);
}
}
}
Aggregations