示例#1
0
 private Transfer initTransfer(ConnectionInfo ci, String db, String server) throws IOException {
   Socket socket = NetUtils.createSocket(server, Constants.DEFAULT_TCP_PORT, ci.isSSL());
   Transfer trans = new Transfer(this, socket);
   trans.setSSL(ci.isSSL());
   trans.init();
   trans.writeInt(Constants.TCP_PROTOCOL_VERSION_6);
   trans.writeInt(Constants.TCP_PROTOCOL_VERSION_12);
   trans.writeString(db);
   trans.writeString(ci.getOriginalURL());
   trans.writeString(ci.getUserName());
   trans.writeBytes(ci.getUserPasswordHash());
   trans.writeBytes(ci.getFilePasswordHash());
   String[] keys = ci.getKeys();
   trans.writeInt(keys.length);
   for (String key : keys) {
     trans.writeString(key).writeString(ci.getProperty(key));
   }
   try {
     done(trans);
     clientVersion = trans.readInt();
     trans.setVersion(clientVersion);
     trans.writeInt(SessionRemote.SESSION_SET_ID);
     trans.writeString(sessionId);
     done(trans);
   } catch (DbException e) {
     trans.close();
     throw e;
   }
   autoCommit = true;
   return trans;
 }
示例#2
0
  protected JdbcConnection createJdbcConnection(String password) throws SQLException {
    Properties info = new Properties();
    info.put("MODE", "PostgreSQL");
    info.put("USER", userName);
    info.put("PASSWORD", password);
    String url = "jdbc:lealone:" + databaseName;
    ConnectionInfo ci = new ConnectionInfo(url, info);
    String baseDir = server.getBaseDir();
    if (baseDir == null) {
      baseDir = SysProperties.getBaseDir();
    }
    if (baseDir != null) {
      ci.setBaseDir(baseDir);
    }
    if (server.getIfExists()) {
      ci.setProperty("IFEXISTS", "TRUE");
    }

    return new JdbcConnection(ci, false);
  }
示例#3
0
  public static SessionRemote getSessionRemote(Properties info, String url) {
    SessionRemote sr = getQueue(url).poll();

    if (sr == null || sr.isClosed()) {
      byte[] userPasswordHash = null;
      byte[] filePasswordHash = null;
      Properties prop = new Properties();
      String key;
      for (Object o : info.keySet()) {
        key = o.toString();

        if (key.equalsIgnoreCase("_userPasswordHash_")) userPasswordHash = (byte[]) info.get(key);
        else if (key.equalsIgnoreCase("_filePasswordHash_"))
          filePasswordHash = (byte[]) info.get(key);
        else prop.setProperty(key, info.getProperty(key));
      }
      ConnectionInfo ci = new ConnectionInfo(url, prop);
      ci.setUserPasswordHash(userPasswordHash);
      ci.setFilePasswordHash(filePasswordHash);
      sr = (SessionRemote) new SessionRemote(ci).connectEmbeddedOrServer(false);
    }

    return sr;
  }
示例#4
0
 public String getURL() {
   return connectionInfo.getURL();
 }
示例#5
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;
    }
  }
示例#6
0
 /**
  * Open a new (remote or embedded) session.
  *
  * @param openNew whether to open a new session in any case
  * @return the session
  */
 public SessionInterface connectEmbeddedOrServer(boolean openNew) {
   ConnectionInfo ci = connectionInfo;
   if (ci.isRemote() || ci.isDynamic()) {
     connectServer(ci);
     return this;
   }
   // create the session using reflection,
   // so that the JDBC layer can be compiled without it
   boolean autoServerMode = Boolean.valueOf(ci.getProperty("AUTO_SERVER", "false")).booleanValue();
   ConnectionInfo backup = null;
   try {
     if (autoServerMode) {
       backup = (ConnectionInfo) ci.clone();
       connectionInfo = (ConnectionInfo) ci.clone();
     }
     if (openNew) {
       ci.setProperty("OPEN_NEW", "true");
     }
     if (sessionFactory == null) {
       sessionFactory = ci.getSessionFactory();
     }
     return sessionFactory.createSession(ci);
   } catch (Exception re) {
     DbException e = DbException.convert(re);
     if (e.getErrorCode() == ErrorCode.DATABASE_ALREADY_OPEN_1) {
       if (autoServerMode) {
         String serverKey = ((JdbcSQLException) e.getSQLException()).getSQL();
         if (serverKey != null) {
           backup.setServerKey(serverKey);
           // OPEN_NEW must be removed now, otherwise
           // opening a session with AUTO_SERVER fails
           // if another connection is already open
           backup.removeProperty("OPEN_NEW", null);
           connectServer(backup);
           return this;
         }
       }
     }
     throw e;
   }
 }