private void setAutoCommitSend(boolean autoCommit) {
   if (clientVersion >= Constants.TCP_PROTOCOL_VERSION_8) {
     for (int i = 0, count = 0; i < transferList.size(); i++) {
       Transfer transfer = transferList.get(i);
       try {
         traceOperation("SESSION_SET_AUTOCOMMIT", autoCommit ? 1 : 0);
         transfer.writeInt(SessionRemote.SESSION_SET_AUTOCOMMIT).writeBoolean(autoCommit);
         done(transfer);
       } catch (IOException e) {
         removeServer(e, i--, ++count);
       }
     }
   } else {
     if (autoCommit) {
       if (autoCommitTrue == null) {
         autoCommitTrue = prepareCommand("SET AUTOCOMMIT TRUE", Integer.MAX_VALUE);
       }
       autoCommitTrue.executeUpdate();
     } else {
       if (autoCommitFalse == null) {
         autoCommitFalse = prepareCommand("SET AUTOCOMMIT FALSE", Integer.MAX_VALUE);
       }
       autoCommitFalse.executeUpdate();
     }
   }
 }
Beispiel #2
0
 /**
  * Executes a query (select statement) and returns the result set. If another result set exists
  * for this statement, this will be closed (even if this statement fails).
  *
  * @param sql the SQL statement to execute
  * @return the result set
  */
 public ResultSet executeQuery(String sql) throws SQLException {
   try {
     int id = getNextId(TraceObject.RESULT_SET);
     if (isDebugEnabled()) {
       debugCodeAssign(
           "ResultSet", TraceObject.RESULT_SET, id, "executeQuery(" + quote(sql) + ")");
     }
     checkClosed();
     closeOldResultSet();
     sql = conn.translateSQL(sql, escapeProcessing);
     synchronized (session) {
       CommandInterface command = conn.prepareCommand(sql, fetchSize);
       ResultInterface result;
       boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY;
       boolean updatable = resultSetConcurrency == ResultSet.CONCUR_UPDATABLE;
       setExecutingStatement(command);
       try {
         result = command.executeQuery(maxRows, scrollable);
       } finally {
         setExecutingStatement(null);
       }
       command.close();
       resultSet =
           new JdbcResultSet(conn, this, result, id, closedByResultSet, scrollable, updatable);
     }
     return resultSet;
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
Beispiel #3
0
 /**
  * Changes the current transaction isolation level. Calling this method will commit an open
  * transaction, even if the new level is the same as the old one, except if the level is not
  * supported. Internally, this method calls SET LOCK_MODE. The following isolation levels are
  * supported:
  *
  * <ul>
  *   <li>Connection.TRANSACTION_READ_UNCOMMITTED = SET LOCK_MODE 0: no locking (should only be
  *       used for testing).
  *   <li>Connection.TRANSACTION_SERIALIZABLE = SET LOCK_MODE 1: table level locking.
  *   <li>Connection.TRANSACTION_READ_COMMITTED = SET LOCK_MODE 3: table level locking, but read
  *       locks are released immediately (default).
  * </ul>
  *
  * This setting is not persistent. Please note that using TRANSACTION_READ_UNCOMMITTED while at
  * the same time using multiple connections may result in inconsistent transactions.
  *
  * @param level the new transaction isolation level: Connection.TRANSACTION_READ_UNCOMMITTED,
  *     Connection.TRANSACTION_READ_COMMITTED, or Connection.TRANSACTION_SERIALIZABLE
  * @throws SQLException if the connection is closed or the isolation level is not supported
  */
 public void setTransactionIsolation(int level) throws SQLException {
   try {
     debugCodeCall("setTransactionIsolation", level);
     checkClosed();
     int lockMode;
     switch (level) {
       case Connection.TRANSACTION_READ_UNCOMMITTED:
         lockMode = Constants.LOCK_MODE_OFF;
         break;
       case Connection.TRANSACTION_READ_COMMITTED:
         lockMode = Constants.LOCK_MODE_READ_COMMITTED;
         break;
       case Connection.TRANSACTION_REPEATABLE_READ:
       case Connection.TRANSACTION_SERIALIZABLE:
         lockMode = Constants.LOCK_MODE_TABLE;
         break;
       default:
         throw DbException.getInvalidValueException("" + level, "level");
     }
     commit();
     setLockMode = prepareCommand("SET LOCK_MODE ?", setLockMode);
     setLockMode.getParameters().get(0).setValue(ValueInt.get(lockMode), false);
     setLockMode.executeUpdate();
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
Beispiel #4
0
 /**
  * Executes an arbitrary statement. If another result set exists for this statement, this will be
  * closed (even if this statement fails).
  *
  * <p>If the statement is a create or drop and does not throw an exception, the current
  * transaction (if any) is committed after executing the statement. If auto commit is on, and the
  * statement is not a select, this statement will be committed.
  *
  * @param sql the SQL statement to execute
  * @return true if a result set is available, false if not
  */
 public boolean execute(String sql) throws SQLException {
   try {
     int id = getNextId(TraceObject.RESULT_SET);
     if (isDebugEnabled()) {
       debugCodeCall("execute", sql);
     }
     checkClosedForWrite();
     closeOldResultSet();
     sql = conn.translateSQL(sql, escapeProcessing);
     CommandInterface command = conn.prepareCommand(sql, fetchSize);
     boolean returnsResultSet;
     synchronized (session) {
       setExecutingStatement(command);
       try {
         if (command.isQuery()) {
           returnsResultSet = true;
           boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY;
           boolean updatable = resultSetConcurrency == ResultSet.CONCUR_UPDATABLE;
           ResultInterface result = command.executeQuery(maxRows, scrollable);
           resultSet =
               new JdbcResultSet(conn, this, result, id, closedByResultSet, scrollable, updatable);
         } else {
           returnsResultSet = false;
           updateCount = command.executeUpdate();
         }
       } finally {
         setExecutingStatement(null);
       }
     }
     command.close();
     return returnsResultSet;
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
Beispiel #5
0
 /** INTERNAL */
 public void setQueryTimeout(int seconds) throws SQLException {
   try {
     debugCodeCall("setQueryTimeout", seconds);
     checkClosed();
     setQueryTimeout = prepareCommand("SET QUERY_TIMEOUT ?", setQueryTimeout);
     setQueryTimeout.getParameters().get(0).setValue(ValueInt.get(seconds * 1000), false);
     setQueryTimeout.executeUpdate();
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
 private void checkClusterDisableAutoCommit(String serverList) {
   if (autoCommit && transferList.size() > 1) {
     setAutoCommitSend(false);
     CommandInterface c = prepareCommand("SET CLUSTER " + serverList, Integer.MAX_VALUE);
     // this will set autoCommit to false
     c.executeUpdate();
     // so we need to switch it on
     autoCommit = true;
     cluster = true;
   }
 }
Beispiel #7
0
  @Override
  public String format(PreparedStatement stmt) {
    try {
      if (stmt instanceof JdbcPreparedStatement) {
        String sql = (String) ReflectUtil.getValue(SQL_FIELD, stmt);

        boolean insert = false;
        if (sql.toUpperCase().startsWith("INSERT INTO")) {
          int pos = sql.indexOf("(");
          sql = sql.substring(0, pos) + "SET " + sql.substring(pos + 1);
          sql = sql.substring(0, sql.indexOf(" VALUES"));
          insert = true;
        }

        CommandInterface command = (CommandInterface) ReflectUtil.getValue(COMMAND_FIELD, stmt);
        ObjectArray<? extends ParameterInterface> parameters = command.getParameters();

        int pos = 0;
        for (int i = 0; i < parameters.size(); i++) {
          ParameterInterface parameter = parameters.get(i);
          Value value = parameter.getParamValue();

          if (insert) {
            String string = "=" + value;

            pos = sql.indexOf(',', pos);
            if (pos == -1) {
              pos = sql.indexOf(')');
              sql = sql.substring(0, pos) + string;
              break;
            }

            sql = sql.substring(0, pos) + string + sql.substring(pos);
            pos += string.length() + 1;
          } else {
            pos = sql.indexOf('?');
            sql = sql.substring(0, pos) + value + sql.substring(pos + 1);
          }
        }

        return sql;
      }
    } catch (Throwable t) {
      // $FALL-THROUGH$
    }

    return super.format(stmt);
  }
Beispiel #8
0
 /**
  * Returns the current transaction isolation level.
  *
  * @return the isolation level.
  * @throws SQLException if the connection is closed
  */
 public int getTransactionIsolation() throws SQLException {
   try {
     debugCodeCall("getTransactionIsolation");
     checkClosed();
     getLockMode = prepareCommand("CALL LOCK_MODE()", getLockMode);
     ResultInterface result = getLockMode.executeQuery(0, false);
     result.next();
     int lockMode = result.currentRow()[0].getInt();
     result.close();
     int transactionIsolationLevel;
     switch (lockMode) {
       case Constants.LOCK_MODE_OFF:
         transactionIsolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED;
         break;
       case Constants.LOCK_MODE_READ_COMMITTED:
         transactionIsolationLevel = Connection.TRANSACTION_READ_COMMITTED;
         break;
       case Constants.LOCK_MODE_TABLE:
       case Constants.LOCK_MODE_TABLE_GC:
         transactionIsolationLevel = Connection.TRANSACTION_SERIALIZABLE;
         break;
       default:
         throw DbException.throwInternalError("lockMode:" + lockMode);
     }
     return transactionIsolationLevel;
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
Beispiel #9
0
 /**
  * Gets the current catalog name.
  *
  * @return the catalog name
  * @throws SQLException if the connection is closed
  */
 public String getCatalog() throws SQLException {
   try {
     debugCodeCall("getCatalog");
     checkClosed();
     if (catalog == null) {
       CommandInterface cat = prepareCommand("CALL DATABASE()", Integer.MAX_VALUE);
       ResultInterface result = cat.executeQuery(0, false);
       result.next();
       catalog = result.currentRow()[0].getString();
       cat.close();
     }
     return catalog;
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
Beispiel #10
0
 /**
  * Switches auto commit on or off. Calling this function does not commit the current transaction.
  *
  * @param autoCommit true for auto commit on, false for off
  * @throws SQLException if the connection is closed
  */
 public synchronized void setAutoCommit(boolean autoCommit) throws SQLException {
   try {
     if (isDebugEnabled()) {
       debugCode("setAutoCommit(" + autoCommit + ");");
     }
     checkClosed();
     if (autoCommit) {
       setAutoCommitTrue = prepareCommand("SET AUTOCOMMIT TRUE", setAutoCommitTrue);
       setAutoCommitTrue.executeUpdate();
     } else {
       setAutoCommitFalse = prepareCommand("SET AUTOCOMMIT FALSE", setAutoCommitFalse);
       setAutoCommitFalse.executeUpdate();
     }
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
Beispiel #11
0
 private boolean getInternalAutoCommit() {
   getAutoCommit = prepareCommand("CALL AUTOCOMMIT()", getAutoCommit);
   ResultInterface result = getAutoCommit.executeQuery(0, false);
   result.next();
   boolean autoCommit = result.currentRow()[0].getBoolean().booleanValue();
   result.close();
   return autoCommit;
 }
Beispiel #12
0
 // ## Java 1.4 begin ##
 public Savepoint setSavepoint(String name) throws SQLException {
   try {
     int id = getNextId(TraceObject.SAVEPOINT);
     if (isDebugEnabled()) {
       debugCodeAssign(
           "Savepoint", TraceObject.SAVEPOINT, id, "setSavepoint(" + quote(name) + ")");
     }
     checkClosed();
     CommandInterface set =
         prepareCommand("SAVEPOINT " + JdbcSavepoint.getName(name, 0), Integer.MAX_VALUE);
     set.executeUpdate();
     JdbcSavepoint savepoint = new JdbcSavepoint(this, 0, name, trace, id);
     return savepoint;
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
Beispiel #13
0
 /**
  * Cancels a currently running statement. This method must be called from within another thread
  * than the execute method.
  *
  * @throws SQLException if this object is closed
  */
 public void cancel() throws SQLException {
   try {
     debugCodeCall("cancel");
     checkClosed();
     // executingCommand can be reset  by another thread
     CommandInterface c = executingCommand;
     try {
       if (c != null) {
         c.cancel();
       }
     } finally {
       setExecutingStatement(null);
     }
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
Beispiel #14
0
 /**
  * Read the serializer name from the persistent database settings.
  *
  * @return the serializer
  */
 private String readSerializationSettings() {
   String javaObjectSerializerFQN = null;
   CommandInterface ci =
       prepareCommand(
           "SELECT VALUE FROM INFORMATION_SCHEMA.SETTINGS "
               + " WHERE NAME='JAVA_OBJECT_SERIALIZER'",
           Integer.MAX_VALUE);
   try {
     ResultInterface result = ci.executeQuery(0, false);
     if (result.next()) {
       Value[] row = result.currentRow();
       javaObjectSerializerFQN = row[0].getString();
     }
   } finally {
     ci.close();
   }
   return javaObjectSerializerFQN;
 }
 /**
  * INTERNAL. Set the statement that is currently running.
  *
  * @param c the command
  */
 protected void setExecutingStatement(CommandInterface c) {
   if (c == null) {
     conn.setExecutingStatement(null);
   } else {
     conn.setExecutingStatement(this);
     lastExecutedCommandType = c.getCommandType();
   }
   executingCommand = c;
 }
 private int executeUpdateInternal(String sql) throws SQLException {
   checkClosedForWrite();
   try {
     closeOldResultSet();
     sql = JdbcConnection.translateSQL(sql, escapeProcessing);
     CommandInterface command = conn.prepareCommand(sql, fetchSize);
     synchronized (session) {
       setExecutingStatement(command);
       try {
         updateCount = command.executeUpdate();
       } finally {
         setExecutingStatement(null);
       }
     }
     command.close();
     return updateCount;
   } finally {
     afterWriting();
   }
 }
Beispiel #17
0
 /** INTERNAL */
 public int getQueryTimeout() throws SQLException {
   try {
     debugCodeCall("getQueryTimeout");
     checkClosed();
     getQueryTimeout =
         prepareCommand(
             "SELECT VALUE FROM INFORMATION_SCHEMA.SETTINGS WHERE NAME=?", getQueryTimeout);
     getQueryTimeout.getParameters().get(0).setValue(ValueString.get("QUERY_TIMEOUT"), false);
     ResultInterface result = getQueryTimeout.executeQuery(0, false);
     result.next();
     int queryTimeout = result.currentRow()[0].getInt();
     result.close();
     if (queryTimeout == 0) {
       return 0;
     }
     // round to the next second, otherwise 999 millis would return 0 seconds
     return (queryTimeout + 999) / 1000;
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
Beispiel #18
0
 /**
  * Executes a statement (insert, update, delete, create, drop) and returns the update count. If
  * another result set exists for this statement, this will be closed (even if this statement
  * fails).
  *
  * <p>If the statement is a create or drop and does not throw an exception, the current
  * transaction (if any) is committed after executing the statement. If auto commit is on, this
  * statement will be committed.
  *
  * @param sql the SQL statement
  * @return the update count (number of row affected by an insert, update or delete, or 0 if no
  *     rows or the statement was a create, drop, commit or rollback)
  * @throws SQLException if a database error occurred or a select statement was executed
  */
 public int executeUpdate(String sql) throws SQLException {
   try {
     debugCodeCall("executeUpdate", sql);
     checkClosedForWrite();
     closeOldResultSet();
     sql = conn.translateSQL(sql, escapeProcessing);
     CommandInterface command = conn.prepareCommand(sql, fetchSize);
     synchronized (session) {
       setExecutingStatement(command);
       try {
         updateCount = command.executeUpdate();
       } finally {
         setExecutingStatement(null);
       }
     }
     command.close();
     return updateCount;
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
Beispiel #19
0
 /**
  * Returns true if the database is read-only.
  *
  * @return if the database is read-only
  * @throws SQLException if the connection is closed
  */
 public boolean isReadOnly() throws SQLException {
   try {
     debugCodeCall("isReadOnly");
     checkClosed();
     getReadOnly = prepareCommand("CALL READONLY()", getReadOnly);
     ResultInterface result = getReadOnly.executeQuery(0, false);
     result.next();
     boolean readOnly = result.currentRow()[0].getBoolean().booleanValue();
     return readOnly;
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
Beispiel #20
0
 /**
  * Commits the current transaction. This call has only an effect if auto commit is switched off.
  *
  * @throws SQLException if the connection is closed
  */
 public synchronized void commit() throws SQLException {
   try {
     debugCodeCall("commit");
     checkClosedForWrite();
     try {
       commit = prepareCommand("COMMIT", commit);
       commit.executeUpdate();
     } finally {
       afterWriting();
     }
   } catch (Exception e) {
     throw logAndConvert(e);
   }
 }
 JdbcParameterMetaData(Trace trace, JdbcPreparedStatement prep, CommandInterface command, int id) {
   setTrace(trace, TraceObject.PARAMETER_META_DATA, id);
   this.prep = prep;
   this.parameters = command.getParameters();
   this.paramCount = parameters.size();
 }
Beispiel #22
0
 private void switchOffCluster() {
   CommandInterface ci = prepareCommand("SET CLUSTER ''", Integer.MAX_VALUE);
   ci.executeUpdate();
 }
Beispiel #23
0
 private void rollbackInternal() {
   rollback = prepareCommand("ROLLBACK", rollback);
   rollback.executeUpdate();
 }
Beispiel #24
0
 /** INTERNAL */
 ResultSet getGeneratedKeys(JdbcStatement stat, int id) {
   getGeneratedKeys = prepareCommand("CALL SCOPE_IDENTITY()", getGeneratedKeys);
   ResultInterface result = getGeneratedKeys.executeQuery(0, false);
   ResultSet rs = new JdbcResultSet(this, stat, result, id, false, true, false);
   return rs;
 }
Beispiel #25
0
 private CommandInterface closeAndSetNull(CommandInterface command) {
   if (command != null) {
     command.close();
   }
   return null;
 }