Search in sources :

Example 6 with ConnectionInfo

use of org.h2.server.web.ConnectionInfo in project h2database by h2database.

the class TestReopen method logDb.

private synchronized void logDb(String fileName) {
    writeCount++;
    if ((writeCount & (testEvery - 1)) != 0) {
        return;
    }
    if (FileUtils.size(fileName) > maxFileSize) {
        // System.out.println(fileName + " " + IOUtils.length(fileName));
        return;
    }
    System.out.println("+ write #" + writeCount + " verify #" + verifyCount);
    try {
        if (fileName.endsWith(Constants.SUFFIX_PAGE_FILE)) {
            IOUtils.copyFiles(fileName, testDatabase + Constants.SUFFIX_PAGE_FILE);
        } else {
            IOUtils.copyFiles(fileName, testDatabase + Constants.SUFFIX_MV_FILE);
        }
        verifyCount++;
        // avoid using the Engine class to avoid deadlocks
        Properties p = new Properties();
        String userName = getUser();
        p.setProperty("user", userName);
        p.setProperty("password", getPassword());
        String url = "jdbc:h2:" + testDatabase + ";FILE_LOCK=NO;TRACE_LEVEL_FILE=0";
        ConnectionInfo ci = new ConnectionInfo(url, p);
        Database database = new Database(ci, null);
        // close the database
        Session session = database.getSystemSession();
        session.prepare("script to '" + testDatabase + ".sql'").query(0);
        session.prepare("shutdown immediately").update();
        database.removeSession(null);
        // everything OK - return
        return;
    } catch (DbException e) {
        SQLException e2 = DbException.toSQLException(e);
        int errorCode = e2.getErrorCode();
        if (errorCode == ErrorCode.WRONG_USER_OR_PASSWORD) {
            return;
        } else if (errorCode == ErrorCode.FILE_ENCRYPTION_ERROR_1) {
            return;
        }
        e.printStackTrace(System.out);
        throw e;
    } catch (Exception e) {
        // failed
        int errorCode = 0;
        if (e instanceof SQLException) {
            errorCode = ((SQLException) e).getErrorCode();
        }
        if (errorCode == ErrorCode.WRONG_USER_OR_PASSWORD) {
            return;
        } else if (errorCode == ErrorCode.FILE_ENCRYPTION_ERROR_1) {
            return;
        }
        e.printStackTrace(System.out);
    }
    System.out.println("begin ------------------------------ " + writeCount);
    try {
        Recover.execute(fileName.substring(0, fileName.lastIndexOf('/')), null);
    } catch (SQLException e) {
    // ignore
    }
    testDatabase += "X";
    try {
        if (fileName.endsWith(Constants.SUFFIX_PAGE_FILE)) {
            IOUtils.copyFiles(fileName, testDatabase + Constants.SUFFIX_PAGE_FILE);
        } else {
            IOUtils.copyFiles(fileName, testDatabase + Constants.SUFFIX_MV_FILE);
        }
        // avoid using the Engine class to avoid deadlocks
        Properties p = new Properties();
        String url = "jdbc:h2:" + testDatabase + ";FILE_LOCK=NO";
        ConnectionInfo ci = new ConnectionInfo(url, p);
        Database database = new Database(ci, null);
        // close the database
        database.removeSession(null);
    } catch (Exception e) {
        int errorCode = 0;
        if (e instanceof DbException) {
            e = ((DbException) e).getSQLException();
            errorCode = ((SQLException) e).getErrorCode();
        }
        if (errorCode == ErrorCode.WRONG_USER_OR_PASSWORD) {
            return;
        } else if (errorCode == ErrorCode.FILE_ENCRYPTION_ERROR_1) {
            return;
        }
        StringBuilder buff = new StringBuilder();
        StackTraceElement[] list = e.getStackTrace();
        for (int i = 0; i < 10 && i < list.length; i++) {
            buff.append(list[i].toString()).append('\n');
        }
        String s = buff.toString();
        if (!knownErrors.contains(s)) {
            System.out.println(writeCount + " code: " + errorCode + " " + e.toString());
            e.printStackTrace(System.out);
            knownErrors.add(s);
        } else {
            System.out.println(writeCount + " code: " + errorCode);
        }
    }
}
Also used : SQLException(java.sql.SQLException) Database(org.h2.engine.Database) ConnectionInfo(org.h2.engine.ConnectionInfo) Properties(java.util.Properties) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException) Session(org.h2.engine.Session) DbException(org.h2.message.DbException)

Example 7 with ConnectionInfo

use of org.h2.server.web.ConnectionInfo in project h2database by h2database.

the class TestConnectionInfo method testConnectionInfo.

