Search in sources :

Example 16 with MySQLTimeoutException

use of com.mysql.cj.jdbc.exceptions.MySQLTimeoutException in project ABC by RuiPinto96274.

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 17 with MySQLTimeoutException

use of com.mysql.cj.jdbc.exceptions.MySQLTimeoutException in project ABC by RuiPinto96274.

the class StatementRegressionTest method testBug103796.

/**
 * Tests fix for Bug#103796 (32922715), CONNECTOR/J 8 STMT SETQUERYTIMEOUT CAN NOT WORK.
 *
 * @throws Exception
 */
@Test
public void testBug103796() throws Exception {
    Connection con = null;
    Properties props = new Properties();
    props.setProperty(PropertyKey.useCursorFetch.getKeyName(), "true");
    try {
        con = getConnectionWithProps(props);
        Statement timeoutStmt = con.createStatement();
        timeoutStmt.setFetchSize(10);
        timeoutStmt.setQueryTimeout(2);
        long begin = System.currentTimeMillis();
        try {
            timeoutStmt.execute("SELECT SLEEP(5)");
            fail("Query didn't time out");
        } catch (MySQLTimeoutException timeoutEx) {
            long duration = System.currentTimeMillis() - begin;
            assertTrue(duration > 1000 && duration < 3000);
        }
    } finally {
        if (con != null) {
            con.close();
        }
    }
}
Also used : MySQLTimeoutException(com.mysql.cj.jdbc.exceptions.MySQLTimeoutException) JdbcStatement(com.mysql.cj.jdbc.JdbcStatement) JdbcPreparedStatement(com.mysql.cj.jdbc.JdbcPreparedStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) ServerPreparedStatement(com.mysql.cj.jdbc.ServerPreparedStatement) ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) Properties(java.util.Properties) StatementsTest(testsuite.simple.StatementsTest) Test(org.junit.jupiter.api.Test)

Example 18 with MySQLTimeoutException

use of com.mysql.cj.jdbc.exceptions.MySQLTimeoutException in project ABC by RuiPinto96274.

the class StatementRegressionTest method testBug22359.

/**
 * Tests fix for BUG#22359 - Driver was using millis for Statement.setQueryTimeout() when spec says argument is seconds.
 *
 * @throws Exception
 */
@Test
public void testBug22359() throws Exception {
    Statement timeoutStmt = null;
    try {
        timeoutStmt = this.conn.createStatement();
        timeoutStmt.setQueryTimeout(2);
        long begin = System.currentTimeMillis();
        try {
            timeoutStmt.execute("SELECT SLEEP(30)");
            fail("Query didn't time out");
        } catch (MySQLTimeoutException timeoutEx) {
            long end = System.currentTimeMillis();
            assertTrue((end - begin) > 1000);
        }
    } finally {
        if (timeoutStmt != null) {
            timeoutStmt.close();
        }
    }
}
Also used : MySQLTimeoutException(com.mysql.cj.jdbc.exceptions.MySQLTimeoutException) JdbcStatement(com.mysql.cj.jdbc.JdbcStatement) JdbcPreparedStatement(com.mysql.cj.jdbc.JdbcPreparedStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) ServerPreparedStatement(com.mysql.cj.jdbc.ServerPreparedStatement) ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement) StatementsTest(testsuite.simple.StatementsTest) Test(org.junit.jupiter.api.Test)

Aggregations

MySQLTimeoutException (com.mysql.cj.jdbc.exceptions.MySQLTimeoutException)18 MySQLStatementCancelledException (com.mysql.cj.jdbc.exceptions.MySQLStatementCancelledException)12 SQLException (java.sql.SQLException)12 CancelQueryTask (com.mysql.cj.CancelQueryTask)9 Test (org.junit.jupiter.api.Test)9 ClientPreparedStatement (com.mysql.cj.jdbc.ClientPreparedStatement)6 JdbcPreparedStatement (com.mysql.cj.jdbc.JdbcPreparedStatement)6 JdbcStatement (com.mysql.cj.jdbc.JdbcStatement)6 ServerPreparedStatement (com.mysql.cj.jdbc.ServerPreparedStatement)6 CallableStatement (java.sql.CallableStatement)6 PreparedStatement (java.sql.PreparedStatement)6 Statement (java.sql.Statement)6 StatementsTest (testsuite.simple.StatementsTest)6 ClientPreparedQuery (com.mysql.cj.ClientPreparedQuery)3 ClientPreparedQueryBindings (com.mysql.cj.ClientPreparedQueryBindings)3 MysqlConnection (com.mysql.cj.MysqlConnection)3 PreparedQuery (com.mysql.cj.PreparedQuery)3 QueryBindings (com.mysql.cj.QueryBindings)3 ServerPreparedQuery (com.mysql.cj.ServerPreparedQuery)3 ServerPreparedQueryBindValue (com.mysql.cj.ServerPreparedQueryBindValue)3