Retrieves log records from the database ready for recovery
processing.

@exception SQLException thrown if a SQLException is
encountered when accessing the
Database.
@exception InternalLogException Thrown if an
unexpected error has occured.
554062
private void recover(Connection conn) throws SQLException, RecoverableUnitSectionExistsException, InternalLogException {
        if (tc.isEntryEnabled())
            Tr.entry(tc, "recover", conn);

        Statement recoveryStmt = null;
        ResultSet recoveryRS = null;

        try {
            recoveryStmt = conn.createStatement();
            String queryString = "SELECT RU_ID, RUSECTION_ID, RUSECTION_DATA_INDEX, DATA" +
                                 " FROM " + _recoveryTableName + _logIdentifierString + _recoveryTableNameSuffix +
                                 " WHERE SERVER_NAME='" + _serverName +
                                 "' AND SERVICE_ID=" + _recoveryAgent.clientIdentifier();
            if (tc.isDebugEnabled())
                Tr.debug(tc, "Retrieve all rows from table using - " + queryString);

            recoveryRS = recoveryStmt.executeQuery(queryString);

            while (recoveryRS.next()) {
                final long ruId = recoveryRS.getLong(1);
                if (ruId != -1) {
                    SQLRecoverableUnitImpl ru = (SQLRecoverableUnitImpl) _recoverableUnits.get(ruId);
                    if (ru == null) {
                        if (tc.isDebugEnabled())
                            Tr.debug(tc, "Creating ru with id: " + ruId);
                        ru = new SQLRecoverableUnitImpl(this, ruId, _failureScope, true);
                    }

                    final long sectId = recoveryRS.getLong(2);
                    final int index = recoveryRS.getInt(3);
                    final byte[] data = recoveryRS.getBytes(4);

                    if (tc.isEventEnabled()) {
                        Tr.event(tc, "sql tranlog: read ruId: " + ruId);
                        Tr.event(tc, "sql tranlog: read sectionId: " + sectId);
                        Tr.event(tc, "sql tranlog: read item: " + index);
                        Tr.event(tc, "sql tranlog: read data: " + RLSUtils.toHexString(data, RLSUtils.MAX_DISPLAY_BYTES));
                    }

                    SQLRecoverableUnitSectionImpl sect = (SQLRecoverableUnitSectionImpl) ru.lookupSection((int) sectId);
                    if (sect == null) {
                        sect = (SQLRecoverableUnitSectionImpl) ru.createSection((int) sectId, index == 0);
                    }

                    sect.addData(index, data);
                } else if (tc.isDebugEnabled())
                    Tr.debug(tc, "Bypass locking row with id: " + ruId);
            }
        } finally {
            if (recoveryRS != null && !recoveryRS.isClosed())
                recoveryRS.close();
            if (recoveryStmt != null && !recoveryStmt.isClosed())
                recoveryStmt.close();
        }

        if (tc.isEntryEnabled())
            Tr.exit(tc, "recover");
    }
252418
private void processQuery() throws SQLException {
		if (preparedStatement != null) {
			return;
		}

		if (query == null) {
			throw new RuntimeException("Invalid NamedParameterStatement: no query!");
		}

		logger.debug("Orig Query: {}", query);
		StringBuffer builder = new StringBuffer();

		String queryByPlugin = query;
		for (INpsPlugin plugin : plugins) {
			queryByPlugin = plugin.process(queryByPlugin, params);
			if (logger.isDebugEnabled()) {
				logger.debug("Apply plugin: name=[{}] query=[{}] params={}",
					plugin.getClass().getSimpleName(), queryByPlugin, params);
			}
		}

		Matcher matcher = PARAM_PATTERN.matcher(queryByPlugin);
		int noOfParams = 0;
		while (matcher.find()) {
			if (matcher.group(4) != null) {
				noOfParams++;
				String param = matcher.group("PARAM");
				logger.debug("Param: {}", param);
				if (!paramsPlacement.containsKey(param)) {
					paramsPlacement.put(param, new ArrayList<>());
				}
				paramsPlacement.get(param).add(noOfParams);

				Object paramValue = params.get(param);
				StringBuilder paramReplacementBuilder = new StringBuilder("?");
				if (paramValue != null && (paramValue instanceof Collection || paramValue.getClass().isArray())) {
					int size = paramValue instanceof Collection ?
						((Collection) paramValue).size() :
						((Object[]) paramValue).length;
					for (int i = 1; i < size; i++) {
						paramReplacementBuilder.append(",?");
					}
				}
				matcher.appendReplacement(builder, paramReplacementBuilder.toString());
			}
		}
		matcher.appendTail(builder);
		finalQuery = builder.toString();

		logger.debug("Final SQL: {}", finalQuery);
		logger.debug("Number of params: {}", noOfParams);

		if (!ignoreExtraPassedParam) {
			for (String param : params.keySet()) {
				if (!paramsPlacement.containsKey(param)) {
					throw new SQLException("Passed parameter not found: " + param);
				}
			}
		}

		preparedStatement = connection.prepareStatement(finalQuery);
		if (fetchSize != null) {
			preparedStatement.setFetchSize(fetchSize);
		}

		if (queryTimeout != null) {
			preparedStatement.setQueryTimeout(queryTimeout);
		}

		if (maxRows != null) {
			preparedStatement.setMaxRows(maxRows);
		}

		builder = new StringBuffer();
		matcher = PARAM_Q_MARK_PATTERN.matcher(finalQuery);
		int idx = 1;
		while (matcher.find()) {
			if (matcher.group("QMARK") != null) {
				String replacement = String.format(" ?%s ", idx++);
				matcher.appendReplacement(builder, replacement);
			}
		}
		matcher.appendTail(builder);

		finalIndexedQuery = builder.toString();
	}