private void testConnectionInfo() {
    Properties info = new Properties();
    ConnectionInfo connectionInfo = new ConnectionInfo("jdbc:h2:mem:" + getTestName() + ";LOG=2" + ";ACCESS_MODE_DATA=rws" + ";INIT=CREATE this...\\;INSERT that..." + ";IFEXISTS=TRUE", info);
    assertEquals("jdbc:h2:mem:" + getTestName(), connectionInfo.getURL());
    assertEquals("2", connectionInfo.getProperty("LOG", ""));
    assertEquals("rws", connectionInfo.getProperty("ACCESS_MODE_DATA", ""));
    assertEquals("CREATE this...;INSERT that...", connectionInfo.getProperty("INIT", ""));
    assertEquals("TRUE", connectionInfo.getProperty("IFEXISTS", ""));
    assertEquals("undefined", connectionInfo.getProperty("CACHE_TYPE", "undefined"));
}
Also used : ConnectionInfo(org.h2.engine.ConnectionInfo) Properties(java.util.Properties) SysProperties(org.h2.engine.SysProperties)

Example 8 with ConnectionInfo

use of org.h2.server.web.ConnectionInfo in project h2database by h2database.

the class H2Database method openDatabase.

/**
 * Open a connection to the given database.
 *
 * @param path the database file name
 * @param factory the cursor factory
 * @param flags 0, or a combination of OPEN_READONLY and CREATE_IF_NECESSARY
 * @return a connection to this database
 */
public static H2Database openDatabase(String path, H2Database.CursorFactory factory, int flags) {
    ConnectionInfo ci = new ConnectionInfo(path);
    if ((flags & OPEN_READWRITE) != 0) {
    // TODO readonly connections
    }
    if ((flags & CREATE_IF_NECESSARY) == 0) {
        ci.setProperty("IFEXISTS", "TRUE");
    }
    ci.setProperty("FILE_LOCK", "FS");
    Database db = new Database(ci, null);
    Session s = db.getSystemSession();
    return new H2Database(s, factory);
}
Also used : Database(org.h2.engine.Database) ConnectionInfo(org.h2.engine.ConnectionInfo) Session(org.h2.engine.Session)

Example 9 with ConnectionInfo

use of org.h2.server.web.ConnectionInfo in project h2database by h2database.

the class H2Database method create.

/**
 * Create a new in-memory database.
 *
 * @param factory the cursor factory
 * @return a connection to this database
 */
public static H2Database create(H2Database.CursorFactory factory) {
    ConnectionInfo ci = new ConnectionInfo("mem:");
    Database db = new Database(ci, null);
    Session s = db.getSystemSession();
    return new H2Database(s, factory);
}
Also used : Database(org.h2.engine.Database) ConnectionInfo(org.h2.engine.ConnectionInfo) Session(org.h2.engine.Session)

Example 10 with ConnectionInfo

use of org.h2.server.web.ConnectionInfo in project h2database by h2database.

the class Shell method connect.

private void connect() throws IOException, SQLException {
    String url = "jdbc:h2:~/test";
    String user = "";
    String driver = null;
    try {
        Properties prop;
        if ("null".equals(serverPropertiesDir)) {
            prop = new Properties();
        } else {
            prop = SortedProperties.loadProperties(serverPropertiesDir + "/" + Constants.SERVER_PROPERTIES_NAME);
        }
        String data = null;
        boolean found = false;
        for (int i = 0; ; i++) {
            String d = prop.getProperty(String.valueOf(i));
            if (d == null) {
                break;
            }
            found = true;
            data = d;
        }
        if (found) {
            ConnectionInfo info = new ConnectionInfo(data);
            url = info.url;
            user = info.user;
            driver = info.driver;
        }
    } catch (IOException e) {
    // ignore
    }
    println("[Enter]   " + url);
    print("URL       ");
    url = readLine(url).trim();
    if (driver == null) {
        driver = JdbcUtils.getDriver(url);
    }
    if (driver != null) {
        println("[Enter]   " + driver);
    }
    print("Driver    ");
    driver = readLine(driver).trim();
    println("[Enter]   " + user);
    print("User      ");
    user = readLine(user);
    println("[Enter]   Hide");
    print("Password  ");
    String password = readLine();
    if (password.length() == 0) {
        password = readPassword();
    }
    conn = JdbcUtils.getConnection(driver, url, user, password);
    stat = conn.createStatement();
    println("Connected");
}
Also used : ConnectionInfo(org.h2.server.web.ConnectionInfo) IOException(java.io.IOException) SortedProperties(org.h2.util.SortedProperties) Properties(java.util.Properties)

Aggregations

DbException (org.h2.message.DbException)10 ConnectionInfo (org.h2.engine.ConnectionInfo)9 Properties (java.util.Properties)8 IOException (java.io.IOException)5 SQLException (java.sql.SQLException)5 Database (org.h2.engine.Database)4 Session (org.h2.engine.Session)4 SysProperties (org.h2.engine.SysProperties)4 ConnectionInfo (org.h2.server.web.ConnectionInfo)3 SortedProperties (org.h2.util.SortedProperties)3 File (java.io.File)2 Field (java.lang.reflect.Field)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 Socket (java.net.Socket)2 JdbcConnection (org.h2.jdbc.JdbcConnection)2 Transfer (org.h2.value.Transfer)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 EOFException (java.io.EOFException)1