Search in sources :

Example 1 with Trace

use of org.h2.message.Trace in project main by JohnPeng739.

the class H2ServerFactory method initWebServer.

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

Example 2 with Trace

use of org.h2.message.Trace in project h2database by h2database.

the class SessionRemote method connectServer.

private void connectServer(ConnectionInfo ci) {
    String name = ci.getName();
    if (name.startsWith("//")) {
        name = name.substring("//".length());
    }
    int idx = name.indexOf('/');
    if (idx < 0) {
        throw ci.getFormatException();
    }
    databaseName = name.substring(idx + 1);
    String server = name.substring(0, idx);
    traceSystem = new TraceSystem(null);
    String traceLevelFile = ci.getProperty(SetTypes.TRACE_LEVEL_FILE, null);
    if (traceLevelFile != null) {
        int level = Integer.parseInt(traceLevelFile);
        String prefix = getFilePrefix(SysProperties.CLIENT_TRACE_DIRECTORY);
        try {
            traceSystem.setLevelFile(level);
            if (level > 0 && level < 4) {
                String file = FileUtils.createTempFile(prefix, Constants.SUFFIX_TRACE_FILE, false, false);
                traceSystem.setFileName(file);
            }
        } catch (IOException e) {
            throw DbException.convertIOException(e, prefix);
        }
    }
    String traceLevelSystemOut = ci.getProperty(SetTypes.TRACE_LEVEL_SYSTEM_OUT, null);
    if (traceLevelSystemOut != null) {
        int level = Integer.parseInt(traceLevelSystemOut);
        traceSystem.setLevelSystemOut(level);
    }
    trace = traceSystem.getTrace(Trace.JDBC);
    String serverList = null;
    if (server.indexOf(',') >= 0) {
        serverList = StringUtils.quoteStringSQL(server);
        ci.setProperty("CLUSTER", Constants.CLUSTERING_ENABLED);
    }
    autoReconnect = ci.getProperty("AUTO_RECONNECT", false);
    // AUTO_SERVER implies AUTO_RECONNECT
    boolean autoServer = ci.getProperty("AUTO_SERVER", false);
    if (autoServer && serverList != null) {
        throw DbException.getUnsupportedException("autoServer && serverList != null");
    }
    autoReconnect |= autoServer;
    if (autoReconnect) {
        String className = ci.getProperty("DATABASE_EVENT_LISTENER");
        if (className != null) {
            className = StringUtils.trim(className, true, true, "'");
            try {
                eventListener = (DatabaseEventListener) JdbcUtils.loadUserClass(className).newInstance();
            } catch (Throwable e) {
                throw DbException.convert(e);
            }
        }
    }
    cipher = ci.getProperty("CIPHER");
    if (cipher != null) {
        fileEncryptionKey = MathUtils.secureRandomBytes(32);
    }
    String[] servers = StringUtils.arraySplit(server, ',', true);
    int len = servers.length;
    transferList.clear();
    sessionId = StringUtils.convertBytesToHex(MathUtils.secureRandomBytes(32));
    // TODO cluster: support more than 2 connections
    boolean switchOffCluster = false;
    try {
        for (String s : servers) {
            try {
                Transfer trans = initTransfer(ci, databaseName, s);
                transferList.add(trans);
            } catch (IOException e) {
                if (len == 1) {
                    throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, e, e + ": " + s);
                }
                switchOffCluster = true;
            }
        }
        checkClosed();
        if (switchOffCluster) {
            switchOffCluster();
        }
        checkClusterDisableAutoCommit(serverList);
    } catch (DbException e) {
        traceSystem.close();
        throw e;
    }
}
Also used : Transfer(org.h2.value.Transfer) IOException(java.io.IOException) TraceSystem(org.h2.message.TraceSystem) DbException(org.h2.message.DbException)

Example 3 with Trace

use of org.h2.message.Trace in project h2database by h2database.

the class WebApp method getMetaResultSet.

