/** * Specifically, this opens a mem-only DB, populates it, starts a HyperSQL Server to server it, * and opens network JDBC Connection "netConn" to it, * * <p>Invoked before each test*() invocation by JUnit. */ protected void setUp() { try { Connection setupConn = DriverManager.getConnection("jdbc:hsqldb:mem:test", "SA", ""); setupConn.setAutoCommit(false); Statement st = setupConn.createStatement(); st.executeUpdate("SET PASSWORD 'sapwd'"); populate(st); st.close(); setupConn.commit(); setupConn.close(); } catch (SQLException se) { throw new RuntimeException("Failed to set up in-memory database", se); } try { server = new Server(); HsqlProperties properties = new HsqlProperties(); if (System.getProperty("VERBOSE") == null) { server.setLogWriter(null); server.setErrWriter(null); } else { properties.setProperty("server.silent", "false"); properties.setProperty("server.trace", "true"); } properties.setProperty("server.database.0", "mem:test"); properties.setProperty("server.dbname.0", ""); properties.setProperty("server.port", AbstractTestOdbc.portString); server.setProperties(properties); server.start(); try { Thread.sleep(1000); } catch (InterruptedException ie) { } } catch (Exception e) { throw new RuntimeException("Failed to set up in-memory database", e); } if (server.getState() != ServerConstants.SERVER_STATE_ONLINE) { throw new RuntimeException("Server failed to start up"); } try { netConn = DriverManager.getConnection("jdbc:odbc:" + dsnName, "SA", "sapwd"); // netConn.setAutoCommit(false); } catch (SQLException se) { if (se.getMessage().indexOf("No suitable driver") > -1) { throw new RuntimeException( "You must install the native library for Sun's jdbc:odbc " + "JDBC driver"); } if (se.getMessage().indexOf("Data source name not found") > -1) { throw new RuntimeException( "You must configure ODBC DSN '" + dsnName + "' (you may change the name and/or port by setting Java " + "system properties 'test.hsqlodbc.port' or " + "'test.hsqlodbc.dsnname'"); } throw new RuntimeException("Failed to set up JDBC/ODBC network connection", se); } }
private void runHsqldbServer(final String fileName, final int port, final char[] password) { org.hsqldb.server.Server hsqlServer = new org.hsqldb.server.Server(); hsqlServer.setPort(port); hsqlServer.setDatabaseName(0, "jgnash"); // the alias hsqlServer.setDatabasePath(0, "file:" + FileUtils.stripFileExtension(fileName)); hsqlServer.start(); // Start the message server and engine, this should block until closed if (!run(DataStoreType.HSQL_DATABASE, fileName, port, password)) { Logger.getLogger(JpaNetworkServer.class.getName()).severe("Failed to start the server"); } hsqlServer.stop(); }
/* * 启动数据库服务 */ public static boolean startHSQL() { if (mode == SERVER_MODE) { Server server = new Server(); // 它可是hsqldb.jar里面的类啊。 server.setDatabaseName(0, DB_NAME); server.setDatabasePath(0, DB_PATH + DB_NAME); server.setPort(PORT); server.setSilent(true); server.start(); // 自动多线程运行 System.out.println("hsqldb started..."); } else if (mode == STAND_ALONE_MODE) { // standalone模式,打开连接就同时启动数据库,所以这里可以什么都不做 } try { Thread.sleep(1000); // 等待Server启动 } catch (InterruptedException e) { } return true; }
@Override protected void before() throws Throwable { super.before(); File hsqlHome = new File("target/temporary/hsqldb/" + name); if (hsqlHome.exists()) { hsqlHome.delete(); } hsqlHome.mkdirs(); String dbHome = "file:" + hsqlHome.getAbsolutePath(); HsqlProperties p = new HsqlProperties(); p.setProperty("server.database.0", dbHome); p.setProperty("server.dbname.0", name); p.setProperty("server.port", port); server = new Server(); server.setProperties(p); server.setLogWriter(new PrintWriter(System.out)); server.setErrWriter(new PrintWriter(System.err)); server.start(); }