470611
@Override
    public void closeLog() throws InternalLogException
    {
        if (tc.isEntryEnabled())
            Tr.entry(tc, "closeLog", this);

        _recoveryLog.closeLog();

        if (tc.isEntryEnabled())
            Tr.exit(tc, "closeLog");
    }
580157
public static void handleException(final Exception exception) throws SQLException {
        if (isExceptionThrown()) {
            if (exception instanceof SQLException) {
                throw (SQLException) exception;
            }
            throw new ShardingException(exception);
        }
        log.error("exception occur: ", exception);
    }
134682
void force() throws InternalLogException
    {
        if (tc.isEntryEnabled())
            Tr.entry(tc, "force", this);

        // Check that the file is actually open
        if (_activeFile == null)
        {
            if (tc.isEntryEnabled())
                Tr.exit(tc, "force", "InternalLogException");
            throw new InternalLogException(null);
        }

        // Attempt to get exclusive lock on the lock object provided by RecoveryLogService
        // to protect access to the isSuspended flag, which is toggled during calls
        // to RecoveryLogService suspend/resume
        synchronized (RLSControllerImpl.SUSPEND_LOCK)
        {
            while (RLSControllerImpl.isSuspended())
            {
                try
                {
                    if (tc.isDebugEnabled())
                        Tr.debug(tc, "Waiting for RecoveryLogService to resume");
                    RLSControllerImpl.SUSPEND_LOCK.wait();
                } catch (InterruptedException exc)
                {
                    // This exception is received if another thread interrupts this thread by calling this threads 
                    // Thread.interrupt method. The RecoveryLogService class does not use this mechanism for 
                    // breaking out of the wait call - it uses notifyAll to wake up all waiting threads. This
                    // exception should never be generated. If for some reason it is called then ignore it and 
                    // start to wait again.
                    FFDCFilter.processException(exc, "com.ibm.ws.recoverylog.spi.LogHandle.force", "834", this);
                }
            }

            // Check if we've been configured to run in snapshot safe mode
            // With this in place we synchronize all force method calls and
            // take the synchronization performance hit.  The benefit is that we
            // can be sure that the log files will be in a consistent state
            // for snapshotting
            if (Configuration._isSnapshotSafe)
            {
                _activeFile.force();
            }
        }

        //  If we're not configured to be snapshot safe then we
        //  run a slight risk here that a force operation could be called
        //  just after the RecoveryLogService has been called to suspend,
        //  as we're outwith the sync block.
        //  The benefit is that we don't have the synchronization performance 
        //  hit when calling the force method 
        if (!Configuration._isSnapshotSafe)
        {
            _activeFile.force();
        }

        if (tc.isEntryEnabled())
            Tr.exit(tc, "force");
    }
1313913
@Override
	public synchronized List<RecordContext> getDataForContext (int contextId) throws DatabaseException {
    	try {
			List<RecordContext> result = new ArrayList<>();
			psGetAllDataForContext.setInt(1, contextId);
			try (ResultSet rs = psGetAllDataForContext.executeQuery()) {
				while (rs.next()) {
					result.add(new RecordContext(rs.getLong(DATAID), rs.getInt(CONTEXTID), rs.getInt(TYPE), rs.getString(DATA)));
				}
			}
			
			return result;
		} catch (SQLException e) {
			throw new DatabaseException(e);
		}
    }
230698
public SQLException exceptionWithQuery(String sql, SQLException sqlException,
      boolean explicitClosed) {
    if (explicitClosed) {
      return new SQLException(
          "Connection has explicitly been closed/aborted.\nQuery is: " + subQuery(sql),
          sqlException.getSQLState(),
          sqlException.getErrorCode(), sqlException.getCause());
    }

    if (options.dumpQueriesOnException || sqlException.getErrorCode() == 1064) {
      return new SQLException(sqlException.getMessage()
          + "\nQuery is: " + subQuery(sql)
          + "\njava thread: " + Thread.currentThread().getName(),
          sqlException.getSQLState(),
          sqlException.getErrorCode(), sqlException.getCause());
    }
    return sqlException;
  }