private ResultSet getMetaResultSet(Connection conn, String sql) throws SQLException {
    DatabaseMetaData meta = conn.getMetaData();
    if (isBuiltIn(sql, "@best_row_identifier")) {
        String[] p = split(sql);
        int scale = p[4] == null ? 0 : Integer.parseInt(p[4]);
        boolean nullable = Boolean.parseBoolean(p[5]);
        return meta.getBestRowIdentifier(p[1], p[2], p[3], scale, nullable);
    } else if (isBuiltIn(sql, "@catalogs")) {
        return meta.getCatalogs();
    } else if (isBuiltIn(sql, "@columns")) {
        String[] p = split(sql);
        return meta.getColumns(p[1], p[2], p[3], p[4]);
    } else if (isBuiltIn(sql, "@column_privileges")) {
        String[] p = split(sql);
        return meta.getColumnPrivileges(p[1], p[2], p[3], p[4]);
    } else if (isBuiltIn(sql, "@cross_references")) {
        String[] p = split(sql);
        return meta.getCrossReference(p[1], p[2], p[3], p[4], p[5], p[6]);
    } else if (isBuiltIn(sql, "@exported_keys")) {
        String[] p = split(sql);
        return meta.getExportedKeys(p[1], p[2], p[3]);
    } else if (isBuiltIn(sql, "@imported_keys")) {
        String[] p = split(sql);
        return meta.getImportedKeys(p[1], p[2], p[3]);
    } else if (isBuiltIn(sql, "@index_info")) {
        String[] p = split(sql);
        boolean unique = Boolean.parseBoolean(p[4]);
        boolean approx = Boolean.parseBoolean(p[5]);
        return meta.getIndexInfo(p[1], p[2], p[3], unique, approx);
    } else if (isBuiltIn(sql, "@primary_keys")) {
        String[] p = split(sql);
        return meta.getPrimaryKeys(p[1], p[2], p[3]);
    } else if (isBuiltIn(sql, "@procedures")) {
        String[] p = split(sql);
        return meta.getProcedures(p[1], p[2], p[3]);
    } else if (isBuiltIn(sql, "@procedure_columns")) {
        String[] p = split(sql);
        return meta.getProcedureColumns(p[1], p[2], p[3], p[4]);
    } else if (isBuiltIn(sql, "@schemas")) {
        return meta.getSchemas();
    } else if (isBuiltIn(sql, "@tables")) {
        String[] p = split(sql);
        String[] types = p[4] == null ? null : StringUtils.arraySplit(p[4], ',', false);
        return meta.getTables(p[1], p[2], p[3], types);
    } else if (isBuiltIn(sql, "@table_privileges")) {
        String[] p = split(sql);
        return meta.getTablePrivileges(p[1], p[2], p[3]);
    } else if (isBuiltIn(sql, "@table_types")) {
        return meta.getTableTypes();
    } else if (isBuiltIn(sql, "@type_info")) {
        return meta.getTypeInfo();
    } else if (isBuiltIn(sql, "@udts")) {
        String[] p = split(sql);
        int[] types;
        if (p[4] == null) {
            types = null;
        } else {
            String[] t = StringUtils.arraySplit(p[4], ',', false);
            types = new int[t.length];
            for (int i = 0; i < t.length; i++) {
                types[i] = Integer.parseInt(t[i]);
            }
        }
        return meta.getUDTs(p[1], p[2], p[3], types);
    } else if (isBuiltIn(sql, "@version_columns")) {
        String[] p = split(sql);
        return meta.getVersionColumns(p[1], p[2], p[3]);
    } else if (isBuiltIn(sql, "@memory")) {
        SimpleResultSet rs = new SimpleResultSet();
        rs.addColumn("Type", Types.VARCHAR, 0, 0);
        rs.addColumn("KB", Types.VARCHAR, 0, 0);
        rs.addRow("Used Memory", "" + Utils.getMemoryUsed());
        rs.addRow("Free Memory", "" + Utils.getMemoryFree());
        return rs;
    } else if (isBuiltIn(sql, "@info")) {
        SimpleResultSet rs = new SimpleResultSet();
        rs.addColumn("KEY", Types.VARCHAR, 0, 0);
        rs.addColumn("VALUE", Types.VARCHAR, 0, 0);
        rs.addRow("conn.getCatalog", conn.getCatalog());
        rs.addRow("conn.getAutoCommit", "" + conn.getAutoCommit());
        rs.addRow("conn.getTransactionIsolation", "" + conn.getTransactionIsolation());
        rs.addRow("conn.getWarnings", "" + conn.getWarnings());
        String map;
        try {
            map = "" + conn.getTypeMap();
        } catch (SQLException e) {
            map = e.toString();
        }
        rs.addRow("conn.getTypeMap", "" + map);
        rs.addRow("conn.isReadOnly", "" + conn.isReadOnly());
        rs.addRow("conn.getHoldability", "" + conn.getHoldability());
        addDatabaseMetaData(rs, meta);
        return rs;
    } else if (isBuiltIn(sql, "@attributes")) {
        String[] p = split(sql);
        return meta.getAttributes(p[1], p[2], p[3], p[4]);
    } else if (isBuiltIn(sql, "@super_tables")) {
        String[] p = split(sql);
        return meta.getSuperTables(p[1], p[2], p[3]);
    } else if (isBuiltIn(sql, "@super_types")) {
        String[] p = split(sql);
        return meta.getSuperTypes(p[1], p[2], p[3]);
    } else if (isBuiltIn(sql, "@prof_stop")) {
        if (profiler != null) {
            profiler.stopCollecting();
            SimpleResultSet rs = new SimpleResultSet();
            rs.addColumn("Top Stack Trace(s)", Types.VARCHAR, 0, 0);
            rs.addRow(profiler.getTop(3));
            profiler = null;
            return rs;
        }
    }
    return null;
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) SQLException(java.sql.SQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 4 with Trace

use of org.h2.message.Trace in project h2database by h2database.

the class WebApp method adminSave.

private String adminSave() {
    try {
        Properties prop = new SortedProperties();
        int port = Integer.decode((String) attributes.get("port"));
        prop.setProperty("webPort", String.valueOf(port));
        server.setPort(port);
        boolean allowOthers = Utils.parseBoolean((String) attributes.get("allowOthers"), false, false);
        prop.setProperty("webAllowOthers", String.valueOf(allowOthers));
        server.setAllowOthers(allowOthers);
        boolean ssl = Utils.parseBoolean((String) attributes.get("ssl"), false, false);
        prop.setProperty("webSSL", String.valueOf(ssl));
        server.setSSL(ssl);
        server.saveProperties(prop);
    } catch (Exception e) {
        trace(e.toString());
    }
    return admin();
}
Also used : SortedProperties(org.h2.util.SortedProperties) SysProperties(org.h2.engine.SysProperties) Properties(java.util.Properties) SortedProperties(org.h2.util.SortedProperties) DbException(org.h2.message.DbException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException)

Example 5 with Trace

use of org.h2.message.Trace in project h2database by h2database.

the class WebApp method logout.

private String logout() {
    try {
        Connection conn = session.getConnection();
        session.setConnection(null);
        session.remove("conn");
        session.remove("result");
        session.remove("tables");
        session.remove("user");
        session.remove("tool");
        if (conn != null) {
            if (session.getShutdownServerOnDisconnect()) {
                server.shutdown();
            } else {
                conn.close();
            }
        }
    } catch (Exception e) {
        trace(e.toString());
    }
    return "index.do";
}
Also used : Connection(java.sql.Connection) DbException(org.h2.message.DbException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException)

Aggregations

SQLException (java.sql.SQLException)16 DbException (org.h2.message.DbException)14 Connection (java.sql.Connection)11 ResultSet (java.sql.ResultSet)10 PreparedStatement (java.sql.PreparedStatement)9 Statement (java.sql.Statement)8 IOException (java.io.IOException)7 Savepoint (java.sql.Savepoint)7 Random (java.util.Random)7 ArrayList (java.util.ArrayList)5 Properties (java.util.Properties)5 TraceSystem (org.h2.message.TraceSystem)5 Server (org.h2.tools.Server)5 ValueString (org.h2.value.ValueString)5 SQLClientInfoException (java.sql.SQLClientInfoException)4 SysProperties (org.h2.engine.SysProperties)4 JdbcConnection (org.h2.jdbc.JdbcConnection)4 SortedProperties (org.h2.util.SortedProperties)4 Value (org.h2.value.Value)4 Socket (java.net.Socket)3