Beispiel #1
0
 private static ServerSocket createServerSocketTry(int port, boolean ssl) {
   try {
     InetAddress bindAddress = getBindAddress();
     if (ssl) {
       return CipherFactory.createServerSocket(port, bindAddress);
     }
     if (bindAddress == null) {
       return new ServerSocket(port);
     }
     return new ServerSocket(port, 0, bindAddress);
   } catch (BindException be) {
     throw DbException.get(ErrorCode.EXCEPTION_OPENING_PORT_2, be, "" + port, be.toString());
   } catch (IOException e) {
     throw DbException.convertIOException(e, "port: " + port + " ssl: " + ssl);
   }
 }
Beispiel #2
0
 private int writeResultSet(ResultSet rs) throws SQLException {
   try {
     int rows = 0;
     ResultSetMetaData meta = rs.getMetaData();
     int columnCount = meta.getColumnCount();
     String[] row = new String[columnCount];
     int[] sqlTypes = new int[columnCount];
     for (int i = 0; i < columnCount; i++) {
       row[i] = meta.getColumnLabel(i + 1);
       sqlTypes[i] = meta.getColumnType(i + 1);
     }
     if (writeColumnHeader) {
       writeRow(row);
     }
     while (rs.next()) {
       for (int i = 0; i < columnCount; i++) {
         Object o;
         switch (sqlTypes[i]) {
           case Types.DATE:
             o = rs.getDate(i + 1);
             break;
           case Types.TIME:
             o = rs.getTime(i + 1);
             break;
           case Types.TIMESTAMP:
             o = rs.getTimestamp(i + 1);
             break;
           default:
             o = rs.getString(i + 1);
         }
         row[i] = o == null ? null : o.toString();
       }
       writeRow(row);
       rows++;
     }
     output.close();
     return rows;
   } catch (IOException e) {
     throw DbException.convertIOException(e, null);
   } finally {
     close();
     JdbcUtils.closeSilently(rs);
   }
 }
Beispiel #3
0
 private static void installPgCatalog(Statement stat) throws SQLException {
   Reader r = null;
   try {
     r =
         new InputStreamReader(
             new ByteArrayInputStream(
                 Utils.getResource("/com/codefollower/lealone/server/pg/pg_catalog.sql")));
     ScriptReader reader = new ScriptReader(r);
     while (true) {
       String sql = reader.readStatement();
       if (sql == null) {
         break;
       }
       stat.execute(sql);
     }
     reader.close();
   } catch (IOException e) {
     throw DbException.convertIOException(e, "Can not read pg_catalog resource");
   } finally {
     IOUtils.closeSilently(r);
   }
 }
Beispiel #4
0
  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) {
          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 = Boolean.valueOf(ci.getProperty("AUTO_RECONNECT", "false")).booleanValue();
    // AUTO_SERVER implies AUTO_RECONNECT
    boolean autoServer = Boolean.valueOf(ci.getProperty("AUTO_SERVER", "false")).booleanValue();
    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) Utils.loadUserClass(className).newInstance();
        } catch (Throwable e) {
          throw DbException.convert(e);
        }
      }
    }
    cipher = ci.getProperty("CIPHER");
    if (cipher != null) {
      fileEncryptionKey = MathUtils.secureRandomBytes(32);
    }

    String[] servers;
    if (ci.isDynamic()) {
      servers = new String[] {ci.getOnlineServer(server)};
    } else {
      servers = StringUtils.arraySplit(server, ',', true);

      if (servers.length > 1 && !ci.removeProperty("USE_H2_CLUSTER_MODE", false))
        servers = new String[] {servers[random.nextInt(servers.length)]};
    }
    int len = servers.length;
    transferList.clear();
    sessionId = StringUtils.convertBytesToHex(MathUtils.secureRandomBytes(32));
    // TODO cluster: support more than 2 connections
    boolean switchOffCluster = false;
    try {
      for (int i = 0; i < len; i++) {
        String s = servers[i];
        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;
    }
  }