Search in sources :

Example 1 with ServerPreparedQuery

use of com.mysql.cj.ServerPreparedQuery in project ABC by RuiPinto96274.

the class ServerPreparedStatement method realClose.

@Override
public void realClose(boolean calledExplicitly, boolean closeOpenResults) throws SQLException {
    JdbcConnection locallyScopedConn = this.connection;
    if (locallyScopedConn == null) {
        // already closed
        return;
    }
    synchronized (locallyScopedConn.getConnectionMutex()) {
        if (this.connection != null) {
            // 
            // Don't communicate with the server if we're being called from the finalizer...
            // 
            // This will leak server resources, but if we don't do this, we'll deadlock (potentially, because there's no guarantee when, what order, and
            // what concurrency finalizers will be called with). Well-behaved programs won't rely on finalizers to clean up their statements.
            // 
            CJException exceptionDuringClose = null;
            if (this.isCached) {
                locallyScopedConn.decachePreparedStatement(this);
                this.isCached = false;
            }
            super.realClose(calledExplicitly, closeOpenResults);
            ((ServerPreparedQuery) this.query).clearParameters(false);
            // Finally deallocate the prepared statement.
            if (calledExplicitly && !locallyScopedConn.isClosed()) {
                synchronized (locallyScopedConn.getConnectionMutex()) {
                    try {
                        ((NativeSession) locallyScopedConn.getSession()).sendCommand(this.commandBuilder.buildComStmtClose(null, ((ServerPreparedQuery) this.query).getServerStatementId()), true, 0);
                    } catch (CJException sqlEx) {
                        exceptionDuringClose = sqlEx;
                    }
                }
            }
            if (exceptionDuringClose != null) {
                throw exceptionDuringClose;
            }
        }
    }
}
Also used : NativeSession(com.mysql.cj.NativeSession) ServerPreparedQuery(com.mysql.cj.ServerPreparedQuery) CJException(com.mysql.cj.exceptions.CJException)

Example 2 with ServerPreparedQuery

use of com.mysql.cj.ServerPreparedQuery in project ABC by RuiPinto96274.

the class ServerPreparedStatement method serverPrepare.