646695
public V get ()
        throws SQLException
    {
        V result = next();
        if (result != null) {
            int spurious = 0;
            while (next() != null) {
                spurious++;
            }
            if (spurious > 0) {
                log.warning("Cursor.get() quietly tossed " + spurious + " spurious additional " +
                            "records.", "query", _query);
            }
        }
        return result;
    }
808669
@Override
    public boolean isClosed() throws SQLException {
        if (!isClosed) {
            try {
                if (internalConn.isClosed()) {
                    destroy();
                }
            } catch (SQLException e) {
                // ignore
                destroy();

                if (logger.isWarnEnabled()) {
                    logger.warn(AbacusException.getErrorMsg(e));
                }
            }
        }

        return isClosed;
    }
982072
public void reset() throws SQLException {

        try {
            this.sessionProxy.resetSession();
        } catch (HsqlException e) {
            throw Util.sqlException(ErrorCode.X_08006, e.getMessage(), e);
        }
    }
581607
public void open() throws DBException
    {
        if (this.isOpen())
            return;         // Ignore if already open
        this.getRecord().handleInitialKey();        // Set up the smaller key
        this.getRecord().handleEndKey();            // Set up the larger key
                // You can't process a QueryRecord using tables, use special table logic.
        ((QueryRecord)m_record).openTableQuery();
    }
953497
protected void internalClose() throws SQLException {
		try {
			clearStatementCaches(true);
			if (this.connection != null){ // safety!
				this.connection.close();

				if (!this.connectionTrackingDisabled && this.finalizableRefs != null){
					this.finalizableRefs.remove(this.connection);
				}
			}
			this.logicallyClosed.set(true);
		} catch (SQLException e) {
			throw markPossiblyBroken(e);
		}
	}
894764
@Override
	protected void handleException(final SqlContext sqlContext, final SQLException ex) throws SQLException {
		SQLException cause = ex;

		while (cause.getNextException() != null) {
			cause = cause.getNextException();
		}

		if (LOG.isErrorEnabled() && isOutputExceptionLog()) {
			StringBuilder builder = new StringBuilder();
			builder.append(System.lineSeparator()).append("Exception occurred in SQL execution.")
					.append(System.lineSeparator());
			builder.append("Executed SQL[").append(sqlContext.getExecutableSql()).append("]")
					.append(System.lineSeparator());
			if (sqlContext instanceof SqlContextImpl) {
				Parameter[] bindParameters = ((SqlContextImpl) sqlContext).getBindParameters();
				for (int i = 0; i < bindParameters.length; i++) {
					Parameter parameter = getSqlFilterManager().doParameter(bindParameters[i]);
					builder.append("Bind Parameter.[INDEX[").append(i + 1).append("], ").append(parameter.toString())
							.append("]").append(System.lineSeparator());
				}
			}
			LOG.error(builder.toString(), cause);
		}

		throw cause;
	}
862611
public void processEvent(LogRecord record) {
		byte[] bytes;

		SerializationObject serializationObject = pool.getSerializationObject();
		try {
			bytes = serializationObject.serialize(record);
		} finally {
			pool.returnSerializationObject(serializationObject);
		}

		synchronized(this) {
			if (traceWriter != null && record.getLevel().intValue() < traceThreshold) {
				traceWriter.logRecord(record.getMillis(), bytes);
			} else if (logWriter != null) {
				logWriter.logRecord(record.getMillis(), bytes);
			}
		}
	}
644645
public static Connection getConnection() throws SQLException {
        if (LOGGER.isTraceEnabled()) {
            Callstacks.printCallstack(Level.TRACE, new String[]{"org.b3log"}, null);
        }

        if (Latkes.RuntimeDatabase.NONE == Latkes.getRuntimeDatabase()) {
            return null;
        }

        final Connection ret = hikari.getConnection();
        ret.setTransactionIsolation(transactionIsolationInt);
        ret.setAutoCommit(false);

        return ret;
    }
1336561
public static void logSQLConsumerException(final Logger logger,
			final SQLConsumerException e)
	{

		logger.logException(Level.ERROR, "SQLConsumerException", e);
	}
1357597
public void loadDatabase() throws Exception {
        // create/start the requested number of threads
        int thread_count = 10;
        Thread[] loginThreads = new Thread[thread_count];
        for (int i = 0; i < thread_count; ++i) {
            loginThreads[i] = new Thread(new LoginThread());
            loginThreads[i].start();
        }

        // Initialize the statistics
        fullStatsContext.fetchAndResetBaseline();

        // Run the data loading for 10 seconds
        System.out.println("\nLoading database...");
        Thread.sleep(1000l * 10);

        // stop the threads
        loadComplete.set(true);

        // block until all outstanding txns return
        client.drain();

        // join on the threads
        for (Thread t : loginThreads) {
            t.join();
        }

        // print the summary statistics of the data load
        printResults();

        // Create entries that we can query on.
        createUniqueData();
    }