Search in sources :

Example 81 with Server

use of org.h2.tools.Server in project main by JohnPeng739.

the class H2ServerFactory method initPgServer.

/**
 * 初始化PG服务器
 */
private void initPgServer() {
    boolean enable = env.getProperty("h2.pg.enable", Boolean.class, false);
    if (enable) {
        int port = env.getProperty("h2.pg.port", Integer.class, 5435);
        String baseDir = env.getProperty("h2.pg.baseDir", String.class, "~/h2db");
        boolean daemon = env.getProperty("h2.pg.daemon", Boolean.class, true);
        boolean trace = env.getProperty("h2.pg.trace", Boolean.class, true);
        List<String> args = new ArrayList<>();
        args.add("-pgPort");
        args.add(String.valueOf(port));
        args.add("-baseDir");
        args.add(baseDir);
        if (daemon) {
            args.add("-pgDaemon");
        }
        if (trace) {
            args.add("-trace");
        }
        try {
            Server server = Server.createPgServer(args.toArray(new String[0]));
            server.start();
            if (logger.isDebugEnabled()) {
                logger.debug(String.format("Start H2 pg Server success, conf: %s.", StringUtils.merge(args, " ")));
            }
            this.pgServer = server;
        } catch (SQLException ex) {
            if (logger.isErrorEnabled()) {
                logger.error(String.format("Create H2 pg server fail, conf: %s.", StringUtils.merge(args, " ")));
            }
        }
    }
}
Also used : Server(org.h2.tools.Server) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList)

Example 82 with Server

use of org.h2.tools.Server in project main by JohnPeng739.

the class H2ServerFactory method initTcpServer.

/**
 * 初始化TCP服务器
 */
private void initTcpServer() {
    boolean enable = env.getProperty("h2.tcp.enable", Boolean.class, false);
    if (enable) {
        int port = env.getProperty("h2.tcp.port", Integer.class, 9092);
        String baseDir = env.getProperty("h2.tcp.baseDir", String.class, "~/h2db");
        boolean daemon = env.getProperty("h2.tcp.daemon", Boolean.class, true);
        boolean trace = env.getProperty("h2.tcp.trace", Boolean.class, true);
        List<String> args = new ArrayList<>();
        args.add("-tcpPort");
        args.add(String.valueOf(port));
        // args.add(baseDir);
        if (daemon) {
            args.add("-tcpDaemon");
        }
        if (trace) {
            args.add("-trace");
        }
        try {
            Server server = Server.createTcpServer(args.toArray(new String[0]));
            server.start();
            if (logger.isDebugEnabled()) {
                logger.debug(String.format("Start H2 TCP Server success, conf: %s.", StringUtils.merge(args, " ")));
            }
            this.tcpServer = server;
        } catch (SQLException ex) {
            if (logger.isErrorEnabled()) {
                logger.error(String.format("Create H2 TCP server fail, conf: %s.", StringUtils.merge(args, " ")));
            }
        }
    }
}
Also used : Server(org.h2.tools.Server) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList)

Example 83 with Server

use of org.h2.tools.Server in project jbpm by kiegroup.

the class JBPMHelper method startH2Server.

public static Server startH2Server() {
    try {
        // start h2 in memory database
        Server server = Server.createTcpServer(new String[0]);
        server.start();
        return server;
    } catch (Throwable t) {
        throw new RuntimeException("Could not start H2 server", t);
    }
}
Also used : Server(org.h2.tools.Server)

Example 84 with Server

use of org.h2.tools.Server in project spf4j by zolyfarkas.

the class JdbcSemaphoreTest method testMultiProcess2.

@Test
public void testMultiProcess2() throws SQLException, IOException, InterruptedException, ExecutionException, TimeoutException {
    int port = PORT.getAndIncrement();
    Server server = Server.createTcpServer(new String[] { "-tcpPort", Integer.toString(port), "-ifNotExists" }).start();
    try {
        File tempDB = File.createTempFile("test", "h2db");
        tempDB.deleteOnExit();
        String connStr = "jdbc:h2:tcp://localhost:" + port + "/nio:" + tempDB.getAbsolutePath() + ";AUTO_SERVER=TRUE";
        JdbcDataSource ds = new JdbcDataSource();
        ds.setURL(connStr);
        ds.setUser("sa");
        ds.setPassword("sa");
        createSchemaObjects(ds);
        JdbcSemaphore semaphore = new JdbcSemaphore(ds, "test_sem2", 1, true);
        String o1 = org.spf4j.base.Runtime.jrun(DecentSemaphoreHandler.class, 10000000, connStr, "test_sem2").toString();
        String o2 = org.spf4j.base.Runtime.jrun(DecentSemaphoreHandler.class, 10000000, connStr, "test_sem2").toString();
        Assert.assertTrue(semaphore.tryAcquire(1, TimeUnit.SECONDS));
        Assert.assertFalse(semaphore.tryAcquire(10, TimeUnit.SECONDS));
        LOG.debug("P1: {}", o1);
        LOG.debug("P2: {}", o2);
        String[] nr1 = o1.split("\n");
        String[] nr2 = o2.split("\n");
        int totatl = nr1.length + nr2.length;
        Set<String> numbers = new HashSet<>(totatl);
        numbers.addAll(Arrays.asList(nr1));
        numbers.addAll(Arrays.asList(nr2));
        Assert.assertEquals(totatl, numbers.size());
    } finally {
        JdbcHeartBeat.stopHeartBeats();
        server.shutdown();
    }
}
Also used : Server(org.h2.tools.Server) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Server (org.h2.tools.Server)47 Connection (java.sql.Connection)27 SQLException (java.sql.SQLException)22 Statement (java.sql.Statement)17 PreparedStatement (java.sql.PreparedStatement)15 IOException (java.io.IOException)13 ResultSet (java.sql.ResultSet)10 DbException (org.h2.message.DbException)9 Socket (java.net.Socket)8 Test (org.junit.Test)8 Properties (java.util.Properties)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 File (java.io.File)6 ServerSocket (java.net.ServerSocket)6 PrintStream (java.io.PrintStream)5 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)5 Task (org.h2.util.Task)5 TcpServer (org.h2.server.TcpServer)4 PgServer (org.h2.server.pg.PgServer)4 WebServer (org.h2.server.web.WebServer)4