protected void serverPrepare(String sql) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        SQLException t = null;
        try {
            ServerPreparedQuery q = (ServerPreparedQuery) this.query;
            q.serverPrepare(sql);
        } catch (IOException ioEx) {
            t = SQLError.createCommunicationsException(this.connection, this.session.getProtocol().getPacketSentTimeHolder(), this.session.getProtocol().getPacketReceivedTimeHolder(), ioEx, this.exceptionInterceptor);
        } catch (CJException sqlEx) {
            SQLException ex = SQLExceptionsMapping.translateException(sqlEx);
            if (this.dumpQueriesOnException.getValue()) {
                StringBuilder messageBuf = new StringBuilder(((PreparedQuery<?>) this.query).getOriginalSql().length() + 32);
                messageBuf.append("\n\nQuery being prepared when exception was thrown:\n\n");
                messageBuf.append(((PreparedQuery<?>) this.query).getOriginalSql());
                ex = appendMessageToException(ex, messageBuf.toString(), this.exceptionInterceptor);
            }
            t = ex;
        } finally {
            // Leave the I/O channel in a known state...there might be packets out there that we're not interested in
            try {
                this.session.clearInputStream();
            } catch (Exception e) {
                if (t == null) {
                    t = SQLError.createCommunicationsException(this.connection, this.session.getProtocol().getPacketSentTimeHolder(), this.session.getProtocol().getPacketReceivedTimeHolder(), e, this.exceptionInterceptor);
                }
            }
            if (t != null) {
                throw t;
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) ServerPreparedQuery(com.mysql.cj.ServerPreparedQuery) PreparedQuery(com.mysql.cj.PreparedQuery) IOException(java.io.IOException) ServerPreparedQuery(com.mysql.cj.ServerPreparedQuery) CJException(com.mysql.cj.exceptions.CJException) SQLException(java.sql.SQLException) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) MySQLStatementCancelledException(com.mysql.cj.jdbc.exceptions.MySQLStatementCancelledException) IOException(java.io.IOException) MySQLTimeoutException(com.mysql.cj.jdbc.exceptions.MySQLTimeoutException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CJException(com.mysql.cj.exceptions.CJException)

Example 3 with ServerPreparedQuery

use of com.mysql.cj.ServerPreparedQuery in project JavaSegundasQuintas by ecteruel.

the class ServerPreparedStatement method executeBatchSerially.

@Override
protected long[] executeBatchSerially(int batchTimeout) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        JdbcConnection locallyScopedConn = this.connection;
        if (locallyScopedConn.isReadOnly()) {
            throw SQLError.createSQLException(Messages.getString("ServerPreparedStatement.2") + Messages.getString("ServerPreparedStatement.3"), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
        }
        clearWarnings();
        // Store this for later, we're going to 'swap' them out
        // as we execute each batched statement...
        ServerPreparedQueryBindValue[] oldBindValues = ((ServerPreparedQuery) this.query).getQueryBindings().getBindValues();
        try {
            long[] updateCounts = null;
            if (this.query.getBatchedArgs() != null) {
                int nbrCommands = this.query.getBatchedArgs().size();
                updateCounts = new long[nbrCommands];
                if (this.retrieveGeneratedKeys) {
                    this.batchedGeneratedKeys = new ArrayList<>(nbrCommands);
                }
                for (int i = 0; i < nbrCommands; i++) {
                    updateCounts[i] = -3;
                }
                SQLException sqlEx = null;
                int commandIndex = 0;
                ServerPreparedQueryBindValue[] previousBindValuesForBatch = null;
                CancelQueryTask timeoutTask = null;
                try {
                    timeoutTask = startQueryTimer(this, batchTimeout);
                    for (commandIndex = 0; commandIndex < nbrCommands; commandIndex++) {
                        Object arg = this.query.getBatchedArgs().get(commandIndex);
                        try {
                            if (arg instanceof String) {
                                updateCounts[commandIndex] = executeUpdateInternal((String) arg, true, this.retrieveGeneratedKeys);
                                // limit one generated key per OnDuplicateKey statement
                                getBatchedGeneratedKeys(this.results.getFirstCharOfQuery() == 'I' && containsOnDuplicateKeyInString((String) arg) ? 1 : 0);
                            } else {
                                ((ServerPreparedQuery) this.query).setQueryBindings((ServerPreparedQueryBindings) arg);
                                ServerPreparedQueryBindValue[] parameterBindings = ((ServerPreparedQuery) this.query).getQueryBindings().getBindValues();
                                if (previousBindValuesForBatch != null) {
                                    for (int j = 0; j < parameterBindings.length; j++) {
                                        if (parameterBindings[j].bufferType != previousBindValuesForBatch[j].bufferType) {
                                            ((ServerPreparedQuery) this.query).getQueryBindings().getSendTypesToServer().set(true);
                                            break;
                                        }
                                    }
                                }
                                try {
                                    updateCounts[commandIndex] = executeUpdateInternal(false, true);
                                } finally {
                                    previousBindValuesForBatch = parameterBindings;
                                }
                                // limit one generated key per OnDuplicateKey statement
                                getBatchedGeneratedKeys(containsOnDuplicateKeyUpdateInSQL() ? 1 : 0);
                            }
                        } catch (SQLException ex) {
                            updateCounts[commandIndex] = EXECUTE_FAILED;
                            if (this.continueBatchOnError && !(ex instanceof MySQLTimeoutException) && !(ex instanceof MySQLStatementCancelledException) && !hasDeadlockOrTimeoutRolledBackTx(ex)) {
                                sqlEx = ex;
                            } else {
                                long[] newUpdateCounts = new long[commandIndex];
                                System.arraycopy(updateCounts, 0, newUpdateCounts, 0, commandIndex);
                                throw SQLError.createBatchUpdateException(ex, newUpdateCounts, this.exceptionInterceptor);
                            }
                        }
                    }
                } finally {
                    stopQueryTimer(timeoutTask, false, false);
                    resetCancelledState();
                }
                if (sqlEx != null) {
                    throw SQLError.createBatchUpdateException(sqlEx, updateCounts, this.exceptionInterceptor);
                }
            }
            return (updateCounts != null) ? updateCounts : new long[0];
        } finally {
            ((ServerPreparedQuery) this.query).getQueryBindings().setBindValues(oldBindValues);
            ((ServerPreparedQuery) this.query).getQueryBindings().getSendTypesToServer().set(true);
            clearBatch();
        }
    }
}
Also used : SQLException(java.sql.SQLException) ServerPreparedQueryBindValue(com.mysql.cj.ServerPreparedQueryBindValue) ServerPreparedQuery(com.mysql.cj.ServerPreparedQuery) MySQLTimeoutException(com.mysql.cj.jdbc.exceptions.MySQLTimeoutException) CancelQueryTask(com.mysql.cj.CancelQueryTask) MySQLStatementCancelledException(com.mysql.cj.jdbc.exceptions.MySQLStatementCancelledException)

Example 4 with ServerPreparedQuery

use of com.mysql.cj.ServerPreparedQuery in project JavaSegundasQuintas by ecteruel.

the class ServerPreparedStatement method serverPrepare.

