mirror of
https://github.com/MariaDB/server.git
synced 2025-01-18 21:12:26 +01:00
dfdbc84585
"HANDLER statements within a transaction might lead to deadlocks". Introduce a notion of a sentinel to MDL_context. A sentinel is a ticket that separates all tickets in the context into two groups: before and after it. Currently we can have (and need) only one designated sentinel -- it separates all locks taken by LOCK TABLE or HANDLER statement, which must survive COMMIT and ROLLBACK and all other locks, which must be released at COMMIT or ROLLBACK. The tricky part is maintaining the sentinel up to date when someone release its corresponding ticket. This can happen, e.g. if someone issues DROP TABLE under LOCK TABLES (generally, see all calls to release_all_locks_for_name()). MDL_context::release_ticket() is modified to take care of it. ****** A fix and a test case for Bug#46224 "HANDLER statements within a transaction might lead to deadlocks". An attempt to mix HANDLER SQL statements, which are transaction- agnostic, an open multi-statement transaction, and DDL against the involved tables (in a concurrent connection) could lead to a deadlock. The deadlock would occur when HANDLER OPEN or HANDLER READ would have to wait on a conflicting metadata lock. If the connection that issued HANDLER statement also had other metadata locks (say, acquired in scope of a transaction), a classical deadlock situation of mutual wait could occur. Incompatible change: entering LOCK TABLES mode automatically closes all open HANDLERs in the current connection. Incompatible change: previously an attempt to wait on a lock in a connection that has an open HANDLER statement could wait indefinitely/deadlock. After this patch, an error ER_LOCK_DEADLOCK is produced. The idea of the fix is to merge thd->handler_mdl_context with the main mdl_context of the connection, used for transactional locks. This makes deadlock detection possible, since all waits with locks are "visible" and available to analysis in a single MDL context of the connection. Since HANDLER locks and transactional locks have a different life cycle -- HANDLERs are explicitly open and closed, and so are HANDLER locks, explicitly acquired and released, whereas transactional locks "accumulate" till the end of a transaction and are released only with COMMIT, ROLLBACK and ROLLBACK TO SAVEPOINT, a concept of "sentinel" was introduced to MDL_context. All locks, HANDLER and others, reside in the same linked list. However, a selected element of the list separates locks with different life cycle. HANDLER locks always reside at the end of the list, after the sentinel. Transactional locks are prepended to the beginning of the list, before the sentinel. Thus, ROLLBACK, COMMIT or ROLLBACK TO SAVEPOINT, only release those locks that reside before the sentinel. HANDLER locks must be released explicitly as part of HANDLER CLOSE statement, or an implicit close. The same approach with sentinel is also employed for LOCK TABLES locks. Since HANDLER and LOCK TABLES statement has never worked together, the implementation is made simple and only maintains one sentinel, which is used either for HANDLER locks, or for LOCK TABLES locks. mysql-test/include/handler.inc: Add test coverage for Bug#46224 "HANDLER statements within a transaction might lead to deadlocks". Extended HANDLER coverage to cover a mix of HANDLER, transactions and DDL statements. mysql-test/r/handler_innodb.result: Update results (Bug#46224). mysql-test/r/handler_myisam.result: Update results (Bug#46224). sql/lock.cc: Remove thd->some_tables_deleted, it's never used. sql/log_event.cc: No need to check for thd->locked_tables_mode, it's done inside release_transactional_locks(). sql/mdl.cc: Implement the concept of HANDLER and LOCK TABLES "sentinel". Implement a method to clone an acquired ticket. Do not return tickets beyond the sentinel when acquiring locks, create a copy. Remove methods to merge and backup MDL_context, they are now not used (Hurra!). This opens a path to a proper constructor and destructor of class MDL_context (to be done in a separate patch). Modify find_ticket() to provide information about where the ticket position is with regard to the sentinel. sql/mdl.h: Add declarations necessary for the implementation of the concept of "sentinel", a dedicated ticket separating transactional and non-transactional locks. sql/mysql_priv.h: Add mark_tmp_table_for_reuse() declaration, a function to "close" a single session (temporary) table. sql/sql_base.cc: Remove thd->some_tables_deleted. Modify deadlock-prevention asserts and deadlock detection heuristics to take into account that from now on HANDLER locks reside in the same locking context. Add broadcast_refresh() to mysql_notify_thread_having_shared_lock(): this is necessary for the case when a thread having a shared lock is asleep in tdc_wait_for_old_versions(). This situation is only possible with HANDLER t1 OPEN; FLUSH TABLE (since all over code paths that lead to tdc_wait_for_old_versions() always have an empty MDL_context). Previously the server would simply deadlock in this situation. sql/sql_class.cc: Remove now unused member "THD::some_tables_deleted". Move mysql_ha_cleanup() a few lines above in THD::cleanup() to make sure that all handlers are closed when it's time to destroy the MDL_context of this connection. Remove handler_mdl_context and handler_tables. sql/sql_class.h: Remove THD::handler_tables, THD::handler_mdl_context, THD::some_tables_deleted. sql/sql_handler.cc: Remove thd->handler_tables. Remove thd->handler_mdl_context. Rewrite mysql_ha_open() to have no special provision for MERGE tables, now that we don't have to manipulate with thd->handler_tables it's easy to do. Remove dead code. Fix a bug in mysql_ha_flush() when we would always flush a temporary HANDLER when mysql_ha_flush() is called (actually mysql_ha_flush() never needs to flush temporary tables). sql/sql_insert.cc: Update a comment, no more thd->some_tables_deleted. sql/sql_parse.cc: Implement an incompatible change: entering LOCK TABLES closes active HANDLERs, if any. Now that we have a sentinel, we don't need to check for thd->locked_tables_mode when releasing metadata locks in COMMIT/ROLLBACK. sql/sql_plist.h: Add new (now necessary) methods to the list class. sql/sql_prepare.cc: Make sure we don't release HANDLER locks when rollback to a savepoint, set to not keep locks taken at PREPARE. sql/sql_servers.cc: Update to a new signature of MDL_context::release_all_locks(). sql/sql_table.cc: Remove thd->some_tables_deleted. sql/transaction.cc: Add comments. Make sure rollback to (MDL) savepoint works under LOCK TABLES and with HANDLER tables.
670 lines
16 KiB
C++
670 lines
16 KiB
C++
/* Copyright (C) 2008 Sun/MySQL
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
#ifdef USE_PRAGMA_IMPLEMENTATION
|
|
#pragma implementation // gcc: Class implementation
|
|
#endif
|
|
|
|
#include "mysql_priv.h"
|
|
#include "transaction.h"
|
|
#include "rpl_handler.h"
|
|
|
|
/* Conditions under which the transaction state must not change. */
|
|
static bool trans_check(THD *thd)
|
|
{
|
|
enum xa_states xa_state= thd->transaction.xid_state.xa_state;
|
|
DBUG_ENTER("trans_check");
|
|
|
|
if (unlikely(thd->in_sub_stmt))
|
|
my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
|
|
if (xa_state != XA_NOTR)
|
|
my_error(ER_XAER_RMFAIL, MYF(0), xa_state_names[xa_state]);
|
|
else
|
|
DBUG_RETURN(FALSE);
|
|
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
|
|
|
|
/**
|
|
Mark a XA transaction as rollback-only if the RM unilaterally
|
|
rolled back the transaction branch.
|
|
|
|
@note If a rollback was requested by the RM, this function sets
|
|
the appropriate rollback error code and transits the state
|
|
to XA_ROLLBACK_ONLY.
|
|
|
|
@return TRUE if transaction was rolled back or if the transaction
|
|
state is XA_ROLLBACK_ONLY. FALSE otherwise.
|
|
*/
|
|
static bool xa_trans_rolled_back(XID_STATE *xid_state)
|
|
{
|
|
if (xid_state->rm_error)
|
|
{
|
|
switch (xid_state->rm_error) {
|
|
case ER_LOCK_WAIT_TIMEOUT:
|
|
my_error(ER_XA_RBTIMEOUT, MYF(0));
|
|
break;
|
|
case ER_LOCK_DEADLOCK:
|
|
my_error(ER_XA_RBDEADLOCK, MYF(0));
|
|
break;
|
|
default:
|
|
my_error(ER_XA_RBROLLBACK, MYF(0));
|
|
}
|
|
xid_state->xa_state= XA_ROLLBACK_ONLY;
|
|
}
|
|
|
|
return (xid_state->xa_state == XA_ROLLBACK_ONLY);
|
|
}
|
|
|
|
|
|
/**
|
|
Begin a new transaction.
|
|
|
|
@note Beginning a transaction implicitly commits any current
|
|
transaction and releases existing locks.
|
|
|
|
@param thd Current thread
|
|
@param flags Transaction flags
|
|
|
|
@retval FALSE Success
|
|
@retval TRUE Failure
|
|
*/
|
|
|
|
bool trans_begin(THD *thd, uint flags)
|
|
{
|
|
int res= FALSE;
|
|
DBUG_ENTER("trans_begin");
|
|
|
|
if (trans_check(thd))
|
|
DBUG_RETURN(TRUE);
|
|
|
|
thd->locked_tables_list.unlock_locked_tables(thd);
|
|
|
|
DBUG_ASSERT(!thd->locked_tables_mode);
|
|
|
|
if (trans_commit_implicit(thd))
|
|
DBUG_RETURN(TRUE);
|
|
|
|
/*
|
|
Release transactional metadata locks only after the
|
|
transaction has been committed.
|
|
*/
|
|
thd->mdl_context.release_transactional_locks();
|
|
|
|
thd->options|= OPTION_BEGIN;
|
|
thd->server_status|= SERVER_STATUS_IN_TRANS;
|
|
|
|
if (flags & MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT)
|
|
res= ha_start_consistent_snapshot(thd);
|
|
|
|
DBUG_RETURN(test(res));
|
|
}
|
|
|
|
|
|
/**
|
|
Commit the current transaction, making its changes permanent.
|
|
|
|
@param thd Current thread
|
|
|
|
@retval FALSE Success
|
|
@retval TRUE Failure
|
|
*/
|
|
|
|
bool trans_commit(THD *thd)
|
|
{
|
|
int res;
|
|
DBUG_ENTER("trans_commit");
|
|
|
|
if (trans_check(thd))
|
|
DBUG_RETURN(TRUE);
|
|
|
|
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
|
|
res= ha_commit_trans(thd, TRUE);
|
|
if (res)
|
|
/*
|
|
if res is non-zero, then ha_commit_trans has rolled back the
|
|
transaction, so the hooks for rollback will be called.
|
|
*/
|
|
RUN_HOOK(transaction, after_rollback, (thd, FALSE));
|
|
else
|
|
RUN_HOOK(transaction, after_commit, (thd, FALSE));
|
|
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
|
|
thd->transaction.all.modified_non_trans_table= FALSE;
|
|
thd->lex->start_transaction_opt= 0;
|
|
|
|
DBUG_RETURN(test(res));
|
|
}
|
|
|
|
|
|
/**
|
|
Implicitly commit the current transaction.
|
|
|
|
@note A implicit commit does not releases existing table locks.
|
|
|
|
@param thd Current thread
|
|
|
|
@retval FALSE Success
|
|
@retval TRUE Failure
|
|
*/
|
|
|
|
bool trans_commit_implicit(THD *thd)
|
|
{
|
|
bool res= FALSE;
|
|
DBUG_ENTER("trans_commit_implicit");
|
|
|
|
if (trans_check(thd))
|
|
DBUG_RETURN(TRUE);
|
|
|
|
if (thd->in_multi_stmt_transaction() || (thd->options & OPTION_TABLE_LOCK))
|
|
{
|
|
/* Safety if one did "drop table" on locked tables */
|
|
if (!thd->locked_tables_mode)
|
|
thd->options&= ~OPTION_TABLE_LOCK;
|
|
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
|
|
res= test(ha_commit_trans(thd, TRUE));
|
|
}
|
|
|
|
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
|
|
thd->transaction.all.modified_non_trans_table= FALSE;
|
|
|
|
DBUG_RETURN(res);
|
|
}
|
|
|
|
|
|
/**
|
|
Rollback the current transaction, canceling its changes.
|
|
|
|
@param thd Current thread
|
|
|
|
@retval FALSE Success
|
|
@retval TRUE Failure
|
|
*/
|
|
|
|
bool trans_rollback(THD *thd)
|
|
{
|
|
int res;
|
|
DBUG_ENTER("trans_rollback");
|
|
|
|
if (trans_check(thd))
|
|
DBUG_RETURN(TRUE);
|
|
|
|
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
|
|
res= ha_rollback_trans(thd, TRUE);
|
|
RUN_HOOK(transaction, after_rollback, (thd, FALSE));
|
|
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
|
|
thd->transaction.all.modified_non_trans_table= FALSE;
|
|
thd->lex->start_transaction_opt= 0;
|
|
|
|
DBUG_RETURN(test(res));
|
|
}
|
|
|
|
|
|
/**
|
|
Commit the single statement transaction.
|
|
|
|
@note Note that if the autocommit is on, then the following call
|
|
inside InnoDB will commit or rollback the whole transaction
|
|
(= the statement). The autocommit mechanism built into InnoDB
|
|
is based on counting locks, but if the user has used LOCK
|
|
TABLES then that mechanism does not know to do the commit.
|
|
|
|
@param thd Current thread
|
|
|
|
@retval FALSE Success
|
|
@retval TRUE Failure
|
|
*/
|
|
|
|
bool trans_commit_stmt(THD *thd)
|
|
{
|
|
DBUG_ENTER("trans_commit_stmt");
|
|
int res= FALSE;
|
|
if (thd->transaction.stmt.ha_list)
|
|
res= ha_commit_trans(thd, FALSE);
|
|
|
|
if (res)
|
|
/*
|
|
if res is non-zero, then ha_commit_trans has rolled back the
|
|
transaction, so the hooks for rollback will be called.
|
|
*/
|
|
RUN_HOOK(transaction, after_rollback, (thd, FALSE));
|
|
else
|
|
RUN_HOOK(transaction, after_commit, (thd, FALSE));
|
|
DBUG_RETURN(test(res));
|
|
}
|
|
|
|
|
|
/**
|
|
Rollback the single statement transaction.
|
|
|
|
@param thd Current thread
|
|
|
|
@retval FALSE Success
|
|
@retval TRUE Failure
|
|
*/
|
|
bool trans_rollback_stmt(THD *thd)
|
|
{
|
|
DBUG_ENTER("trans_rollback_stmt");
|
|
|
|
if (thd->transaction.stmt.ha_list)
|
|
{
|
|
ha_rollback_trans(thd, FALSE);
|
|
if (thd->transaction_rollback_request && !thd->in_sub_stmt)
|
|
ha_rollback_trans(thd, TRUE);
|
|
}
|
|
|
|
RUN_HOOK(transaction, after_rollback, (thd, FALSE));
|
|
|
|
DBUG_RETURN(FALSE);
|
|
}
|
|
|
|
/* Find a named savepoint in the current transaction. */
|
|
static SAVEPOINT **
|
|
find_savepoint(THD *thd, LEX_STRING name)
|
|
{
|
|
SAVEPOINT **sv= &thd->transaction.savepoints;
|
|
|
|
while (*sv)
|
|
{
|
|
if (my_strnncoll(system_charset_info, (uchar *) name.str, name.length,
|
|
(uchar *) (*sv)->name, (*sv)->length) == 0)
|
|
break;
|
|
sv= &(*sv)->prev;
|
|
}
|
|
|
|
return sv;
|
|
}
|
|
|
|
|
|
/**
|
|
Set a named transaction savepoint.
|
|
|
|
@param thd Current thread
|
|
@param name Savepoint name
|
|
|
|
@retval FALSE Success
|
|
@retval TRUE Failure
|
|
*/
|
|
|
|
bool trans_savepoint(THD *thd, LEX_STRING name)
|
|
{
|
|
SAVEPOINT **sv, *newsv;
|
|
DBUG_ENTER("trans_savepoint");
|
|
|
|
if (!(thd->in_multi_stmt_transaction() || thd->in_sub_stmt) ||
|
|
!opt_using_transactions)
|
|
DBUG_RETURN(FALSE);
|
|
|
|
sv= find_savepoint(thd, name);
|
|
|
|
if (*sv) /* old savepoint of the same name exists */
|
|
{
|
|
newsv= *sv;
|
|
ha_release_savepoint(thd, *sv);
|
|
*sv= (*sv)->prev;
|
|
}
|
|
else if ((newsv= (SAVEPOINT *) alloc_root(&thd->transaction.mem_root,
|
|
savepoint_alloc_size)) == NULL)
|
|
{
|
|
my_error(ER_OUT_OF_RESOURCES, MYF(0));
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
|
|
newsv->name= strmake_root(&thd->transaction.mem_root, name.str, name.length);
|
|
newsv->length= name.length;
|
|
|
|
/*
|
|
if we'll get an error here, don't add new savepoint to the list.
|
|
we'll lose a little bit of memory in transaction mem_root, but it'll
|
|
be free'd when transaction ends anyway
|
|
*/
|
|
if (ha_savepoint(thd, newsv))
|
|
DBUG_RETURN(TRUE);
|
|
|
|
newsv->prev= thd->transaction.savepoints;
|
|
thd->transaction.savepoints= newsv;
|
|
|
|
/*
|
|
Remember the last acquired lock before the savepoint was set.
|
|
This is used as a marker to only release locks acquired after
|
|
the setting of this savepoint.
|
|
Note: this works just fine if we're under LOCK TABLES,
|
|
since mdl_savepoint() is guaranteed to be beyond
|
|
the last locked table. This allows to release some
|
|
locks acquired during LOCK TABLES.
|
|
*/
|
|
newsv->mdl_savepoint = thd->mdl_context.mdl_savepoint();
|
|
|
|
DBUG_RETURN(FALSE);
|
|
}
|
|
|
|
|
|
/**
|
|
Rollback a transaction to the named savepoint.
|
|
|
|
@note Modifications that the current transaction made to
|
|
rows after the savepoint was set are undone in the
|
|
rollback.
|
|
|
|
@note Savepoints that were set at a later time than the
|
|
named savepoint are deleted.
|
|
|
|
@param thd Current thread
|
|
@param name Savepoint name
|
|
|
|
@retval FALSE Success
|
|
@retval TRUE Failure
|
|
*/
|
|
|
|
bool trans_rollback_to_savepoint(THD *thd, LEX_STRING name)
|
|
{
|
|
int res= FALSE;
|
|
SAVEPOINT *sv= *find_savepoint(thd, name);
|
|
DBUG_ENTER("trans_rollback_to_savepoint");
|
|
|
|
if (sv == NULL)
|
|
{
|
|
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", name.str);
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
|
|
if (ha_rollback_to_savepoint(thd, sv))
|
|
res= TRUE;
|
|
else if (((thd->options & OPTION_KEEP_LOG) ||
|
|
thd->transaction.all.modified_non_trans_table) &&
|
|
!thd->slave_thread)
|
|
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
|
|
ER_WARNING_NOT_COMPLETE_ROLLBACK,
|
|
ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
|
|
|
|
thd->transaction.savepoints= sv;
|
|
|
|
/*
|
|
Release metadata locks that were acquired during this savepoint unit.
|
|
*/
|
|
if (!res)
|
|
thd->mdl_context.rollback_to_savepoint(sv->mdl_savepoint);
|
|
|
|
DBUG_RETURN(test(res));
|
|
}
|
|
|
|
|
|
/**
|
|
Remove the named savepoint from the set of savepoints of
|
|
the current transaction.
|
|
|
|
@note No commit or rollback occurs. It is an error if the
|
|
savepoint does not exist.
|
|
|
|
@param thd Current thread
|
|
@param name Savepoint name
|
|
|
|
@retval FALSE Success
|
|
@retval TRUE Failure
|
|
*/
|
|
|
|
bool trans_release_savepoint(THD *thd, LEX_STRING name)
|
|
{
|
|
int res= FALSE;
|
|
SAVEPOINT *sv= *find_savepoint(thd, name);
|
|
DBUG_ENTER("trans_release_savepoint");
|
|
|
|
if (sv == NULL)
|
|
{
|
|
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", name.str);
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
|
|
if (ha_release_savepoint(thd, sv))
|
|
res= TRUE;
|
|
|
|
thd->transaction.savepoints= sv->prev;
|
|
|
|
DBUG_RETURN(test(res));
|
|
}
|
|
|
|
|
|
/**
|
|
Starts an XA transaction with the given xid value.
|
|
|
|
@param thd Current thread
|
|
|
|
@retval FALSE Success
|
|
@retval TRUE Failure
|
|
*/
|
|
|
|
bool trans_xa_start(THD *thd)
|
|
{
|
|
enum xa_states xa_state= thd->transaction.xid_state.xa_state;
|
|
DBUG_ENTER("trans_xa_start");
|
|
|
|
if (xa_state == XA_IDLE && thd->lex->xa_opt == XA_RESUME)
|
|
{
|
|
bool not_equal= !thd->transaction.xid_state.xid.eq(thd->lex->xid);
|
|
if (not_equal)
|
|
my_error(ER_XAER_NOTA, MYF(0));
|
|
else
|
|
thd->transaction.xid_state.xa_state= XA_ACTIVE;
|
|
DBUG_RETURN(not_equal);
|
|
}
|
|
|
|
/* TODO: JOIN is not supported yet. */
|
|
if (thd->lex->xa_opt != XA_NONE)
|
|
my_error(ER_XAER_INVAL, MYF(0));
|
|
else if (xa_state != XA_NOTR)
|
|
my_error(ER_XAER_RMFAIL, MYF(0), xa_state_names[xa_state]);
|
|
else if (thd->locked_tables_mode || thd->active_transaction())
|
|
my_error(ER_XAER_OUTSIDE, MYF(0));
|
|
else if (xid_cache_search(thd->lex->xid))
|
|
my_error(ER_XAER_DUPID, MYF(0));
|
|
else if (!trans_begin(thd))
|
|
{
|
|
DBUG_ASSERT(thd->transaction.xid_state.xid.is_null());
|
|
thd->transaction.xid_state.xa_state= XA_ACTIVE;
|
|
thd->transaction.xid_state.rm_error= 0;
|
|
thd->transaction.xid_state.xid.set(thd->lex->xid);
|
|
xid_cache_insert(&thd->transaction.xid_state);
|
|
DBUG_RETURN(FALSE);
|
|
}
|
|
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
|
|
|
|
/**
|
|
Put a XA transaction in the IDLE state.
|
|
|
|
@param thd Current thread
|
|
|
|
@retval FALSE Success
|
|
@retval TRUE Failure
|
|
*/
|
|
|
|
bool trans_xa_end(THD *thd)
|
|
{
|
|
DBUG_ENTER("trans_xa_end");
|
|
|
|
/* TODO: SUSPEND and FOR MIGRATE are not supported yet. */
|
|
if (thd->lex->xa_opt != XA_NONE)
|
|
my_error(ER_XAER_INVAL, MYF(0));
|
|
else if (thd->transaction.xid_state.xa_state != XA_ACTIVE)
|
|
my_error(ER_XAER_RMFAIL, MYF(0),
|
|
xa_state_names[thd->transaction.xid_state.xa_state]);
|
|
else if (!thd->transaction.xid_state.xid.eq(thd->lex->xid))
|
|
my_error(ER_XAER_NOTA, MYF(0));
|
|
else if (!xa_trans_rolled_back(&thd->transaction.xid_state))
|
|
thd->transaction.xid_state.xa_state= XA_IDLE;
|
|
|
|
DBUG_RETURN(thd->transaction.xid_state.xa_state != XA_IDLE);
|
|
}
|
|
|
|
|
|
/**
|
|
Put a XA transaction in the PREPARED state.
|
|
|
|
@param thd Current thread
|
|
|
|
@retval FALSE Success
|
|
@retval TRUE Failure
|
|
*/
|
|
|
|
bool trans_xa_prepare(THD *thd)
|
|
{
|
|
DBUG_ENTER("trans_xa_prepare");
|
|
|
|
if (thd->transaction.xid_state.xa_state != XA_IDLE)
|
|
my_error(ER_XAER_RMFAIL, MYF(0),
|
|
xa_state_names[thd->transaction.xid_state.xa_state]);
|
|
else if (!thd->transaction.xid_state.xid.eq(thd->lex->xid))
|
|
my_error(ER_XAER_NOTA, MYF(0));
|
|
else if (ha_prepare(thd))
|
|
{
|
|
xid_cache_delete(&thd->transaction.xid_state);
|
|
thd->transaction.xid_state.xa_state= XA_NOTR;
|
|
my_error(ER_XA_RBROLLBACK, MYF(0));
|
|
}
|
|
else
|
|
thd->transaction.xid_state.xa_state= XA_PREPARED;
|
|
|
|
DBUG_RETURN(thd->transaction.xid_state.xa_state != XA_PREPARED);
|
|
}
|
|
|
|
|
|
/**
|
|
Commit and terminate the a XA transaction.
|
|
|
|
@param thd Current thread
|
|
|
|
@retval FALSE Success
|
|
@retval TRUE Failure
|
|
*/
|
|
|
|
bool trans_xa_commit(THD *thd)
|
|
{
|
|
bool res= TRUE;
|
|
enum xa_states xa_state= thd->transaction.xid_state.xa_state;
|
|
DBUG_ENTER("trans_xa_commit");
|
|
|
|
if (!thd->transaction.xid_state.xid.eq(thd->lex->xid))
|
|
{
|
|
XID_STATE *xs= xid_cache_search(thd->lex->xid);
|
|
res= !xs || xs->in_thd;
|
|
if (res)
|
|
my_error(ER_XAER_NOTA, MYF(0));
|
|
else
|
|
{
|
|
res= xa_trans_rolled_back(xs);
|
|
ha_commit_or_rollback_by_xid(thd->lex->xid, !res);
|
|
xid_cache_delete(xs);
|
|
}
|
|
DBUG_RETURN(res);
|
|
}
|
|
|
|
if (xa_trans_rolled_back(&thd->transaction.xid_state))
|
|
{
|
|
if ((res= test(ha_rollback_trans(thd, TRUE))))
|
|
my_error(ER_XAER_RMERR, MYF(0));
|
|
}
|
|
else if (xa_state == XA_IDLE && thd->lex->xa_opt == XA_ONE_PHASE)
|
|
{
|
|
int r= ha_commit_trans(thd, TRUE);
|
|
if ((res= test(r)))
|
|
my_error(r == 1 ? ER_XA_RBROLLBACK : ER_XAER_RMERR, MYF(0));
|
|
}
|
|
else if (xa_state == XA_PREPARED && thd->lex->xa_opt == XA_NONE)
|
|
{
|
|
if (wait_if_global_read_lock(thd, 0, 0))
|
|
{
|
|
ha_rollback_trans(thd, TRUE);
|
|
my_error(ER_XAER_RMERR, MYF(0));
|
|
}
|
|
else
|
|
{
|
|
res= test(ha_commit_one_phase(thd, 1));
|
|
if (res)
|
|
my_error(ER_XAER_RMERR, MYF(0));
|
|
start_waiting_global_read_lock(thd);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
my_error(ER_XAER_RMFAIL, MYF(0), xa_state_names[xa_state]);
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
|
|
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
|
|
thd->transaction.all.modified_non_trans_table= FALSE;
|
|
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
|
|
xid_cache_delete(&thd->transaction.xid_state);
|
|
thd->transaction.xid_state.xa_state= XA_NOTR;
|
|
|
|
DBUG_RETURN(res);
|
|
}
|
|
|
|
|
|
/**
|
|
Roll back and terminate a XA transaction.
|
|
|
|
@param thd Current thread
|
|
|
|
@retval FALSE Success
|
|
@retval TRUE Failure
|
|
*/
|
|
|
|
bool trans_xa_rollback(THD *thd)
|
|
{
|
|
bool res= TRUE;
|
|
enum xa_states xa_state= thd->transaction.xid_state.xa_state;
|
|
DBUG_ENTER("trans_xa_rollback");
|
|
|
|
if (!thd->transaction.xid_state.xid.eq(thd->lex->xid))
|
|
{
|
|
XID_STATE *xs= xid_cache_search(thd->lex->xid);
|
|
if (!xs || xs->in_thd)
|
|
my_error(ER_XAER_NOTA, MYF(0));
|
|
else
|
|
{
|
|
xa_trans_rolled_back(xs);
|
|
ha_commit_or_rollback_by_xid(thd->lex->xid, 0);
|
|
xid_cache_delete(xs);
|
|
}
|
|
DBUG_RETURN(thd->stmt_da->is_error());
|
|
}
|
|
|
|
if (xa_state != XA_IDLE && xa_state != XA_PREPARED && xa_state != XA_ROLLBACK_ONLY)
|
|
{
|
|
my_error(ER_XAER_RMFAIL, MYF(0), xa_state_names[xa_state]);
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
|
|
/*
|
|
Resource Manager error is meaningless at this point, as we perform
|
|
explicit rollback request by user. We must reset rm_error before
|
|
calling ha_rollback(), so thd->transaction.xid structure gets reset
|
|
by ha_rollback()/THD::transaction::cleanup().
|
|
*/
|
|
thd->transaction.xid_state.rm_error= 0;
|
|
if ((res= test(ha_rollback_trans(thd, TRUE))))
|
|
my_error(ER_XAER_RMERR, MYF(0));
|
|
|
|
thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
|
|
thd->transaction.all.modified_non_trans_table= FALSE;
|
|
thd->server_status&= ~SERVER_STATUS_IN_TRANS;
|
|
xid_cache_delete(&thd->transaction.xid_state);
|
|
thd->transaction.xid_state.xa_state= XA_NOTR;
|
|
|
|
DBUG_RETURN(res);
|
|
}
|