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(); } } }
/** * 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); } }
/** * 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); } }
/** * 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); } }
/** 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; } }
/** * 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); } }
// ## 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); } }
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(); } }
/** * 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); } }
private void rollbackInternal() { rollback = prepareCommand("ROLLBACK", rollback); rollback.executeUpdate(); }
private void switchOffCluster() { CommandInterface ci = prepareCommand("SET CLUSTER ''", Integer.MAX_VALUE); ci.executeUpdate(); }