Search in sources :

Example 41 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class WebApp method tools.

private String tools() {
    try {
        String toolName = (String) attributes.get("tool");
        session.put("tool", toolName);
        String args = (String) attributes.get("args");
        String[] argList = StringUtils.arraySplit(args, ',', false);
        Tool tool = null;
        if ("Backup".equals(toolName)) {
            tool = new Backup();
        } else if ("Restore".equals(toolName)) {
            tool = new Restore();
        } else if ("Recover".equals(toolName)) {
            tool = new Recover();
        } else if ("DeleteDbFiles".equals(toolName)) {
            tool = new DeleteDbFiles();
        } else if ("ChangeFileEncryption".equals(toolName)) {
            tool = new ChangeFileEncryption();
        } else if ("Script".equals(toolName)) {
            tool = new Script();
        } else if ("RunScript".equals(toolName)) {
            tool = new RunScript();
        } else if ("ConvertTraceFile".equals(toolName)) {
            tool = new ConvertTraceFile();
        } else if ("CreateCluster".equals(toolName)) {
            tool = new CreateCluster();
        } else {
            throw DbException.throwInternalError(toolName);
        }
        ByteArrayOutputStream outBuff = new ByteArrayOutputStream();
        PrintStream out = new PrintStream(outBuff, false, "UTF-8");
        tool.setOut(out);
        try {
            tool.runTool(argList);
            out.flush();
            String o = new String(outBuff.toByteArray(), StandardCharsets.UTF_8);
            String result = PageParser.escapeHtml(o);
            session.put("toolResult", result);
        } catch (Exception e) {
            session.put("toolResult", getStackTrace(0, e, true));
        }
    } catch (Exception e) {
        server.traceError(e);
    }
    return "tools.jsp";
}
Also used : ConvertTraceFile(org.h2.tools.ConvertTraceFile) RunScript(org.h2.tools.RunScript) Script(org.h2.tools.Script) PrintStream(java.io.PrintStream) Backup(org.h2.tools.Backup) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Restore(org.h2.tools.Restore) DeleteDbFiles(org.h2.tools.DeleteDbFiles) DbException(org.h2.message.DbException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException) Recover(org.h2.tools.Recover) RunScript(org.h2.tools.RunScript) Tool(org.h2.util.Tool) ChangeFileEncryption(org.h2.tools.ChangeFileEncryption) CreateCluster(org.h2.tools.CreateCluster)

Example 42 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class Recover method writeRow.

private void writeRow(PrintWriter writer, Data s, Value[] data) {
    StringBuilder sb = new StringBuilder();
    sb.append("INSERT INTO ").append(storageName).append(" VALUES(");
    for (valueId = 0; valueId < recordLength; valueId++) {
        try {
            Value v = s.readValue();
            data[valueId] = v;
            if (valueId > 0) {
                sb.append(", ");
            }
            String columnName = storageName + "." + valueId;
            sb.append(getSQL(columnName, v));
        } catch (Exception e) {
            writeDataError(writer, "exception " + e, s.getBytes());
        } catch (OutOfMemoryError e) {
            writeDataError(writer, "out of memory", s.getBytes());
        }
    }
    sb.append(");");
    writer.println(sb.toString());
    if (storageId == 0) {
        try {
            SimpleRow r = new SimpleRow(data);
            MetaRecord meta = new MetaRecord(r);
            schema.add(meta);
            if (meta.getObjectType() == DbObject.TABLE_OR_VIEW) {
                String sql = data[3].getString();
                String name = extractTableOrViewName(sql);
                tableMap.put(meta.getId(), name);
            }
        } catch (Throwable t) {
            writeError(writer, t);
        }
    }
}
Also used : MetaRecord(org.h2.engine.MetaRecord) Value(org.h2.value.Value) SimpleRow(org.h2.result.SimpleRow) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 43 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class RegularTable method checkDeadlock.

@Override
public ArrayList<Session> checkDeadlock(Session session, Session clash, Set<Session> visited) {
    // only one deadlock check at any given time
    synchronized (RegularTable.class) {
        if (clash == null) {
            // verification is started
            clash = session;
            visited = new HashSet<>();
        } else if (clash == session) {
            // we found a circle where this session is involved
            return New.arrayList();
        } else if (visited.contains(session)) {
            // find it out themselves
            return null;
        }
        visited.add(session);
        ArrayList<Session> error = null;
        for (Session s : lockSharedSessions) {
            if (s == session) {
                // it doesn't matter if we have locked the object already
                continue;
            }
            Table t = s.getWaitForLock();
            if (t != null) {
                error = t.checkDeadlock(s, clash, visited);
                if (error != null) {
                    error.add(session);
                    break;
                }
            }
        }
        if (error == null && lockExclusiveSession != null) {
            Table t = lockExclusiveSession.getWaitForLock();
            if (t != null) {
                error = t.checkDeadlock(lockExclusiveSession, clash, visited);
                if (error != null) {
                    error.add(session);
                }
            }
        }
        return error;
    }
}
Also used : Session(org.h2.engine.Session)

Example 44 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out 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 45 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class JdbcBlob method position.

/**
 * [Not supported] Searches a pattern and return the position.
 *
 * @param blobPattern the pattern to search
 * @param start the index, the first byte is at position 1
 * @return the position (first byte is at position 1), or -1 for not found
 */
@Override
public long position(Blob blobPattern, long start) throws SQLException {
    if (isDebugEnabled()) {
        debugCode("position(blobPattern, " + start + ");");
    }
    if (Constants.BLOB_SEARCH) {
        try {
            checkClosed();
            if (blobPattern == null) {
                return -1;
            }
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            InputStream in = blobPattern.getBinaryStream();
            while (true) {
                int x = in.read();
                if (x < 0) {
                    break;
                }
                out.write(x);
            }
            return position(out.toByteArray(), start);
        } catch (Exception e) {
            throw logAndConvert(e);
        }
    }
    throw unsupported("LOB subset");
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PipedInputStream(java.io.PipedInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)27 IOException (java.io.IOException)23 OutputStream (java.io.OutputStream)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 SQLException (java.sql.SQLException)17 DbException (org.h2.message.DbException)17 Random (java.util.Random)11 ResultSet (java.sql.ResultSet)10 InputStream (java.io.InputStream)9 Statement (java.sql.Statement)9 Connection (java.sql.Connection)7 PrintStream (java.io.PrintStream)6 Properties (java.util.Properties)6 Task (org.h2.util.Task)6 BufferedOutputStream (java.io.BufferedOutputStream)5 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 InputStreamReader (java.io.InputStreamReader)5 PipedInputStream (java.io.PipedInputStream)5 OutputStreamWriter (java.io.OutputStreamWriter)4