Search in sources :

Example 16 with ConnectionInfo

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

the class RecoverTester method testDatabase.

private synchronized void testDatabase(String fileName, PrintWriter out) {
    out.println("+ write #" + writeCount + " verify #" + verifyCount);
    try {
        IOUtils.copyFiles(fileName, testDatabase + Constants.SUFFIX_PAGE_FILE);
        String mvFileName = fileName.substring(0, fileName.length() - Constants.SUFFIX_PAGE_FILE.length()) + Constants.SUFFIX_MV_FILE;
        if (FileUtils.exists(mvFileName)) {
            IOUtils.copyFiles(mvFileName, testDatabase + Constants.SUFFIX_MV_FILE);
        }
        verifyCount++;
        // avoid using the Engine class to avoid deadlocks
        Properties p = new Properties();
        p.setProperty("user", "");
        p.setProperty("password", "");
        ConnectionInfo ci = new ConnectionInfo("jdbc:h2:" + testDatabase + ";FILE_LOCK=NO;TRACE_LEVEL_FILE=0", p);
        Database database = new Database(ci, null);
        // close the database
        Session sysSession = database.getSystemSession();
        sysSession.prepare("script to '" + testDatabase + ".sql'").query(0);
        sysSession.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);
    } 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);
    }
    out.println("begin ------------------------------ " + writeCount);
    try {
        Recover.execute(fileName.substring(0, fileName.lastIndexOf('/')), null);
    } catch (SQLException e) {
    // ignore
    }
    testDatabase += "X";
    try {
        IOUtils.copyFiles(fileName, testDatabase + Constants.SUFFIX_PAGE_FILE);
        // avoid using the Engine class to avoid deadlocks
        Properties p = new Properties();
        ConnectionInfo ci = new ConnectionInfo("jdbc:h2:" + testDatabase + ";FILE_LOCK=NO", 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)) {
            out.println(writeCount + " code: " + errorCode + " " + e.toString());
            e.printStackTrace(System.out);
            knownErrors.add(s);
        } else {
            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) IOException(java.io.IOException) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException) Session(org.h2.engine.Session) DbException(org.h2.message.DbException)

Example 17 with ConnectionInfo

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

the class WebServer method getSettings.

/**
 * Get the list of connection info objects.
 *
 * @return the list
 */
synchronized ArrayList<ConnectionInfo> getSettings() {
    ArrayList<ConnectionInfo> settings = New.arrayList();
    if (connInfoMap.size() == 0) {
        Properties prop = loadProperties();
        if (prop.size() == 0) {
            for (String gen : GENERIC) {
                ConnectionInfo info = new ConnectionInfo(gen);
                settings.add(info);
                updateSetting(info);
            }
        } else {
            for (int i = 0; ; i++) {
                String data = prop.getProperty(String.valueOf(i));
                if (data == null) {
                    break;
                }
                ConnectionInfo info = new ConnectionInfo(data);
                settings.add(info);
                updateSetting(info);
            }
        }
    } else {
        settings.addAll(connInfoMap.values());
    }
    Collections.sort(settings);
    return settings;
}
Also used : SortedProperties(org.h2.util.SortedProperties) SysProperties(org.h2.engine.SysProperties) Properties(java.util.Properties)

Example 18 with ConnectionInfo

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

the class DbUpgrade method upgrade.

