Search in sources :

Example 41 with User

use of org.h2.engine.User in project h2database by h2database.

the class TestCluster method testClob.

private void testClob() 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;
    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 t1(id int, name clob)");
    stat.execute("insert into t1 values(1, repeat('Hello', 50))");
    conn.close();
    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);
    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);
    conn.close();
    n1.stop();
    n2.stop();
    deleteFiles();
}
Also used : Server(org.h2.tools.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 42 with User

use of org.h2.engine.User in project h2database by h2database.

the class TestCluster method testCase.

private void testCase() 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 serverList = "localhost:" + port1 + ",localhost:" + port2;
    String url1 = getURL("jdbc:h2:tcp://localhost:" + port1 + "/test", true);
    String url2 = getURL("jdbc:h2:tcp://localhost:" + port2 + "/test", true);
    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(name int)");
    assertTrue(conn.getAutoCommit());
    stat.execute("insert into test values(1)");
    conn.setAutoCommit(false);
    assertFalse(conn.getAutoCommit());
    stat.execute("insert into test values(2)");
    assertFalse(conn.getAutoCommit());
    conn.rollback();
    rs = stat.executeQuery("select * from test order by name");
    assertTrue(rs.next());
    assertFalse(rs.next());
    conn.close();
    // stop server 2, and test if only one server is available
    n2.stop();
    conn = getConnection(urlCluster, user, password);
    stat = conn.createStatement();
    rs = stat.executeQuery("select * from test");
    assertTrue(rs.next());
    assertEquals(1, rs.getInt(1));
    conn.close();
    n1.stop();
    deleteFiles();
}
Also used : Server(org.h2.tools.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 43 with User

use of org.h2.engine.User in project h2database by h2database.

the class TestOpenClose method testCase.

private void testCase() throws Exception {
    if (config.memory) {
        return;
    }
    org.h2.Driver.load();
    deleteDb("openClose");
    final String url = getURL("openClose;FILE_LOCK=NO", true);
    final String user = getUser(), password = getPassword();
    Connection conn = DriverManager.getConnection(url, user, password);
    conn.createStatement().execute("drop table employee if exists");
    conn.createStatement().execute("create table employee(id int primary key, name varchar, salary int)");
    conn.close();
    // previously using getSize(200, 1000);
    // but for Ubuntu, the default ulimit is 1024,
    // which breaks the test
    int len = getSize(10, 50);
    Task[] tasks = new Task[len];
    for (int i = 0; i < len; i++) {
        tasks[i] = new Task() {

            @Override
            public void call() throws SQLException {
                Connection c = DriverManager.getConnection(url, user, password);
                PreparedStatement prep = c.prepareStatement("insert into employee values(?, ?, 0)");
                int id = getNextId();
                prep.setInt(1, id);
                prep.setString(2, "employee " + id);
                prep.execute();
                c.close();
            }
        };
        tasks[i].execute();
    }
    // }
    for (int i = 0; i < len; i++) {
        tasks[i].get();
    }
    conn = DriverManager.getConnection(url, user, password);
    ResultSet rs = conn.createStatement().executeQuery("select count(*) from employee");
    rs.next();
    assertEquals(len, rs.getInt(1));
    conn.close();
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 44 with User

use of org.h2.engine.User in project h2database by h2database.

the class TestOptimizations method testAutoAnalyze.

private void testAutoAnalyze() throws SQLException {
    deleteDb("optimizations");
    Connection conn = getConnection("optimizations");
    Statement stat = conn.createStatement();
    ResultSet rs = stat.executeQuery("select value " + "from information_schema.settings where name='analyzeAuto'");
    int auto = rs.next() ? rs.getInt(1) : 0;
    if (auto != 0) {
        stat.execute("create table test(id int)");
        stat.execute("create user onlyInsert password ''");
        stat.execute("grant insert on test to onlyInsert");
        Connection conn2 = getConnection("optimizations", "onlyInsert", getPassword(""));
        Statement stat2 = conn2.createStatement();
        stat2.execute("insert into test select x " + "from system_range(1, " + (auto + 10) + ")");
        conn2.close();
    }
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 45 with User

use of org.h2.engine.User in project narayana by jbosstm.

the class PerformanceTestCommitMarkableResource method testCommitMarkableResource.

// @org.junit.Ignore
@Test
public void testCommitMarkableResource() throws Exception {
    System.out.println("testCommitMarkableResource: " + new Date());
    ConnectionPoolDataSource dataSource = null;
    DataSource recoveryDataSource = null;
    if (dbType.equals("oracle")) {
        // ORA-01795: maximum number of expressions in a list is 1000
        BeanPopulator.getDefaultInstance(JTAEnvironmentBean.class).setCommitMarkableResourceRecordDeleteBatchSize(1000);
        Class clazz = Class.forName("oracle.jdbc.pool.OracleConnectionPoolDataSource");
        dataSource = (ConnectionPoolDataSource) clazz.newInstance();
        clazz.getMethod("setDriverType", new Class[] { String.class }).invoke(dataSource, new Object[] { "thin" });
        clazz.getMethod("setServerName", new Class[] { String.class }).invoke(dataSource, new Object[] { "tywin.eng.hst.ams2.redhat.com" });
        clazz.getMethod("setNetworkProtocol", new Class[] { String.class }).invoke(dataSource, new Object[] { "tcp" });
        clazz.getMethod("setDatabaseName", new Class[] { String.class }).invoke(dataSource, new Object[] { "orcl" });
        clazz.getMethod("setUser", new Class[] { String.class }).invoke(dataSource, new Object[] { "dtf11" });
        clazz.getMethod("setPassword", new Class[] { String.class }).invoke(dataSource, new Object[] { "dtf11" });
        clazz.getMethod("setPortNumber", new Class[] { int.class }).invoke(dataSource, new Object[] { 1521 });
        recoveryDataSource = (DataSource) dataSource;
    } else if (dbType.equals("sybase")) {
        // wide table support?
        BeanPopulator.getDefaultInstance(JTAEnvironmentBean.class).setCommitMarkableResourceRecordDeleteBatchSize(2000);
        Class clazz = Class.forName("com.sybase.jdbc3.jdbc.SybConnectionPoolDataSource");
        dataSource = (ConnectionPoolDataSource) clazz.newInstance();
        clazz.getMethod("setServerName", new Class[] { String.class }).invoke(dataSource, new Object[] { "192.168.1.5" });
        clazz.getMethod("setDatabaseName", new Class[] { String.class }).invoke(dataSource, new Object[] { "LOCALHOST" });
        clazz.getMethod("setUser", new Class[] { String.class }).invoke(dataSource, new Object[] { "sa" });
        clazz.getMethod("setPassword", new Class[] { String.class }).invoke(dataSource, new Object[] { "sybase" });
        clazz.getMethod("setPortNumber", new Class[] { int.class }).invoke(dataSource, new Object[] { 5000 });
        Class clazz2 = Class.forName("com.sybase.jdbc3.jdbc.SybDataSource");
        recoveryDataSource = (DataSource) clazz2.newInstance();
        clazz2.getMethod("setServerName", new Class[] { String.class }).invoke(recoveryDataSource, new Object[] { "192.168.1.5" });
        clazz2.getMethod("setDatabaseName", new Class[] { String.class }).invoke(recoveryDataSource, new Object[] { "LOCALHOST" });
        clazz2.getMethod("setUser", new Class[] { String.class }).invoke(recoveryDataSource, new Object[] { "sa" });
        clazz2.getMethod("setPassword", new Class[] { String.class }).invoke(recoveryDataSource, new Object[] { "sybase" });
        clazz2.getMethod("setPortNumber", new Class[] { int.class }).invoke(recoveryDataSource, new Object[] { 5000 });
    } else if (dbType.equals("h2")) {
        // Smaller batch size as H2 uses a hashtable in the delete which is
        // inefficent for bytearray clause
        BeanPopulator.getDefaultInstance(JTAEnvironmentBean.class).setCommitMarkableResourceRecordDeleteBatchSize(100);
        dataSource = new JdbcDataSource();
        ((JdbcDataSource) dataSource).setURL("jdbc:h2:mem:JBTMDB;MVCC=TRUE;DB_CLOSE_DELAY=-1");
        recoveryDataSource = ((JdbcDataSource) dataSource);
    } else if (dbType.equals("postgres")) {
        dataSource = new PGConnectionPoolDataSource();
        ((PGConnectionPoolDataSource) dataSource).setPortNumber(5432);
        ((PGConnectionPoolDataSource) dataSource).setUser("dtf11");
        ((PGConnectionPoolDataSource) dataSource).setPassword("dtf11");
        ((PGConnectionPoolDataSource) dataSource).setServerName("tywin.eng.hst.ams2.redhat.com");
        ((PGConnectionPoolDataSource) dataSource).setDatabaseName("jbossts");
        recoveryDataSource = new PGSimpleDataSource();
        ((PGSimpleDataSource) recoveryDataSource).setPortNumber(5432);
        ((PGSimpleDataSource) recoveryDataSource).setUser("dtf11");
        ((PGSimpleDataSource) recoveryDataSource).setPassword("dtf11");
        ((PGSimpleDataSource) recoveryDataSource).setServerName("tywin.eng.hst.ams2.redhat.com");
        ((PGSimpleDataSource) recoveryDataSource).setDatabaseName("jbossts");
    } else if (dbType.equals("mysql")) {
        // com.mysql.jdbc.PacketTooBigException: Packet for query is too
        // large (1318148 > 1048576). You can change this value on the
        // server by setting the max_allowed_packet' variable
        BeanPopulator.getDefaultInstance(JTAEnvironmentBean.class).setCommitMarkableResourceRecordDeleteBatchSize(3500);
        dataSource = new MysqlConnectionPoolDataSource();
        // need paranoid as otherwise it sends a connection change user
        ((MysqlConnectionPoolDataSource) dataSource).setUrl("jdbc:mysql://tywin.eng.hst.ams2.redhat.com:3306/jbossts?user=dtf11&password=dtf11&paranoid=true");
        recoveryDataSource = (DataSource) dataSource;
    } else if (dbType.equals("db2")) {
        Class clazz = Class.forName("com.ibm.db2.jcc.DB2ConnectionPoolDataSource");
        dataSource = (ConnectionPoolDataSource) clazz.newInstance();
        clazz.getMethod("setServerName", new Class[] { String.class }).invoke(dataSource, new Object[] { "tywin.eng.hst.ams2.redhat.com" });
        clazz.getMethod("setDatabaseName", new Class[] { String.class }).invoke(dataSource, new Object[] { "BTDB1" });
        clazz.getMethod("setUser", new Class[] { String.class }).invoke(dataSource, new Object[] { "db2" });
        clazz.getMethod("setPassword", new Class[] { String.class }).invoke(dataSource, new Object[] { "db2" });
        clazz.getMethod("setDriverType", new Class[] { int.class }).invoke(dataSource, new Object[] { 4 });
        clazz.getMethod("setPortNumber", new Class[] { int.class }).invoke(dataSource, new Object[] { 50001 });
        Class clazz2 = Class.forName("com.ibm.db2.jcc.DB2DataSource");
        recoveryDataSource = (DataSource) clazz2.newInstance();
        clazz2.getMethod("setServerName", new Class[] { String.class }).invoke(recoveryDataSource, new Object[] { "tywin.eng.hst.ams2.redhat.com" });
        clazz2.getMethod("setDatabaseName", new Class[] { String.class }).invoke(recoveryDataSource, new Object[] { "BTDB1" });
        clazz2.getMethod("setUser", new Class[] { String.class }).invoke(recoveryDataSource, new Object[] { "db2" });
        clazz2.getMethod("setPassword", new Class[] { String.class }).invoke(recoveryDataSource, new Object[] { "db2" });
        clazz2.getMethod("setDriverType", new Class[] { int.class }).invoke(recoveryDataSource, new Object[] { 4 });
        clazz2.getMethod("setPortNumber", new Class[] { int.class }).invoke(recoveryDataSource, new Object[] { 50001 });
    } else if (dbType.equals("sqlserver")) {
        Class clazz = Class.forName("com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource");
        dataSource = (ConnectionPoolDataSource) clazz.newInstance();
        clazz.getMethod("setServerName", new Class[] { String.class }).invoke(dataSource, new Object[] { "dev30.mw.lab.eng.bos.redhat.com" });
        clazz.getMethod("setDatabaseName", new Class[] { String.class }).invoke(dataSource, new Object[] { "dballo01" });
        clazz.getMethod("setUser", new Class[] { String.class }).invoke(dataSource, new Object[] { "dballo01" });
        clazz.getMethod("setPassword", new Class[] { String.class }).invoke(dataSource, new Object[] { "dballo01" });
        clazz.getMethod("setSendStringParametersAsUnicode", new Class[] { Boolean.class }).invoke(dataSource, new Object[] { false });
        clazz.getMethod("setPortNumber", new Class[] { int.class }).invoke(dataSource, new Object[] { 3918 });
        recoveryDataSource = (DataSource) dataSource;
    }
    PooledConnection pooledConnection = dataSource.getPooledConnection();
    Utils.createTables(pooledConnection.getConnection());
    doTest(new Handler(dataSource, recoveryDataSource), "_testCommitMarkableResource_" + dbType);
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) PGConnectionPoolDataSource(org.postgresql.ds.PGConnectionPoolDataSource) JTAEnvironmentBean(com.arjuna.ats.jta.common.JTAEnvironmentBean) Date(java.util.Date) PGSimpleDataSource(org.postgresql.ds.PGSimpleDataSource) XADataSource(javax.sql.XADataSource) PGConnectionPoolDataSource(org.postgresql.ds.PGConnectionPoolDataSource) MysqlXADataSource(com.mysql.jdbc.jdbc2.optional.MysqlXADataSource) DataSource(javax.sql.DataSource) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) MysqlConnectionPoolDataSource(com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource) ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) PGXADataSource(org.postgresql.xa.PGXADataSource) PGConnectionPoolDataSource(org.postgresql.ds.PGConnectionPoolDataSource) MysqlConnectionPoolDataSource(com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource) ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) PooledConnection(javax.sql.PooledConnection) PGSimpleDataSource(org.postgresql.ds.PGSimpleDataSource) MysqlConnectionPoolDataSource(com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource) Test(org.junit.Test)

Aggregations

Connection (java.sql.Connection)36 SQLException (java.sql.SQLException)21 PreparedStatement (java.sql.PreparedStatement)17 Statement (java.sql.Statement)17 ResultSet (java.sql.ResultSet)16 Server (org.h2.tools.Server)15 DbException (org.h2.message.DbException)14 Column (org.h2.table.Column)12 ValueString (org.h2.value.ValueString)12 Properties (java.util.Properties)10 Database (org.h2.engine.Database)10 Schema (org.h2.schema.Schema)8 IOException (java.io.IOException)7 User (org.h2.engine.User)7 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)7 SimpleResultSet (org.h2.tools.SimpleResultSet)7 Value (org.h2.value.Value)7 PrintStream (java.io.PrintStream)6 Timestamp (java.sql.Timestamp)6 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)6