use of herddb.server.Server in project herddb by diennea.
the class SimpleClientServerTest method testClientCloseOnConnectionAndResumeTransaction.
@Test
public void testClientCloseOnConnectionAndResumeTransaction() throws Exception {
Path baseDir = folder.newFolder().toPath();
AtomicInteger connectionToUse = new AtomicInteger();
AtomicReference<ClientSideConnectionPeer[]> connections = new AtomicReference<>();
try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
server.start();
server.waitForStandaloneBoot();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 2);
clientConfiguration.set(ClientConfiguration.PROPERTY_TIMEOUT, 2000);
try (HDBClient client = new HDBClient(clientConfiguration) {
@Override
public HDBConnection openConnection() {
HDBConnection con = new HDBConnection(this) {
@Override
protected ClientSideConnectionPeer chooseConnection(ClientSideConnectionPeer[] all) {
connections.set(all);
LOG.log(Level.INFO, "chooseConnection among " + all.length + " connections, getting " + connectionToUse);
return all[connectionToUse.get()];
}
};
registerConnection(con);
return con;
}
};
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
// force using the first connection of two
connectionToUse.set(0);
assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id int primary key, s1 string)", 0, false, true, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
// transaction is bound to the first connection (in HerdDB 11.0.0)
long tx = connection.beginTransaction(TableSpace.DEFAULT);
assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", tx, false, true, Arrays.asList(1, "test1")).updateCount);
// close the connection that initiated the transaction
connections.get()[0].close();
// give time to the server to close the connection
Thread.sleep(100);
// use the second connection, with the same transaction
connectionToUse.set(1);
connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", tx, false, true, Arrays.asList(2, "test1"));
}
}
}
use of herddb.server.Server in project herddb by diennea.
the class SimpleClientServerTest method testKeepReadLocks.
@Test
public void testKeepReadLocks() throws Exception {
Path baseDir = folder.newFolder().toPath();
try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
server.start();
server.waitForStandaloneBoot();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
try (HDBClient client = new HDBClient(clientConfiguration);
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, true, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
long tx = connection.beginTransaction(TableSpace.DEFAULT);
long countInsert = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, true, Arrays.asList("test", 1, 2)).updateCount;
Assert.assertEquals(1, countInsert);
long countInsert2 = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, true, Arrays.asList("test2", 2, 3)).updateCount;
Assert.assertEquals(1, countInsert2);
connection.commitTransaction(TableSpace.DEFAULT, tx);
// new transaction
tx = connection.beginTransaction(TableSpace.DEFAULT);
// do not keep locks
try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", true, Collections.emptyList(), tx, 0, 10, false)) {
Map<String, Object> record = scan.consume().get(0);
assertEquals(RawString.of("test"), record.get("id"));
assertEquals(Long.valueOf(1), record.get("n1"));
assertEquals(Integer.valueOf(2), record.get("n2"));
Transaction transaction = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransaction(tx);
assertNull(transaction.lookupLock("mytable", Bytes.from_string("test")));
}
// keep locks
try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", true, Collections.emptyList(), tx, 0, 10, true)) {
Map<String, Object> record = scan.consume().get(0);
assertEquals(RawString.of("test"), record.get("id"));
assertEquals(Long.valueOf(1), record.get("n1"));
assertEquals(Integer.valueOf(2), record.get("n2"));
Transaction transaction = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransaction(tx);
assertFalse(transaction.lookupLock("mytable", Bytes.from_string("test")).write);
}
// upgrade lock to write
try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test' FOR UPDATE", true, Collections.emptyList(), tx, 0, 10, false)) {
Map<String, Object> record = scan.consume().get(0);
assertEquals(RawString.of("test"), record.get("id"));
assertEquals(Long.valueOf(1), record.get("n1"));
assertEquals(Integer.valueOf(2), record.get("n2"));
Transaction transaction = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransaction(tx);
assertTrue(transaction.lookupLock("mytable", Bytes.from_string("test")).write);
}
connection.rollbackTransaction(TableSpace.DEFAULT, tx);
// new transaction
tx = connection.beginTransaction(TableSpace.DEFAULT);
// SELECT FOR UPDATE must hold WRITE LOCK even with keepLocks = false
try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test' FOR UPDATE", true, Collections.emptyList(), tx, 0, 10, false)) {
Map<String, Object> record = scan.consume().get(0);
assertEquals(RawString.of("test"), record.get("id"));
assertEquals(Long.valueOf(1), record.get("n1"));
assertEquals(Integer.valueOf(2), record.get("n2"));
Transaction transaction = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransaction(tx);
assertTrue(transaction.lookupLock("mytable", Bytes.from_string("test")).write);
}
}
}
}
use of herddb.server.Server in project herddb by diennea.
the class SimpleClientServerTest method test.
@Test
public void test() throws Exception {
Path baseDir = folder.newFolder().toPath();
String _baseDir = baseDir.toString();
try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
server.start();
server.waitForStandaloneBoot();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
try (HDBClient client = new HDBClient(clientConfiguration);
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, true, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
long tx = connection.beginTransaction(TableSpace.DEFAULT);
long countInsert = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, true, Arrays.asList("test", 1, 2)).updateCount;
Assert.assertEquals(1, countInsert);
long countInsert2 = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, true, Arrays.asList("test2", 2, 3)).updateCount;
Assert.assertEquals(1, countInsert2);
{
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", tx, true, Collections.emptyList());
Map<RawString, Object> record = res.data;
Assert.assertNotNull(record);
assertEquals(RawString.of("test"), record.get(RawString.of("id")));
assertEquals(Long.valueOf(1), record.get(RawString.of("n1")));
assertEquals(Integer.valueOf(2), record.get(RawString.of("n2")));
}
{
server.getManager().getPreparedStatementsCache().clear();
GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", tx, true, Collections.emptyList());
Map<RawString, Object> record = res.data;
Assert.assertNotNull(record);
}
List<DMLResult> executeUpdates = connection.executeUpdates(TableSpace.DEFAULT, "UPDATE mytable set n2=? WHERE id=?", tx, false, true, Arrays.asList(Arrays.asList(1, "test"), Arrays.asList(2, "test2"), Arrays.asList(3, "test_not_exists")));
assertEquals(3, executeUpdates.size());
assertEquals(1, executeUpdates.get(0).updateCount);
assertEquals(1, executeUpdates.get(1).updateCount);
assertEquals(0, executeUpdates.get(2).updateCount);
assertEquals(tx, executeUpdates.get(0).transactionId);
assertEquals(tx, executeUpdates.get(1).transactionId);
assertEquals(tx, executeUpdates.get(2).transactionId);
connection.commitTransaction(TableSpace.DEFAULT, tx);
try (ScanResultSet scan = connection.executeScan(server.getManager().getVirtualTableSpaceId(), "SELECT * FROM sysconfig", true, Collections.emptyList(), 0, 0, 10, true)) {
List<Map<String, Object>> all = scan.consume();
for (Map<String, Object> aa : all) {
RawString name = (RawString) aa.get("name");
if (RawString.of("server.base.dir").equals(name)) {
assertEquals(RawString.of("server.base.dir"), name);
RawString value = (RawString) aa.get("value");
assertEquals(RawString.of(_baseDir), value);
} else if (RawString.of("server.port").equals(name)) {
RawString value = (RawString) aa.get("value");
assertEquals(RawString.of("0"), value);
} else {
assertEquals(RawString.of("server.node.id"), name);
}
}
}
try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM " + server.getManager().getVirtualTableSpaceId() + ".sysclients", true, Collections.emptyList(), 0, 0, 10, true)) {
List<Map<String, Object>> all = scan.consume();
for (Map<String, Object> aa : all) {
assertEquals(RawString.of("jvm-local"), aa.get("address"));
assertNotNull(aa.get("id"));
assertEquals(RawString.of(ClientConfiguration.PROPERTY_CLIENT_USERNAME_DEFAULT), aa.get("username"));
assertNotNull(aa.get("connectionts"));
}
assertTrue(all.size() >= 1);
}
List<DMLResult> executeUpdatesWithoutParams = connection.executeUpdates(TableSpace.DEFAULT, "UPDATE mytable set n2=1 WHERE id='test'", -1, false, true, Arrays.asList(// empty list of parameters
Arrays.asList()));
assertEquals(1, executeUpdatesWithoutParams.size());
assertEquals(1, executeUpdatesWithoutParams.get(0).updateCount);
// missing JDBC parameter
try {
connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList("test"));
fail("cannot issue a query without setting each parameter");
} catch (HDBException ok) {
assertTrue(ok.getMessage().contains(MissingJDBCParameterException.class.getName()));
}
// simple rollback
long txToRollback = connection.beginTransaction(TableSpace.DEFAULT);
long countInsertToRollback = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", txToRollback, false, true, Arrays.asList("test123", 7, 8)).updateCount;
Assert.assertEquals(1, countInsertToRollback);
connection.rollbackTransaction(TableSpace.DEFAULT, txToRollback);
}
}
}
use of herddb.server.Server in project herddb by diennea.
the class SimpleClientServerTest method testSQLIntegrityViolation.
@Test
public void testSQLIntegrityViolation() throws Exception {
Path baseDir = folder.newFolder().toPath();
try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
server.start();
server.waitForStandaloneBoot();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
try (HDBClient client = new HDBClient(clientConfiguration);
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id int primary key, s1 string)", 0, false, true, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
long tx = connection.beginTransaction(TableSpace.DEFAULT);
long countInsert = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", tx, false, true, Arrays.asList(1, "test1")).updateCount;
Assert.assertEquals(1, countInsert);
try {
connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", tx, false, true, Arrays.asList(1, "test2"));
} catch (Exception ex) {
Assert.assertTrue(ex.getMessage().contains("SQLIntegrityConstraintViolationException"));
}
connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", tx, false, true, Arrays.asList(2, "test2"));
connection.commitTransaction(TableSpace.DEFAULT, tx);
try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM herd.mytable", true, Collections.emptyList(), 0, 0, 10, true)) {
List<Map<String, Object>> rows = scan.consume();
int i = 0;
for (Map<String, Object> row : rows) {
if (i == 0) {
i++;
Assert.assertEquals(row.get("id"), 1);
Assert.assertEquals(row.get("s1"), "test1");
} else {
Assert.assertEquals(row.get("id"), 2);
Assert.assertEquals(row.get("s1"), "test2");
}
}
}
}
}
}
use of herddb.server.Server in project herddb by diennea.
the class StreamBasedWritesTest method test.
@Test
public void test() throws Exception {
try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(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 int primary key," + "valstring string," + "valbinary blob" + ")");
String myString = "mystring";
byte[] myBinary = "mybinary".getBytes(StandardCharsets.UTF_8);
String myShortString = "my";
byte[] myShortBinary = "my".getBytes(StandardCharsets.UTF_8);
Set<Integer> shorterValues = new HashSet<>();
try (PreparedStatement insert = con.prepareStatement("INSERT INTO mytable (key,valstring,valbinary ) values(?,?,?)")) {
int i = 0;
insert.setInt(1, i);
insert.setString(2, myString);
insert.setBytes(3, myBinary);
insert.executeUpdate();
i++;
insert.setInt(1, i);
insert.setCharacterStream(2, new StringReader(myString));
insert.setBinaryStream(3, new ByteArrayInputStream(myBinary));
insert.executeUpdate();
i++;
insert.setInt(1, i);
insert.setNCharacterStream(2, new StringReader(myString));
insert.setBinaryStream(3, new ByteArrayInputStream(myBinary));
insert.executeUpdate();
i++;
insert.setInt(1, i);
insert.setCharacterStream(2, new StringReader(myString), 2);
insert.setBinaryStream(3, new ByteArrayInputStream(myBinary), 2);
insert.executeUpdate();
shorterValues.add(i);
i++;
insert.setInt(1, i);
insert.setNCharacterStream(2, new StringReader(myString), 2);
insert.setBinaryStream(3, new ByteArrayInputStream(myBinary), 2L);
insert.executeUpdate();
shorterValues.add(i);
i++;
insert.setInt(1, i);
insert.setClob(2, new StringReader(myString));
insert.setBlob(3, new ByteArrayInputStream(myBinary));
insert.executeUpdate();
i++;
insert.setInt(1, i);
insert.setClob(2, new StringReader(myString), 2);
insert.setBlob(3, new ByteArrayInputStream(myBinary), 2);
insert.executeUpdate();
shorterValues.add(i);
i++;
insert.setInt(1, i);
insert.setAsciiStream(2, new ByteArrayInputStream(myString.getBytes(StandardCharsets.US_ASCII)), 2);
insert.setBlob(3, new ByteArrayInputStream(myBinary), 2);
insert.executeUpdate();
shorterValues.add(i);
i++;
insert.setInt(1, i);
insert.setAsciiStream(2, new ByteArrayInputStream(myString.getBytes(StandardCharsets.US_ASCII)));
insert.setBlob(3, new ByteArrayInputStream(myBinary));
insert.executeUpdate();
i++;
insert.setInt(1, i);
insert.setAsciiStream(2, new ByteArrayInputStream(myString.getBytes(StandardCharsets.US_ASCII)), 2L);
insert.setBlob(3, new ByteArrayInputStream(myBinary), 2);
insert.executeUpdate();
shorterValues.add(i);
i++;
insert.setInt(1, i);
insert.setUnicodeStream(2, new ByteArrayInputStream(myString.getBytes(StandardCharsets.US_ASCII)), 2);
insert.setBlob(3, new ByteArrayInputStream(myBinary), 2);
insert.executeUpdate();
shorterValues.add(i);
}
try (PreparedStatement ps = con.prepareStatement("SELECT key,valstring,valbinary" + " FROM mytable ORDER BY key");
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
int key = rs.getInt(1);
if (shorterValues.contains(key)) {
checkString(rs, 2, myShortString, key);
checkBlob(rs, 3, myShortBinary, key);
} else {
checkString(rs, 2, myString, key);
checkBlob(rs, 3, myBinary, key);
}
}
}
}
}
}
}
Aggregations