private static void upgrade(ConnectionInfo ci, Properties info) throws SQLException {
    String name = ci.getName();
    String data = name + Constants.SUFFIX_OLD_DATABASE_FILE;
    String index = name + ".index.db";
    String lobs = name + ".lobs.db";
    String backupData = data + ".backup";
    String backupIndex = index + ".backup";
    String backupLobs = lobs + ".backup";
    String script = null;
    try {
        if (scriptInTempDir) {
            new File(Utils.getProperty("java.io.tmpdir", ".")).mkdirs();
            script = File.createTempFile("h2dbmigration", "backup.sql").getAbsolutePath();
        } else {
            script = name + ".script.sql";
        }
        String oldUrl = "jdbc:h2v1_1:" + name + ";UNDO_LOG=0;LOG=0;LOCK_MODE=0";
        String cipher = ci.getProperty("CIPHER", null);
        if (cipher != null) {
            oldUrl += ";CIPHER=" + cipher;
        }
        Connection conn = DriverManager.getConnection(oldUrl, info);
        Statement stat = conn.createStatement();
        String uuid = UUID.randomUUID().toString();
        if (cipher != null) {
            stat.execute("script to '" + script + "' cipher aes password '" + uuid + "' --hide--");
        } else {
            stat.execute("script to '" + script + "'");
        }
        conn.close();
        FileUtils.move(data, backupData);
        FileUtils.move(index, backupIndex);
        if (FileUtils.exists(lobs)) {
            FileUtils.move(lobs, backupLobs);
        }
        ci.removeProperty("IFEXISTS", false);
        conn = new JdbcConnection(ci, true);
        stat = conn.createStatement();
        if (cipher != null) {
            stat.execute("runscript from '" + script + "' cipher aes password '" + uuid + "' --hide--");
        } else {
            stat.execute("runscript from '" + script + "'");
        }
        stat.execute("analyze");
        stat.execute("shutdown compact");
        stat.close();
        conn.close();
        if (deleteOldDb) {
            FileUtils.delete(backupData);
            FileUtils.delete(backupIndex);
            FileUtils.deleteRecursive(backupLobs, false);
        }
    } catch (Exception e) {
        if (FileUtils.exists(backupData)) {
            FileUtils.move(backupData, data);
        }
        if (FileUtils.exists(backupIndex)) {
            FileUtils.move(backupIndex, index);
        }
        if (FileUtils.exists(backupLobs)) {
            FileUtils.move(backupLobs, lobs);
        }
        FileUtils.delete(name + ".h2.db");
        throw DbException.toSQLException(e);
    } finally {
        if (script != null) {
            FileUtils.delete(script);
        }
    }
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) JdbcConnection(org.h2.jdbc.JdbcConnection) File(java.io.File) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 19 with ConnectionInfo

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

the class TestConnectionInfo method testName.

private void testName() throws Exception {
    char differentFileSeparator = File.separatorChar == '/' ? '\\' : '/';
    ConnectionInfo connectionInfo = new ConnectionInfo("./test" + differentFileSeparator + "subDir");
    File file = new File("test" + File.separatorChar + "subDir");
    assertEquals(file.getCanonicalPath().replace('\\', '/'), connectionInfo.getName());
}
Also used : ConnectionInfo(org.h2.engine.ConnectionInfo) File(java.io.File)

Example 20 with ConnectionInfo

use of org.h2.engine.ConnectionInfo in project traccar by traccar.

the class ConsoleServlet method init.

@Override
public void init() {
    super.init();
    try {
        Field field = WebServlet.class.getDeclaredField("server");
        field.setAccessible(true);
        org.h2.server.web.WebServer server = (org.h2.server.web.WebServer) field.get(this);
        ConnectionInfo connectionInfo = new ConnectionInfo("Traccar|" + Context.getConfig().getString("database.driver") + "|" + Context.getConfig().getString("database.url") + "|" + Context.getConfig().getString("database.user"));
        Method method;
        method = org.h2.server.web.WebServer.class.getDeclaredMethod("updateSetting", ConnectionInfo.class);
        method.setAccessible(true);
        method.invoke(server, connectionInfo);
        method = org.h2.server.web.WebServer.class.getDeclaredMethod("setAllowOthers", boolean.class);
        method.setAccessible(true);
        method.invoke(server, true);
    } catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        Log.warning(e);
    }
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) ConnectionInfo(org.h2.server.web.ConnectionInfo)

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