Search in sources :

Example 21 with Server

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

the class ConnectionPoolMaxActiveTest method testMaxActive.

@Test
public void testMaxActive() throws Exception {
    try (HerdDBEmbeddedDataSource dataSource = new HerdDBEmbeddedDataSource()) {
        dataSource.setMaxActive(20);
        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)");
        }
        Server server = dataSource.getServer();
        List<Connection> connections = new ArrayList<>();
        try {
            for (int i = 0; i < 10; i++) {
                Connection con = dataSource.getConnection();
                connections.add(con);
                try (Statement statement = con.createStatement()) {
                    assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k1" + i + "','name1')"));
                }
            }
            // this is the number of sockets
            assertTrue(server.getConnectionCount() >= 1);
        } finally {
            for (Connection c : connections) {
                c.close();
            }
        }
    }
}
Also used : Server(herddb.server.Server) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 22 with Server

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

the class GenericClientDataSourceTest method test.

@Test
public void test() throws Exception {
    try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
        server.start();
        try (HerdDBDataSource dataSource = new HerdDBDataSource()) {
            dataSource.setUrl(server.getJdbcUrl());
            try (Connection connection = dataSource.getConnection();
                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);
            }
        }
    }
}
Also used : Server(herddb.server.Server) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 23 with Server

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

the class HerdDBResultSetTest method getStatement.

@Test
public void getStatement() throws Exception {
    try (final Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
        server.start();
        server.waitForStandaloneBoot();
        try (final HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            try (final BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client)) {
                try (Connection con = dataSource.getConnection();
                    Statement statement = con.createStatement()) {
                    statement.execute("CREATE TABLE mytable (c1 int primary key)");
                }
                try (final Connection con = dataSource.getConnection();
                    final PreparedStatement statement = con.prepareStatement("select * from mytable");
                    final ResultSet rows = statement.executeQuery()) {
                    Assert.assertEquals(statement, rows.getStatement());
                }
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 24 with Server

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

the class SimpleJoinTest 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 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 a" + " INNER JOIN mytable b ON 1=1")) {
                    int count = 0;
                    while (rs.next()) {
                        count++;
                    }
                    assertEquals(9, count);
                }
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 25 with Server

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

the class TransactionIsolationTest 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)");
            statement.execute("CREATE TABLE mytable2 (key string primary key, name string)");
        }
        Server server = dataSource.getServer();
        try (Connection con = dataSource.getConnection();
            Statement statement = con.createStatement()) {
            assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k1','name1')"));
            assertEquals(Connection.TRANSACTION_READ_COMMITTED, con.getTransactionIsolation());
            assertEquals(TableSpace.DEFAULT, con.getSchema());
            assertTrue(con.getAutoCommit());
            con.setAutoCommit(false);
            {
                HerdDBConnection hCon = (HerdDBConnection) con;
                assertEquals(0, hCon.getTransactionId());
                // force the creation of a transaction, by issuing a DML command
                assertEquals(1, statement.executeUpdate("INSERT INTO mytable2 (key,name) values('c1','name1')"));
                long tx = hCon.getTransactionId();
                statement.executeQuery("SELECT * FROM mytable").close();
                Transaction transaction = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransaction(tx);
                // in TRANSACTION_READ_COMMITTED no lock is to be retained
                assertTrue(transaction.locks.get("mytable").isEmpty());
                con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
                statement.executeQuery("SELECT * FROM mytable").close();
                LockHandle lock = transaction.lookupLock("mytable", Bytes.from_string("k1"));
                assertFalse(lock.write);
                statement.executeQuery("SELECT * FROM mytable FOR UPDATE").close();
                lock = transaction.lookupLock("mytable", Bytes.from_string("k1"));
                assertTrue(lock.write);
                con.rollback();
                assertNull(server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransaction(tx));
            }
            // test SELECT ... FOR UPDATE
            {
                HerdDBConnection hCon = (HerdDBConnection) con;
                assertEquals(0, hCon.getTransactionId());
                con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
                // force the creation of a transaction, by issuing a DML command
                assertEquals(1, statement.executeUpdate("INSERT INTO mytable2 (key,name) values('c2','name1')"));
                long tx = hCon.getTransactionId();
                statement.executeQuery("SELECT * FROM mytable FOR UPDATE").close();
                Transaction transaction = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransaction(tx);
                LockHandle lock = transaction.lookupLock("mytable", Bytes.from_string("k1"));
                assertTrue(lock.write);
                con.rollback();
                assertNull(server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransaction(tx));
            }
        }
    }
}
Also used : LockHandle(herddb.utils.LockHandle) Server(herddb.server.Server) Transaction(herddb.model.Transaction) Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.Test)

Aggregations

Server (herddb.server.Server)119 Test (org.junit.Test)111 ServerConfiguration (herddb.server.ServerConfiguration)60 ClientConfiguration (herddb.client.ClientConfiguration)56 StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)56 Connection (java.sql.Connection)53 HDBClient (herddb.client.HDBClient)52 Statement (java.sql.Statement)47 ResultSet (java.sql.ResultSet)37 Table (herddb.model.Table)36 CreateTableStatement (herddb.model.commands.CreateTableStatement)35 InsertStatement (herddb.model.commands.InsertStatement)32 PreparedStatement (java.sql.PreparedStatement)30 HashSet (java.util.HashSet)24 AlterTableSpaceStatement (herddb.model.commands.AlterTableSpaceStatement)19 Path (java.nio.file.Path)19 DataScanner (herddb.model.DataScanner)18 TableSpaceManager (herddb.core.TableSpaceManager)15 HDBConnection (herddb.client.HDBConnection)14 Index (herddb.model.Index)14