Search in sources :

Example 16 with StaticClientSideMetadataProvider

use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.

the class GeneratedKeysTest method testPreparedStatementInt.

@Test
public void testPreparedStatementInt() 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 (n1 int primary key auto_increment, name string)");
                try (PreparedStatement prepared = con.prepareStatement("INSERT INTO mytable (name) values('name1')", Statement.RETURN_GENERATED_KEYS)) {
                    assertEquals(1, prepared.executeUpdate());
                    Object key = null;
                    try (ResultSet generatedKeys = prepared.getGeneratedKeys()) {
                        if (generatedKeys.next()) {
                            key = generatedKeys.getObject(1);
                        }
                    }
                    assertNotNull(key);
                    assertEquals(Integer.valueOf(1), key);
                }
                try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
                    int count = 0;
                    while (rs.next()) {
                        count++;
                    }
                    assertEquals(1, count);
                }
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 17 with StaticClientSideMetadataProvider

use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.

the class TransactionsTest method noAutoSteartTransaction.

/**
 * Commit on a connection with just instructions that do not auto create a transaction
 */
@Test
public void noAutoSteartTransaction() 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();
                Connection con2 = dataSource.getConnection();
                Statement statement = con.createStatement();
                Statement statement2 = con2.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')"));
                con2.setAutoCommit(false);
                int update = statement2.executeUpdate("ALTER TABLE mytable ADD COLUMN other string");
                assertEquals(1, update);
                con2.commit();
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 18 with StaticClientSideMetadataProvider

use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.

the class SimpleClientServerTest method testSimpleJoinFromNetwork.

@Test
public void testSimpleJoinFromNetwork() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    ServerConfiguration config = newServerConfigurationWithAutoPort(baseDir);
    config.set(ServerConfiguration.PROPERTY_ABANDONED_TRANSACTIONS_TIMEOUT, 5000);
    try (Server server = new Server(config)) {
        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);
            for (int i = 0; i < 10; i++) {
                assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", 0, false, true, Arrays.asList(i, "test1")).updateCount);
            }
            // test join with different
            try (ScanResultSet scanner = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable a" + " INNER JOIN mytable b ON 1=1", true, Collections.emptyList(), 0, 0, 100000, true)) {
                List<Map<String, Object>> resultSet = scanner.consume();
                assertEquals(100, resultSet.size());
            }
            try (ScanResultSet scanner = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable a" + " INNER JOIN mytable b ON 1=1", true, Collections.emptyList(), 0, 0, 1, true)) {
                List<Map<String, Object>> resultSet = scanner.consume();
                assertEquals(100, resultSet.size());
            }
            try (ScanResultSet scanner = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable a" + " INNER JOIN mytable b ON 1=1", true, Collections.emptyList(), 0, 0, 10, true)) {
                List<Map<String, Object>> resultSet = scanner.consume();
                assertEquals(100, resultSet.size());
            }
            long tx = connection.beginTransaction(TableSpace.DEFAULT);
            try (ScanResultSet scanner = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable a" + " INNER JOIN mytable b ON 1=1", true, Collections.emptyList(), tx, 0, 1, true)) {
                List<Map<String, Object>> resultSet = scanner.consume();
                assertEquals(100, resultSet.size());
            }
            connection.commitTransaction(TableSpace.DEFAULT, tx);
        }
    }
}
Also used : Path(java.nio.file.Path) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) Server(herddb.server.Server) ServerConfiguration(herddb.server.ServerConfiguration) Map(java.util.Map) Test(org.junit.Test)

Example 19 with StaticClientSideMetadataProvider

use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.

the class SimpleClientServerTest method testCachePreparedStatementsRestartServer.

@Test
public void testCachePreparedStatementsRestartServer() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
    clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 1);
    try (HDBClient client = new HDBClient(clientConfiguration)) {
        try (HDBConnection connection1 = client.openConnection()) {
            try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
                server.start();
                server.waitForStandaloneBoot();
                client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
                assertTrue(connection1.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
                // this is 1 for the client and for the server
                long resultCreateTable = connection1.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, true, /*usePreparedStatement*/
                Collections.emptyList()).updateCount;
                Assert.assertEquals(1, resultCreateTable);
                // this is 2 for the client and for the server
                connection1.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable", true, /*usePreparedStatement*/
                Collections.emptyList(), 0, 0, 10, true).close();
                // this is 3 for the client and for the server
                connection1.executeScan(TableSpace.DEFAULT, "SELECT id FROM mytable", true, /*usePreparedStatement*/
                Collections.emptyList(), 0, 0, 10, true).close();
            }
            try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
                server.start();
                server.waitForStandaloneBoot();
                client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
                assertTrue(connection1.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
                // this is 1 for the server, the client will invalidate its cache for this statement
                connection1.executeScan(TableSpace.DEFAULT, "SELECT n1 FROM mytable", true, /*usePreparedStatement*/
                Collections.emptyList(), 0, 0, 10, true).close();
                // this is 2 for the server
                try (HDBConnection connection2 = client.openConnection()) {
                    connection2.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set n1=2", 0, false, true, /*usePreparedStatement*/
                    Collections.emptyList());
                }
                // this would be 2 for connection1 (bug in 0.10.0), but for the server 2 is "UPDATE mytable set n1=2"
                connection1.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable", true, /*usePreparedStatement*/
                Collections.emptyList(), 0, 0, 10, true).close();
            }
        }
    }
}
Also used : Path(java.nio.file.Path) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) Server(herddb.server.Server) Test(org.junit.Test)

Example 20 with StaticClientSideMetadataProvider

use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.

the class SimpleClientServerTest method testAutoTransaction.

@Test
public void testAutoTransaction() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    String _baseDir = baseDir.toString();
    try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
        server.start();
        ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
        try (HDBClient client = new HDBClient(clientConfiguration);
            HDBConnection connection = client.openConnection()) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            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);
            DMLResult executeUpdateResult = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", TransactionContext.AUTOTRANSACTION_ID, false, true, Arrays.asList("test", 1, 2));
            long tx = executeUpdateResult.transactionId;
            long countInsert = executeUpdateResult.updateCount;
            Assert.assertEquals(1, countInsert);
            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")));
            connection.commitTransaction(TableSpace.DEFAULT, tx);
            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"));
                    assertEquals(RawString.of(ClientConfiguration.PROPERTY_CLIENT_USERNAME_DEFAULT), aa.get("username"));
                    assertNotNull(aa.get("connectionts"));
                }
                assertTrue(all.size() >= 1);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) Server(herddb.server.Server) RawString(herddb.utils.RawString) RawString(herddb.utils.RawString) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) Map(java.util.Map) Test(org.junit.Test)

Aggregations

StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)59 Server (herddb.server.Server)56 Test (org.junit.Test)54 ClientConfiguration (herddb.client.ClientConfiguration)41 HDBClient (herddb.client.HDBClient)41 Connection (java.sql.Connection)39 Statement (java.sql.Statement)36 PreparedStatement (java.sql.PreparedStatement)30 ResultSet (java.sql.ResultSet)29 Path (java.nio.file.Path)15 RawString (herddb.utils.RawString)8 ServerConfiguration (herddb.server.ServerConfiguration)7 Map (java.util.Map)7 SQLException (java.sql.SQLException)6 HashSet (java.util.HashSet)4 HDBConnection (herddb.client.HDBConnection)3 MissingJDBCParameterException (herddb.model.MissingJDBCParameterException)3 List (java.util.List)3 HDBOperationTimeoutException (herddb.client.impl.HDBOperationTimeoutException)2 Transaction (herddb.model.Transaction)2