Search in sources :

Example 56 with Mode

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

the class MixedMode method main.

/**
 * This method is called when executing this sample application from the
 * command line.
 *
 * @param args the command line parameters
 */
public static void main(String... args) throws Exception {
    // start the server, allows to access the database remotely
    Server server = Server.createTcpServer("-tcpPort", "9081");
    server.start();
    System.out.println("You can access the database remotely now, using the URL:");
    System.out.println("jdbc:h2:tcp://localhost:9081/~/test (user: sa, password: sa)");
    // now use the database in your application in embedded mode
    Class.forName("org.h2.Driver");
    Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "sa");
    // some simple 'business usage'
    Statement stat = conn.createStatement();
    stat.execute("DROP TABLE TIMER IF EXISTS");
    stat.execute("CREATE TABLE TIMER(ID INT PRIMARY KEY, TIME VARCHAR)");
    System.out.println("Execute this a few times: " + "SELECT TIME FROM TIMER");
    System.out.println("To stop this application " + "(and the server), run: DROP TABLE TIMER");
    try {
        while (true) {
            // runs forever, except if you drop the table remotely
            stat.execute("MERGE INTO TIMER VALUES(1, NOW())");
            Thread.sleep(1000);
        }
    } catch (SQLException e) {
        System.out.println("Error: " + e.toString());
    }
    conn.close();
    // stop the server
    server.stop();
}
Also used : Server(org.h2.tools.Server) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 57 with Mode

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

the class TestTableEngines method testAffinityKey.

private void testAffinityKey() throws SQLException {
    deleteDb("tableEngine");
    Connection conn = getConnection("tableEngine;mode=Ignite;MV_STORE=FALSE");
    Statement stat = conn.createStatement();
    stat.executeUpdate("CREATE TABLE T(ID INT AFFINITY PRIMARY KEY, NAME VARCHAR, AGE INT)" + " ENGINE \"" + AffinityTableEngine.class.getName() + "\"");
    Table tbl = AffinityTableEngine.createdTbl;
    assertNotNull(tbl);
    assertEquals(3, tbl.getIndexes().size());
    Index aff = tbl.getIndexes().get(2);
    assertTrue(aff.getIndexType().isAffinity());
    assertEquals("T_AFF", aff.getName());
    assertEquals(1, aff.getIndexColumns().length);
    assertEquals("ID", aff.getIndexColumns()[0].columnName);
    conn.close();
    deleteDb("tableEngine");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) BaseIndex(org.h2.index.BaseIndex) Index(org.h2.index.Index)

Example 58 with Mode

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

the class FileReorderWrites method open.

@Override
public FileChannel open(String mode) throws IOException {
    InputStream in = newInputStream();
    FilePath copy = FilePath.get(getBase().toString() + ".copy");
    OutputStream out = copy.newOutputStream(false);
    IOUtils.copy(in, out);
    in.close();
    out.close();
    FileChannel base = getBase().open(mode);
    FileChannel readBase = copy.open(mode);
    return new FileReorderWrites(this, base, readBase);
}
Also used : FilePath(org.h2.store.fs.FilePath) InputStream(java.io.InputStream) FileChannel(java.nio.channels.FileChannel) OutputStream(java.io.OutputStream)

Example 59 with Mode

use of org.h2.engine.Mode in project jdbi by jdbi.

the class TestOnDemandSqlObject method setUp.

@Before
public void setUp() throws Exception {
    ds = new JdbcDataSource();
    // in MVCC mode h2 doesn't shut down immediately on all connections closed, so need random db name
    ds.setURL(String.format("jdbc:h2:mem:%s;MVCC=TRUE", UUID.randomUUID()));
    db = Jdbi.create(ds);
    db.installPlugin(new SqlObjectPlugin());
    handle = db.open();
    handle.execute("create table something (id int primary key, name varchar(100))");
    db.installPlugin(tracker);
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Before(org.junit.Before)

Example 60 with Mode

use of org.h2.engine.Mode in project jdbi by jdbi.

the class TestReentrancy method setUp.

@Before
public void setUp() throws Exception {
    JdbcDataSource ds = new JdbcDataSource();
    // in MVCC mode h2 doesn't shut down immediately on all connections closed, so need random db name
    ds.setURL(String.format("jdbc:h2:mem:%s;MVCC=TRUE", UUID.randomUUID()));
    db = Jdbi.create(ds);
    db.installPlugin(new SqlObjectPlugin());
    db.registerRowMapper(new SomethingMapper());
    handle = db.open();
    handle.execute("create table something (id int primary key, name varchar(100))");
}
Also used : SomethingMapper(org.jdbi.v3.core.mapper.SomethingMapper) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Before(org.junit.Before)

Aggregations

Connection (java.sql.Connection)12 Mode (org.h2.engine.Mode)12 ValueString (org.h2.value.ValueString)11 PreparedStatement (java.sql.PreparedStatement)9 Column (org.h2.table.Column)9 IOException (java.io.IOException)8 Statement (java.sql.Statement)8 ArrayList (java.util.ArrayList)8 DbException (org.h2.message.DbException)8 CompareMode (org.h2.value.CompareMode)8 Value (org.h2.value.Value)8 ResultSet (java.sql.ResultSet)7 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)7 SQLException (java.sql.SQLException)6 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)6 ValueExpression (org.h2.expression.ValueExpression)5 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)5 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)4 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)4 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)4