protected void serverPrepare(String sql) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        SQLException t = null;
        try {
            ServerPreparedQuery q = (ServerPreparedQuery) this.query;
            q.serverPrepare(sql);
        } catch (IOException ioEx) {
            t = SQLError.createCommunicationsException(this.connection, this.session.getProtocol().getPacketSentTimeHolder(), this.session.getProtocol().getPacketReceivedTimeHolder(), ioEx, this.exceptionInterceptor);
        } catch (CJException sqlEx) {
            SQLException ex = SQLExceptionsMapping.translateException(sqlEx);
            if (this.dumpQueriesOnException.getValue()) {
                StringBuilder messageBuf = new StringBuilder(((PreparedQuery<?>) this.query).getOriginalSql().length() + 32);
                messageBuf.append("\n\nQuery being prepared when exception was thrown:\n\n");
                messageBuf.append(((PreparedQuery<?>) this.query).getOriginalSql());
                ex = appendMessageToException(ex, messageBuf.toString(), this.exceptionInterceptor);
            }
            t = ex;
        } finally {
            // Leave the I/O channel in a known state...there might be packets out there that we're not interested in
            try {
                this.session.clearInputStream();
            } catch (Exception e) {
                if (t == null) {
                    t = SQLError.createCommunicationsException(this.connection, this.session.getProtocol().getPacketSentTimeHolder(), this.session.getProtocol().getPacketReceivedTimeHolder(), e, this.exceptionInterceptor);
                }
            }
            if (t != null) {
                throw t;
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) ServerPreparedQuery(com.mysql.cj.ServerPreparedQuery) PreparedQuery(com.mysql.cj.PreparedQuery) IOException(java.io.IOException) ServerPreparedQuery(com.mysql.cj.ServerPreparedQuery) CJException(com.mysql.cj.exceptions.CJException) SQLException(java.sql.SQLException) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) MySQLStatementCancelledException(com.mysql.cj.jdbc.exceptions.MySQLStatementCancelledException) IOException(java.io.IOException) MySQLTimeoutException(com.mysql.cj.jdbc.exceptions.MySQLTimeoutException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CJException(com.mysql.cj.exceptions.CJException)

Example 5 with ServerPreparedQuery

use of com.mysql.cj.ServerPreparedQuery in project JavaSegundasQuintas by ecteruel.

the class ServerPreparedStatement method asSql.

@Override
public String asSql(boolean quoteStreamsAndUnknowns) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        ClientPreparedStatement pStmtForSub = null;
        try {
            pStmtForSub = ClientPreparedStatement.getInstance(this.connection, ((PreparedQuery<?>) this.query).getOriginalSql(), this.getCurrentDatabase());
            int numParameters = ((PreparedQuery<?>) pStmtForSub.query).getParameterCount();
            int ourNumParameters = ((PreparedQuery<?>) this.query).getParameterCount();
            ServerPreparedQueryBindValue[] parameterBindings = ((ServerPreparedQuery) this.query).getQueryBindings().getBindValues();
            for (int i = 0; (i < numParameters) && (i < ourNumParameters); i++) {
                if (parameterBindings[i] != null) {
                    if (parameterBindings[i].isNull()) {
                        pStmtForSub.setNull(i + 1, MysqlType.NULL);
                    } else {
                        ServerPreparedQueryBindValue bindValue = parameterBindings[i];
                        // 
                        switch(bindValue.bufferType) {
                            case MysqlType.FIELD_TYPE_TINY:
                                pStmtForSub.setByte(i + 1, ((Long) bindValue.value).byteValue());
                                break;
                            case MysqlType.FIELD_TYPE_SHORT:
                                pStmtForSub.setShort(i + 1, ((Long) bindValue.value).shortValue());
                                break;
                            case MysqlType.FIELD_TYPE_LONG:
                                pStmtForSub.setInt(i + 1, ((Long) bindValue.value).intValue());
                                break;
                            case MysqlType.FIELD_TYPE_LONGLONG:
                                pStmtForSub.setLong(i + 1, ((Long) bindValue.value).longValue());
                                break;
                            case MysqlType.FIELD_TYPE_FLOAT:
                                pStmtForSub.setFloat(i + 1, ((Float) bindValue.value).floatValue());
                                break;
                            case MysqlType.FIELD_TYPE_DOUBLE:
                                pStmtForSub.setDouble(i + 1, ((Double) bindValue.value).doubleValue());
                                break;
                            default:
                                pStmtForSub.setObject(i + 1, parameterBindings[i].value);
                                break;
                        }
                    }
                }
            }
            return pStmtForSub.asSql(quoteStreamsAndUnknowns);
        } finally {
            if (pStmtForSub != null) {
                try {
                    pStmtForSub.close();
                } catch (SQLException sqlEx) {
                // ignore
                }
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) ServerPreparedQuery(com.mysql.cj.ServerPreparedQuery) PreparedQuery(com.mysql.cj.PreparedQuery) ServerPreparedQueryBindValue(com.mysql.cj.ServerPreparedQueryBindValue)

Aggregations

ServerPreparedQuery (com.mysql.cj.ServerPreparedQuery)12 SQLException (java.sql.SQLException)9 PreparedQuery (com.mysql.cj.PreparedQuery)6 ServerPreparedQueryBindValue (com.mysql.cj.ServerPreparedQueryBindValue)6 CJException (com.mysql.cj.exceptions.CJException)6 MySQLStatementCancelledException (com.mysql.cj.jdbc.exceptions.MySQLStatementCancelledException)6 MySQLTimeoutException (com.mysql.cj.jdbc.exceptions.MySQLTimeoutException)6 CancelQueryTask (com.mysql.cj.CancelQueryTask)3 NativeSession (com.mysql.cj.NativeSession)3 WrongArgumentException (com.mysql.cj.exceptions.WrongArgumentException)3 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3