Below is the list of changes that have just been committed into a local
5.1 repository of istruewing. When istruewing does a push these changes will
be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html
ChangeSet@stripped, 2007-01-03 09:53:01+01:00, istruewing@stripped +15 -0
WL#3561 - transactional LOCK TABLE
WL 3594 - transactional LOCK TABLE Testing
Added a new clause to the LOCK TABLE statement:
LOCK TABLE table_name IN { SHARE | EXCLUSIVE } MODE [NOWAIT]
This allows for transactional locks.
- They do _not_ commit a transaction.
- One can access tables not mentioned in the LOCK TABLE statement.
- One can add more tables to the locked set.
- Tables are unlocked at the end of transaction.
If
- transactional and standard locks are mixed in a statement,
- non-transactional (standard READ/WRITE) locks exist already,
- or a storage engine does not support transactional locking,
the locks are converted to standard (READ/WRITE) locks with
warnings. In strict mode the conversion is rejected with an
error message.
mysql-test/include/locktrans.inc@stripped, 2007-01-03 09:52:58+01:00, istruewing@stripped +874 -0
WL#3561 - transactional LOCK TABLE
New main test code for inclusion by top-level test scripts.
mysql-test/include/locktrans.inc@stripped, 2007-01-03 09:52:58+01:00, istruewing@stripped +0 -0
mysql-test/r/locktrans_innodb.result@stripped, 2007-01-03 09:52:58+01:00, istruewing@stripped +771 -0
WL#3561 - transactional LOCK TABLE
New test result.
mysql-test/r/locktrans_innodb.result@stripped, 2007-01-03 09:52:58+01:00, istruewing@stripped +0 -0
mysql-test/t/locktrans_innodb-master.opt@stripped, 2007-01-03 09:52:58+01:00, istruewing@stripped +1 -0
WL#3561 - transactional LOCK TABLE
Options for new test case.
mysql-test/t/locktrans_innodb-master.opt@stripped, 2007-01-03 09:52:58+01:00, istruewing@stripped +0 -0
mysql-test/t/locktrans_innodb.test@stripped, 2007-01-03 09:52:58+01:00, istruewing@stripped +17 -0
WL#3561 - transactional LOCK TABLE
New top-level test script.
mysql-test/t/locktrans_innodb.test@stripped, 2007-01-03 09:52:58+01:00, istruewing@stripped +0 -0
sql/handler.h@stripped, 2007-01-03 09:52:56+01:00, istruewing@stripped +38 -0
WL#3561 - transactional LOCK TABLE
Defined transactional lock types.
Declared new handler::lock_table() virtual method
with default implementation.
sql/lex.h@stripped, 2007-01-03 09:52:57+01:00, istruewing@stripped +2 -0
WL#3561 - transactional LOCK TABLE
New symbols "EXCLUSIVE" and "NOWAIT".
sql/lex_symbol.h@stripped, 2007-01-03 09:52:57+01:00, istruewing@stripped +9 -0
WL#3561 - transactional LOCK TABLE
Declared struct st_table_lock_info for parser internal use.
sql/lock.cc@stripped, 2007-01-03 09:52:57+01:00, istruewing@stripped +238 -0
WL#3561 - transactional LOCK TABLE
Defined try_transactional_lock() and
check_transactional_lock() for transactional table locking.
Defined set_handler_table_locks() for non-transactional
table locking.
sql/mysql_priv.h@stripped, 2007-01-03 09:52:57+01:00, istruewing@stripped +3 -0
WL#3561 - transactional LOCK TABLE
Declared try_transactional_lock() and
check_transactional_lock() for transactional table locking.
Declared set_handler_table_locks() for non-transactional
table locking.
sql/share/errmsg.txt@stripped, 2007-01-03 09:52:58+01:00, istruewing@stripped +10 -0
WL#3561 - transactional LOCK TABLE
Added new error messages.
sql/sql_lex.h@stripped, 2007-01-03 09:52:57+01:00, istruewing@stripped +2 -1
WL#3561 - transactional LOCK TABLE
Moved the inclusion of lex_symbol.h to allow parser local
declaration of struct st_table_lock_info.
Added lock_transactional to LEX.
sql/sql_parse.cc@stripped, 2007-01-03 09:52:57+01:00, istruewing@stripped +46 -2
WL#3561 - transactional LOCK TABLE
Added calls to the new functions try_transactional_lock() and
check_transactional_lock() below SQLCOM_LOCK_TABLES.
Moved check_table_access() before trying to lock/unlock.
sql/sql_yacc.yy@stripped, 2007-01-03 09:52:58+01:00, istruewing@stripped +89 -12
WL#3561 - transactional LOCK TABLE
Added type 'table_lock_info'.
Added symbols EXCLUSIVE_SYM and NOWAIT_SYM.
Replaced 'lock_option' by 'table_lock_info'.
Added new clauses for transactional locking.
sql/table.h@stripped, 2007-01-03 09:52:58+01:00, istruewing@stripped +3 -0
WL#3561 - transactional LOCK TABLE
Added transactional locking variables in TABLE_LIST.
storage/innobase/handler/ha_innodb.h@stripped, 2007-01-03 09:52:58+01:00, istruewing@stripped +16 -0
WL#3561 - transactional LOCK TABLE
New ha_innodb::lock_table() method, preliminarily calling
ha_innodb::transactional_table_lock() for transactional locks.
# This is a BitKeeper patch. What follows are the unified diffs for the
# set of deltas contained in the patch. The rest of the patch, the part
# that BitKeeper cares about, is below these diffs.
# User: istruewing
# Host: chilla.local
# Root: /home/mydev/mysql-5.1-wl3561-one
--- 1.248/sql/handler.h 2007-01-03 09:53:11 +01:00
+++ 1.249/sql/handler.h 2007-01-03 09:53:11 +01:00
@@ -218,6 +218,10 @@
#define HA_BLOCK_LOCK 256 /* unlock when reading some records */
#define HA_OPEN_TEMPORARY 512
+/* For transactional LOCK TABLE. handler::lock_table() */
+#define HA_LOCK_IN_SHARE_MODE F_RDLCK
+#define HA_LOCK_IN_EXCLUSIVE_MODE F_WRLCK
+
/* Some key definitions */
#define HA_KEY_NULL_LENGTH 1
#define HA_KEY_BLOB_LENGTH 2
@@ -1598,6 +1602,40 @@ public:
but we don't have a primary key
*/
virtual void use_hidden_primary_key();
+
+ /*
+ Lock table.
+
+ SYNOPSIS
+ handler::lock_table()
+ thd Thread handle
+ lock_type HA_LOCK_IN_SHARE_MODE (F_RDLCK)
+ HA_LOCK_IN_EXCLUSIVE_MODE (F_WRLCK)
+ lock_timeout -1 default timeout
+ 0 no wait
+ >0 wait timeout in milliseconds.
+
+ NOTE
+ lock_timeout >0 is not used by MySQL currently. If the storage
+ engine does not support NOWAIT (lock_timeout == 0) it should
+ return an error. But if it does not support WAIT X (lock_timeout
+ >0) it should treat it as lock_timeout == -1 and wait a default
+ (or even hard-coded) timeout.
+
+ RETURN
+ HA_ERR_WRONG_COMMAND Storage engine does not support lock_table()
+ HA_ERR_UNSUPPORTED Storage engine does not support
+ non-default timeout like NOWAIT or WAIT [X]
+ HA_ERR_LOCK_WAIT_TIMEOUT Lock request timed out or
+ lock conflict with NOWAIT option
+ HA_ERR_LOCK_DEADLOCK Deadlock detected
+ */
+ virtual int lock_table(THD *thd __attribute__((unused)),
+ int lock_type __attribute__((unused)),
+ int lock_timeout __attribute__((unused)))
+ {
+ return HA_ERR_WRONG_COMMAND;
+ }
private:
/*
--- 1.166/sql/lex.h 2007-01-03 09:53:11 +01:00
+++ 1.167/sql/lex.h 2007-01-03 09:53:11 +01:00
@@ -190,6 +190,7 @@ static SYMBOL symbols[] = {
{ "EVENT", SYM(EVENT_SYM)},
{ "EVENTS", SYM(EVENTS_SYM)},
{ "EVERY", SYM(EVERY_SYM)},
+ { "EXCLUSIVE", SYM(EXCLUSIVE_SYM)},
{ "EXECUTE", SYM(EXECUTE_SYM)},
{ "EXISTS", SYM(EXISTS)},
{ "EXIT", SYM(EXIT_SYM)},
@@ -357,6 +358,7 @@ static SYMBOL symbols[] = {
{ "NODEGROUP", SYM(NODEGROUP_SYM)},
{ "NONE", SYM(NONE_SYM)},
{ "NOT", SYM(NOT_SYM)},
+ { "NOWAIT", SYM(NOWAIT_SYM)},
{ "NO_WRITE_TO_BINLOG", SYM(NO_WRITE_TO_BINLOG)},
{ "NULL", SYM(NULL_SYM)},
{ "NUMERIC", SYM(NUMERIC_SYM)},
--- 1.8/sql/lex_symbol.h 2007-01-03 09:53:11 +01:00
+++ 1.9/sql/lex_symbol.h 2007-01-03 09:53:11 +01:00
@@ -20,6 +20,15 @@
#ifndef _lex_symbol_h
#define _lex_symbol_h
+#include "thr_lock.h"
+/* A helper type for transactional locking. */
+struct st_table_lock_info
+{
+ thr_lock_type lock_type;
+ int lock_timeout;
+ bool lock_transactional;
+};
+
struct st_sym_group;
typedef struct st_symbol {
--- 1.98/sql/lock.cc 2007-01-03 09:53:11 +01:00
+++ 1.99/sql/lock.cc 2007-01-03 09:53:11 +01:00
@@ -1384,3 +1384,241 @@ void broadcast_refresh(void)
}
+/*
+ Try to get transactional table locks for the tables in the list.
+
+ SYNOPSIS
+ try_transactional_lock()
+ thd Thread handle
+ table_list List of tables to lock
+
+ DESCRIPTION
+ This is called if transactional table locks are requested for all
+ tables in table_list and no non-transactional locks pre-exist.
+
+ RETURN
+ 0 OK. All tables are transactional locked.
+ 1 Error: must fall back to non-transactional locks.
+ -1 Error: no recovery possible.
+*/
+
+int try_transactional_lock(THD *thd, TABLE_LIST *table_list)
+{
+ TABLE_LIST *tlist;
+ uint dummy_counter;
+ int error= 0;
+ int result= 0;
+ DBUG_ENTER("try_transactional_lock");
+
+ /* Need to open the tables to be able to access engine methods. */
+ if (open_tables(thd, &table_list, &dummy_counter, 0))
+ {
+ /* purecov: begin tested */
+ DBUG_PRINT("lock_info", ("aborting, open_tables failed"));
+ DBUG_RETURN(-1);
+ /* purecov: end */
+ }
+
+ /* Required by InnoDB. */
+ thd->in_lock_tables= TRUE;
+
+ for (tlist= table_list; tlist; tlist= tlist->next_global)
+ {
+ int lock_type;
+ int lock_timeout;
+
+ if (tlist->placeholder())
+ continue;
+
+ DBUG_ASSERT((tlist->lock_type == TL_READ) ||
+ (tlist->lock_type == TL_READ_NO_INSERT) ||
+ (tlist->lock_type == TL_WRITE) ||
+ (tlist->lock_type == TL_WRITE_LOW_PRIORITY));
+
+ /*
+ Every tlist object has a proper lock_type set. Even if it came in
+ the list as a base table from a view only.
+ */
+ lock_type= ((tlist->lock_type <= TL_READ_NO_INSERT) ?
+ HA_LOCK_IN_SHARE_MODE : HA_LOCK_IN_EXCLUSIVE_MODE);
+ /*
+ The lock timeout is not set if this table belongs to a view. We
+ need to take it from the top-level view. After this loop
+ iteration, lock_timeout is not needed any more. Not even if the
+ locks are converted to non-transactional locks later.
+ Non-transactional locks do not support a lock_timeout.
+ */
+ lock_timeout= tlist->top_table()->lock_timeout;
+ /*
+ For warning/error reporting we need to set the intended lock
+ method in the TABLE_LIST object. It will be used later by
+ check_transactional_lock(). The lock method is not set if this
+ table belongs to a view. We can safely set it to transactional
+ locking here. Even for non-view tables. This function is not
+ called if non-transactional locking was requested for any object.
+ */
+ tlist->lock_transactional= TRUE;
+
+ DBUG_PRINT("lock_info", ("table: '%s' lock_type: %d timeout: %d",
+ tlist->alias ? tlist->alias : tlist->table_name,
+ lock_type, lock_timeout));
+
+ /*
+ Because we need to set the lock method (see above) for all
+ involved tables, we cannot break the loop on an error.
+ But we do not try more locks after the first error.
+ */
+ if (!error)
+ {
+ error= tlist->table->file->lock_table(thd, lock_type, lock_timeout);
+ if (error && (error != HA_ERR_WRONG_COMMAND))
+ tlist->table->file->print_error(error, MYF(0));
+ }
+ }
+
+ if (error)
+ {
+ /*
+ Not all transactional locks could be taken. If the error was
+ something else but "unsupported by storage engine", abort the
+ execution of this statement.
+ */
+ if (error != HA_ERR_WRONG_COMMAND)
+ {
+ DBUG_PRINT("lock_info", ("aborting, lock_table failed"));
+ result= -1;
+ goto err;
+ }
+ /*
+ Fall back to non-transactional locks because transactional locks
+ are unsupported by a storage engine. No need to unlock the
+ successfully taken transactional locks. They go away at end of
+ transaction anyway.
+ */
+ DBUG_PRINT("lock_info", ("fall back to non-trans lock: no SE support"));
+ result= 1;
+ }
+
+ err:
+ /* We need to explicitly commit if autocommit mode is active. */
+ (void) ha_autocommit_or_rollback(thd, 0);
+ /* Close the tables. The locks (if taken) persist in the storage engines. */
+ close_tables_for_reopen(thd, &table_list);
+ DBUG_PRINT("lock_info", ("result: %d", result));
+ thd->in_lock_tables= FALSE;
+ DBUG_RETURN(result);
+}
+
+
+/*
+ Check if lock method conversion was done and was allowed.
+
+ SYNOPSIS
+ check_transactional_lock()
+ thd Thread handle
+ table_list List of tables to lock
+
+ DESCRIPTION
+
+ Lock method conversion can be done during parsing if one of the
+ locks is non-transactional. It can also happen if non-transactional
+ table locks exist when the statement is executed or if a storage
+ engine does not support transactional table locks.
+
+ Check if transactional table locks have been converted to
+ non-transactional and if this was allowed. In a running transaction
+ or in strict mode lock method conversion is not allowed - report an
+ error. Otherwise it is allowed - issue a warning.
+
+ RETURN
+ 0 OK. Proceed with non-transactional locks.
+ -1 Error: Lock conversion is prohibited.
+*/
+
+int check_transactional_lock(THD *thd, TABLE_LIST *table_list)
+{
+ TABLE_LIST *tlist;
+ int result= 0;
+ char warn_buff[MYSQL_ERRMSG_SIZE];
+ DBUG_ENTER("check_transactional_lock");
+
+ for (tlist= table_list; tlist; tlist= tlist->next_global)
+ {
+ if (tlist->placeholder() || !tlist->lock_transactional)
+ continue;
+
+ /* We must not convert the lock method in strict mode. */
+ if (thd->variables.sql_mode & (MODE_STRICT_ALL_TABLES |
+ MODE_STRICT_TRANS_TABLES))
+ {
+ my_error(ER_NO_AUTO_CONVERT_LOCK_STRICT, MYF(0),
+ tlist->alias ? tlist->alias : tlist->table_name);
+ result= -1;
+ continue;
+ }
+
+ /* We must not convert the lock method within an active transaction. */
+ if (thd->active_transaction())
+ {
+ my_error(ER_NO_AUTO_CONVERT_LOCK_TRANSACTION, MYF(0),
+ tlist->alias ? tlist->alias : tlist->table_name);
+ result= -1;
+ continue;
+ }
+
+ /* Warn about the conversion. */
+ my_snprintf(warn_buff, sizeof(warn_buff), ER(ER_WARN_AUTO_CONVERT_LOCK),
+ tlist->alias ? tlist->alias : tlist->table_name);
+ push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
+ ER_WARN_AUTO_CONVERT_LOCK, warn_buff);
+ }
+
+ DBUG_PRINT("lock_info", ("result: %d", result));
+ DBUG_RETURN(result);
+}
+
+
+/*
+ Set table locks in the table handler.
+
+ SYNOPSIS
+ set_handler_table_locks()
+ thd Thread handle
+ table_list List of tables to lock
+
+ RETURN
+ void
+*/
+
+void set_handler_table_locks(THD *thd, TABLE_LIST *table_list)
+{
+ TABLE_LIST *tlist;
+ DBUG_ENTER("set_handler_table_locks");
+
+ for (tlist= table_list; tlist; tlist= tlist->next_global)
+ {
+ int lock_type;
+
+ if (tlist->placeholder())
+ continue;
+
+ DBUG_ASSERT((tlist->lock_type == TL_READ) ||
+ (tlist->lock_type == TL_READ_NO_INSERT) ||
+ (tlist->lock_type == TL_WRITE) ||
+ (tlist->lock_type == TL_WRITE_LOW_PRIORITY));
+
+ /*
+ Every tlist object has a proper lock_type set. Even if it came in
+ the list as a base table from a view only.
+ */
+ lock_type= ((tlist->lock_type <= TL_READ_NO_INSERT) ?
+ HA_LOCK_IN_SHARE_MODE : HA_LOCK_IN_EXCLUSIVE_MODE);
+
+ /* Timeout is always -1 (default) for non-transactional locks. */
+ (void) tlist->table->file->lock_table(thd, lock_type, -1);
+ }
+
+ DBUG_VOID_RETURN;
+}
+
+
--- 1.464/sql/mysql_priv.h 2007-01-03 09:53:11 +01:00
+++ 1.465/sql/mysql_priv.h 2007-01-03 09:53:11 +01:00
@@ -1704,6 +1704,9 @@ bool make_global_read_lock_block_commit(
bool set_protect_against_global_read_lock(void);
void unset_protect_against_global_read_lock(void);
void broadcast_refresh(void);
+int try_transactional_lock(THD *thd, TABLE_LIST *table_list);
+int check_transactional_lock(THD *thd, TABLE_LIST *table_list);
+void set_handler_table_locks(THD *thd, TABLE_LIST *table_list);
/* Lock based on name */
int lock_and_wait_for_table_name(THD *thd, TABLE_LIST *table_list);
--- 1.252/sql/sql_lex.h 2007-01-03 09:53:11 +01:00
+++ 1.253/sql/sql_lex.h 2007-01-03 09:53:11 +01:00
@@ -40,8 +40,8 @@ class Event_parse_data;
#ifdef MYSQL_YACC
#define LEX_YYSTYPE void *
#else
-#include "lex_symbol.h"
#if MYSQL_LEX
+#include "lex_symbol.h"
#include "sql_yacc.h"
#define LEX_YYSTYPE YYSTYPE *
#else
@@ -1007,6 +1007,7 @@ typedef struct st_lex : public Query_tab
union {
enum ha_rkey_function ha_rkey_mode;
enum xa_option_words xa_opt;
+ bool lock_transactional; /* For LOCK TABLE ... IN ... MODE */
};
enum enum_var_type option_type;
enum enum_view_create_mode create_view_mode;
--- 1.612/sql/sql_parse.cc 2007-01-03 09:53:11 +01:00
+++ 1.613/sql/sql_parse.cc 2007-01-03 09:53:11 +01:00
@@ -3784,11 +3784,51 @@ end_with_restore_list:
send_ok(thd);
break;
case SQLCOM_LOCK_TABLES:
+ {
+ if (check_table_access(thd, LOCK_TABLES_ACL | SELECT_ACL, all_tables, 0))
+ goto error;
+ /*
+ We try to take transactional locks if
+ - only transactional locks are requested (lex->lock_transactional) and
+ - no non-transactional locks exist (!thd->locked_tables).
+ */
+ DBUG_PRINT("lock_info", ("lex->lock_transactional: %d "
+ "thd->locked_tables: 0x%lx",
+ lex->lock_transactional,
+ (long) thd->locked_tables));
+ if (lex->lock_transactional && !thd->locked_tables)
+ {
+ int rc;
+ /*
+ All requested locks are transactional and no non-transactional
+ locks exist.
+ */
+ if ((rc= try_transactional_lock(thd, all_tables)) == -1)
+ goto error;
+ if (rc == 0)
+ {
+ send_ok(thd);
+ break;
+ }
+ /*
+ Non-transactional locking has been requested or
+ non-transactional locks exist already or transactional locks are
+ not supported by all storage engines. Take non-transactional
+ locks.
+ */
+ }
+ /*
+ One or more requested locks are non-transactional and/or
+ non-transactional locks exist or a storage engine does not support
+ transactional locks. Check if at least one transactional lock is
+ requested. If yes, warn about the conversion to non-transactional
+ locks or abort in strict mode.
+ */
+ if (check_transactional_lock(thd, all_tables))
+ goto error;
unlock_locked_tables(thd);
if (end_active_trans(thd))
goto error;
- if (check_table_access(thd, LOCK_TABLES_ACL | SELECT_ACL, all_tables, 0))
- goto error;
thd->in_lock_tables=1;
thd->options|= OPTION_TABLE_LOCK;
@@ -3800,12 +3840,16 @@ end_with_restore_list:
#endif /*HAVE_QUERY_CACHE*/
thd->locked_tables=thd->lock;
thd->lock=0;
+ set_handler_table_locks(thd, all_tables);
send_ok(thd);
+ DBUG_PRINT("lock_info", ("thd->locked_tables: 0x%lx",
+ (long) thd->locked_tables));
}
else
thd->options&= ~(ulong) (OPTION_TABLE_LOCK);
thd->in_lock_tables=0;
break;
+ }
case SQLCOM_CREATE_DB:
{
if (end_active_trans(thd))
--- 1.524/sql/sql_yacc.yy 2007-01-03 09:53:11 +01:00
+++ 1.525/sql/sql_yacc.yy 2007-01-03 09:53:11 +01:00
@@ -140,6 +140,7 @@ static bool is_native_function(THD *thd,
enum Item_udftype udf_type;
CHARSET_INFO *charset;
thr_lock_type lock_type;
+ struct st_table_lock_info table_lock_info;
interval_type interval, interval_time_st;
timestamp_type date_time_type;
st_select_lex *select_lex;
@@ -322,6 +323,7 @@ bool my_yyoverflow(short **a, YYSTYPE **
%token EVENTS_SYM
%token EVENT_SYM
%token EVERY_SYM /* SQL-2003-N */
+%token EXCLUSIVE_SYM
%token EXECUTE_SYM /* SQL-2003-R */
%token EXISTS /* SQL-2003-R */
%token EXIT_SYM
@@ -492,6 +494,7 @@ bool my_yyoverflow(short **a, YYSTYPE **
%token NOT2_SYM
%token NOT_SYM /* SQL-2003-R */
%token NOW_SYM
+%token NOWAIT_SYM
%token NO_SYM /* SQL-2003-R */
%token NO_WAIT_SYM
%token NO_WRITE_TO_BINLOG
@@ -762,7 +765,7 @@ bool my_yyoverflow(short **a, YYSTYPE **
text_string opt_gconcat_separator
%type <num>
- type int_type real_type order_dir lock_option
+ type int_type real_type order_dir
udf_type if_exists opt_local opt_table_options table_options
table_option opt_if_not_exists opt_no_write_to_binlog
delete_option opt_temporary all_or_any opt_distinct
@@ -772,6 +775,8 @@ bool my_yyoverflow(short **a, YYSTYPE **
opt_natural_language_mode opt_query_expansion
opt_ev_status opt_ev_on_completion ev_on_completion opt_ev_comment
ev_alter_on_schedule_completion opt_ev_rename_to opt_ev_sql_stmt
+ opt_transactional_lock_timeout
+ /* opt_lock_timeout_value */
%type <ulong_num>
ulong_num real_ulong_num merge_insert_types
@@ -784,6 +789,10 @@ bool my_yyoverflow(short **a, YYSTYPE **
%type <lock_type>
replace_lock_option opt_low_priority insert_lock_option load_data_lock
+ transactional_lock_mode
+
+%type <table_lock_info>
+ table_lock_info
%type <item>
literal text_literal insert_ident order_ident
@@ -9402,6 +9411,7 @@ keyword_sp:
| EVENT_SYM {}
| EVENTS_SYM {}
| EVERY_SYM {}
+ | EXCLUSIVE_SYM { /* purecov: tested */ }
| EXPANSION_SYM {}
| EXTENDED_SYM {}
| EXTENT_SIZE_SYM {}
@@ -9487,6 +9497,7 @@ keyword_sp:
| NO_WAIT_SYM {}
| NODEGROUP_SYM {}
| NONE_SYM {}
+ | NOWAIT_SYM { /* purecov: tested */ }
| NVARCHAR_SYM {}
| OFFSET_SYM {}
| OLD_PASSWORD {}
@@ -10011,7 +10022,19 @@ set_expr_or_default:
/* Lock function */
lock:
- LOCK_SYM table_or_tables
+ LOCK_SYM
+ {
+ /*
+ Transactional locks can be taken only if all requested locks
+ are transactional. Initialize lex->lock_transactional as
+ TRUE. Any non-transactional lock request turns this to FALSE.
+ Table specific variables keep track of the locking method
+ requested for the table. This is used to warn about a
+ changed locking method later.
+ */
+ Lex->lock_transactional= TRUE;
+ }
+ table_or_tables
{
LEX *lex= Lex;
@@ -10035,19 +10058,73 @@ table_lock_list:
| table_lock_list ',' table_lock;
table_lock:
- table_ident opt_table_alias lock_option
- {
- if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
- YYABORT;
- }
+ table_ident opt_table_alias table_lock_info
+ {
+ TABLE_LIST *tlist;
+ if (!(tlist= Select->add_table_to_list(YYTHD, $1, $2, 0,
+ $3.lock_type)))
+ YYABORT; /* purecov: inspected */
+ tlist->lock_timeout= $3.lock_timeout;
+ /* Store the requested lock method for later warning. */
+ tlist->lock_transactional= $3.lock_transactional;
+ /* Compute the resulting lock method for all tables. */
+ if (!$3.lock_transactional)
+ Lex->lock_transactional= FALSE;
+ }
+ ;
+
+table_lock_info:
+ READ_SYM
+ {
+ $$.lock_type= TL_READ_NO_INSERT;
+ $$.lock_timeout= -1;
+ $$.lock_transactional= FALSE;
+ }
+ | WRITE_SYM
+ {
+ $$.lock_type= YYTHD->update_lock_default;
+ $$.lock_timeout= -1;
+ $$.lock_transactional= FALSE;
+ }
+ | LOW_PRIORITY WRITE_SYM
+ {
+ $$.lock_type= TL_WRITE_LOW_PRIORITY;
+ $$.lock_timeout= -1;
+ $$.lock_transactional= FALSE;
+ }
+ | READ_SYM LOCAL_SYM
+ {
+ $$.lock_type= TL_READ;
+ $$.lock_timeout= -1;
+ $$.lock_transactional= FALSE;
+ }
+ | IN_SYM transactional_lock_mode MODE_SYM opt_transactional_lock_timeout
+ {
+ $$.lock_type= $2;
+ $$.lock_timeout= $4;
+ $$.lock_transactional= TRUE;
+ }
+ ;
+
+/* Use thr_lock_type here for easier fallback to non-trans locking. */
+transactional_lock_mode:
+ SHARE_SYM { $$= TL_READ_NO_INSERT; }
+ | EXCLUSIVE_SYM { $$= YYTHD->update_lock_default; }
;
-lock_option:
- READ_SYM { $$=TL_READ_NO_INSERT; }
- | WRITE_SYM { $$=YYTHD->update_lock_default; }
- | LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
- | READ_SYM LOCAL_SYM { $$= TL_READ; }
+opt_transactional_lock_timeout:
+ /* empty */ { $$= -1; }
+ | NOWAIT_SYM { $$= 0; }
+ /* | WAIT_SYM opt_lock_timeout_value { $$= $2; } */
;
+
+/*
+ We have a timeout resolution of milliseconds. The WAIT argument is in
+ seconds with decimal fragments for sub-second resolution. E.g. 22.5, 0.015
+*/
+/* opt_lock_timeout_value: */
+ /* empty { $$= -1; } */
+ /* | NUM { $$= (int) (atof($1.str) * 1000.0 + 0.5); } */
unlock:
UNLOCK_SYM
--- 1.155/sql/table.h 2007-01-03 09:53:11 +01:00
+++ 1.156/sql/table.h 2007-01-03 09:53:11 +01:00
@@ -820,6 +820,9 @@ typedef struct st_table_list
used for implicit LOCK TABLES only and won't be used in real statement.
*/
bool prelocking_placeholder;
+ /* For transactional locking. */
+ int lock_timeout; /* NOWAIT or WAIT [X] */
+ bool lock_transactional; /* If transactional lock requested. */
void calc_md5(char *buffer);
void set_underlying_merge();
--- 1.137/sql/share/errmsg.txt 2007-01-03 09:53:11 +01:00
+++ 1.138/sql/share/errmsg.txt 2007-01-03 09:53:11 +01:00
@@ -6016,3 +6016,13 @@ ER_NATIVE_FCT_NAME_COLLISION
eng "This function '%-.64s' has the same name as a native function."
ER_BINLOG_PURGE_EMFILE
eng "Too many files opened, please execute the command again"
+
+ER_WARN_AUTO_CONVERT_LOCK
+ eng "Converted to non-transactional lock on '%-.64s'"
+
+ER_NO_AUTO_CONVERT_LOCK_STRICT
+ eng "Cannot convert to non-transactional lock in strict mode on '%-.64s'"
+
+ER_NO_AUTO_CONVERT_LOCK_TRANSACTION
+ eng "Cannot convert to non-transactional lock in an active transaction on '%-.64s'"
+
--- New file ---
+++ mysql-test/include/locktrans.inc 07/01/03 09:52:58
# include/locktrans.inc
#
# Transactional LOCK TABLE tests
eval SET SESSION STORAGE_ENGINE = $engine_type;
--disable_warnings
DROP DATABASE IF EXISTS mysqltest;
DROP TABLE IF EXISTS t1, t2, t3, t4, v1;
DROP VIEW IF EXISTS t1, t2, t3, t4, v1;
DROP PROCEDURE IF EXISTS lock_t1_excl;
DROP PROCEDURE IF EXISTS lock_t4_excl;
--enable_warnings
--echo #
--echo # WL3561 - transactional LOCK TABLE - Syntax tests
--echo # ================================================
#
CREATE TABLE t1 (c1 INT);
CREATE TABLE t2 (c2 INT);
CREATE TABLE t3 (c3 INT);
#
--echo #
--echo # Valid syntax for non-transactional locks.
LOCK TABLE t1 READ, t2 WRITE;
UNLOCK TABLES;
LOCK TABLE t1 READ LOCAL, t2 LOW_PRIORITY WRITE;
UNLOCK TABLES;
#
--echo #
--echo # Valid syntax for transactional locks.
LOCK TABLE t1 IN SHARE MODE, t2 IN EXCLUSIVE MODE;
UNLOCK TABLES;
#
if ($nowait_support)
{
--echo #
--echo # Valid syntax for transactional locks with NOWAIT option.
--echo ## In the preliminary reference implementation we expect these errors:
--echo ## NOWAIT+SHARE="timed out", NOWAIT+EXCLUSIVE="not supported".
--echo ## Statements abort on first error.
--error ER_LOCK_WAIT_TIMEOUT
LOCK TABLE t1 IN SHARE MODE NOWAIT, t2 IN EXCLUSIVE MODE NOWAIT;
--error ER_UNSUPPORTED_EXTENSION
LOCK TABLE t1 IN EXCLUSIVE MODE NOWAIT, t2 IN SHARE MODE NOWAIT;
--error ER_LOCK_WAIT_TIMEOUT
LOCK TABLE t1 IN EXCLUSIVE MODE, t2 IN SHARE MODE NOWAIT;
}
#
--echo #
--echo # Valid syntax for aliases with and without 'AS'.
LOCK TABLE t1 AS a1 READ, t2 a2 WRITE;
UNLOCK TABLES;
LOCK TABLE t1 AS a1 IN SHARE MODE, t2 a2 IN EXCLUSIVE MODE;
UNLOCK TABLES;
#
--echo #
--echo # Transactional locks taken on a view.
CREATE VIEW v1 AS SELECT * FROM t1, t2 WHERE t1.c1 = t2.c2;
LOCK TABLE v1 IN SHARE MODE;
LOCK TABLE v1 IN EXCLUSIVE MODE;
DROP VIEW v1;
#
--echo #
--echo # Locking INFORMATION_SCHEMA fails on missing privileges.
--error ER_DBACCESS_DENIED_ERROR
LOCK TABLE information_schema.tables IN SHARE MODE;
--error ER_DBACCESS_DENIED_ERROR
LOCK TABLE information_schema.tables IN EXCLUSIVE MODE;
--error ER_DBACCESS_DENIED_ERROR
LOCK TABLE information_schema.tables READ;
--error ER_DBACCESS_DENIED_ERROR
LOCK TABLE information_schema.tables WRITE;
#
--echo #
--echo # The new keywords EXCLUSIVE and NOWAIT are not reserved words.
CREATE TABLE t4 (exclusive INT, nowait INT);
DROP TABLE t4;
#
--echo #
--echo # Syntax errors for misspelled modes or left out symbols.
--echo ##-------------------------------------------------------
--error ER_PARSE_ERROR
LOCK TABLE t1 IN SHARED MODE;
--error ER_PARSE_ERROR
LOCK TABLE t1 SHARE MODE;
--error ER_PARSE_ERROR
LOCK TABLE t1 IN SHARE;
--error ER_PARSE_ERROR
LOCK TABLE t1 IN MODE;
--error ER_PARSE_ERROR
LOCK TABLE t1 READ NOWAIT, t2 WRITE NOWAIT;
--error ER_PARSE_ERROR
LOCK TABLE t1 READ NOWAIT, t2 IN EXCLUSIVE MODE NOWAIT;
--error ER_PARSE_ERROR
LOCK TABLE t1 IN SHARE MODE NOWAIT, t2 WRITE NOWAIT;
--error ER_PARSE_ERROR
LOCK TABLE t1 IN SHARED MODE NOWAIT;
--error ER_PARSE_ERROR
LOCK TABLE t1 SHARE MODE NOWAIT;
--error ER_PARSE_ERROR
LOCK TABLE t1 IN SHARE NOWAIT;
--error ER_PARSE_ERROR
LOCK TABLE t1 IN MODE NOWAIT;
--echo ##----------------------
--echo ## End of syntax errors.
#
--echo #
--echo #
--echo # WL3561 - transactional LOCK TABLE - Lock method conversion
--echo # ==========================================================
--echo #
--echo # Implicit lock method conversion due to mix in statement.
LOCK TABLE t1 READ, t2 IN EXCLUSIVE MODE;
UNLOCK TABLES;
--echo # Lock t1 share (converted to read), t2 write.
LOCK TABLE t1 IN SHARE MODE, t2 WRITE;
--echo # Show t1 is read locked, t2 write locked.
--error ER_TABLE_NOT_LOCKED_FOR_WRITE
INSERT INTO t1 SELECT * FROM t2;
INSERT INTO t2 SELECT * FROM t1;
#
--echo #
--echo # Implicit lock method conversion due to existing non-transact. locks.
--echo # Implicitly unlock existing non-transactional locks and take new ones.
--echo # Lock t1 exclusive (converted to write), t2 share (converted to read).
LOCK TABLE t1 IN EXCLUSIVE MODE, t2 IN SHARE MODE;
--echo # Show t1 is write locked, t2 read locked.
INSERT INTO t1 SELECT * FROM t2;
--error ER_TABLE_NOT_LOCKED_FOR_WRITE
INSERT INTO t2 SELECT * FROM t1;
UNLOCK TABLES;
#
--echo #
--echo # Reject lock method conversion in strict mode.
--echo # Set strict mode.
SET @wl3561_save_sql_mode= @@SQL_MODE;
SET @@SQL_MODE= 'STRICT_ALL_TABLES';
--echo # Try mixed mode locks.
--error ER_NO_AUTO_CONVERT_LOCK_STRICT
LOCK TABLE t1 READ, t2 IN EXCLUSIVE MODE;
--error ER_NO_AUTO_CONVERT_LOCK_STRICT
LOCK TABLE t1 IN SHARE MODE, t2 WRITE;
--echo # Lock non-transactional.
LOCK TABLE t1 READ, t2 WRITE;
--echo # Try transactional locks on top of the existing non-transactional locks.
--error ER_NO_AUTO_CONVERT_LOCK_STRICT
LOCK TABLE t1 IN SHARE MODE, t2 IN EXCLUSIVE MODE;
--echo ## Error is reported on first table only. Show both errors:
SHOW WARNINGS;
UNLOCK TABLES;
SET @@SQL_MODE= @wl3561_save_sql_mode;
#
--echo #
--echo # Reject lock method conversion in an active transaction.
--echo # Start transaction.
START TRANSACTION;
--echo # Try mixed mode locks.
--error ER_NO_AUTO_CONVERT_LOCK_TRANSACTION
LOCK TABLE t1 READ, t2 IN EXCLUSIVE MODE;
--error ER_NO_AUTO_CONVERT_LOCK_TRANSACTION
LOCK TABLE t1 IN SHARE MODE, t2 WRITE;
COMMIT;
#
--echo #
--echo # Implicit lock method conversion for non-transactional storage engine.
--echo # Create a non-transactional table.
eval CREATE TABLE t4 (c4 INT) ENGINE= $other_non_trans_engine_type;
--echo # Request a transactional lock, which is converted to non-transactional.
LOCK TABLE t4 IN SHARE MODE;
--echo # Try a conflict with the existing non-transactional lock.
--error ER_TABLE_NOT_LOCKED_FOR_WRITE
INSERT INTO t4 VALUES(444);
UNLOCK TABLES;
--echo # Set strict mode.
SET @@SQL_MODE= 'STRICT_ALL_TABLES';
--echo # Try a transactional lock, which would need a conversion.
--error ER_NO_AUTO_CONVERT_LOCK_STRICT
LOCK TABLE t4 IN SHARE MODE;
SET @@SQL_MODE= @wl3561_save_sql_mode;
#
--echo #
--echo # View with transactional and non-transactional storage engine.
CREATE VIEW v1 AS SELECT * FROM t3, t4 WHERE t3.c3 = t4.c4;
--echo # Request a share lock on the view, which is converted to read locks.
LOCK TABLE v1 IN SHARE MODE;
--echo # Show that read locks on the base tables prohibit writing ...
--error ER_TABLE_NOT_LOCKED_FOR_WRITE
INSERT INTO t3 SELECT * FROM t4;
--error ER_TABLE_NOT_LOCKED_FOR_WRITE
INSERT INTO t4 SELECT * FROM t3;
--echo # ... but allow reading.
SELECT COUNT(*) FROM t3, t4 WHERE t3.c3 = t4.c4;
SELECT COUNT(*) FROM v1;
--echo ## Report conversion on view due to existing non-transactional locks.
LOCK TABLE v1 IN EXCLUSIVE MODE;
INSERT INTO t3 VALUES(333);
INSERT INTO t4 VALUES(444);
--error ER_TABLE_NOT_LOCKED
INSERT INTO t1 VALUES(111);
UNLOCK TABLES;
--echo ## Now report conversion on base table again.
LOCK TABLE v1 IN EXCLUSIVE MODE;
INSERT INTO t3 VALUES(333);
INSERT INTO t4 VALUES(444);
--error ER_TABLE_NOT_LOCKED
INSERT INTO t1 VALUES(111);
UNLOCK TABLES;
DROP VIEW v1;
DELETE FROM t4;
#
--echo #
--echo # Insufficient privileges do not unlock tables nor end transactions.
--echo # Prepare database, tables and an user with insufficient privileges.
--echo # Make a new connection with this user.
CREATE DATABASE mysqltest;
CREATE TABLE mysqltest.t5 (c5 INT);
CREATE TABLE mysqltest.t6 (c6 INT);
CREATE USER mysqltest_1@localhost;
GRANT SELECT, INSERT ON mysqltest.* TO mysqltest_1@localhost;
connect (conn1,localhost,mysqltest_1,,);
--echo # connection conn1.
connection conn1;
--echo # Show sufficient privileges to lock tables in the test database.
LOCK TABLE t1 READ, t2 WRITE;
--echo # Show insufficient privileges in the mysqltest database.
--error ER_DBACCESS_DENIED_ERROR
LOCK TABLE mysqltest.t5 READ, mysqltest.t6 WRITE;
--echo # Show that the locks in 'test' still exist.
--error ER_TABLE_NOT_LOCKED_FOR_WRITE
INSERT INTO t1 SELECT * FROM t2;
INSERT INTO t2 SELECT * FROM t1;
--echo # Unlock tables.
UNLOCK TABLES;
--echo # Start transaction.
START TRANSACTION;
--echo # Insert a value.
INSERT INTO t1 VALUES(111);
--echo # Try a lock that fails on privileges.
--error ER_DBACCESS_DENIED_ERROR
LOCK TABLE mysqltest.t5 READ;
--echo # Rollback transaction.
ROLLBACK;
--echo # Show that the inserted value has gone.
SELECT * FROM t1;
--echo # Drop the connection with the unprivileged user.
disconnect conn1;
--echo # connection default.
connection default;
--echo #
--echo # Sufficient privileges do unlock tables and end transactions.
--echo # Grant sufficient privileges to the user.
--echo # Make a new connection with this user.
GRANT SELECT, INSERT, LOCK TABLES ON mysqltest.* TO mysqltest_1@localhost;
connect (conn1,localhost,mysqltest_1,,);
--echo # connection conn1.
connection conn1;
--echo # Lock tables in the test database.
LOCK TABLE t1 READ, t2 WRITE;
--echo # Lock tables in the mysqltest database.
LOCK TABLE mysqltest.t5 READ, mysqltest.t6 WRITE;
--echo # Show that the locks in 'test' have been replaced ...
--error ER_TABLE_NOT_LOCKED
INSERT INTO t1 SELECT * FROM t2;
--error ER_TABLE_NOT_LOCKED
INSERT INTO t2 SELECT * FROM t1;
--echo # ... by the locks in 'mysqltest'.
--error ER_TABLE_NOT_LOCKED_FOR_WRITE
INSERT INTO mysqltest.t5 SELECT * FROM mysqltest.t6;
INSERT INTO mysqltest.t6 SELECT * FROM mysqltest.t5;
--echo # Unlock tables.
UNLOCK TABLES;
--echo # Start transaction.
START TRANSACTION;
--echo # Insert a value.
INSERT INTO t1 VALUES(111);
--echo # Take a new lock.
LOCK TABLE mysqltest.t5 READ;
--echo # Rollback transaction.
ROLLBACK;
UNLOCK TABLES;
--echo # Show that the inserted value had been committed.
SELECT * FROM t1;
DELETE FROM t1;
disconnect conn1;
--echo # connection default.
connection default;
#
UNLOCK TABLES;
DROP USER mysqltest_1@localhost;
DROP DATABASE mysqltest;
DROP TABLE t1, t2, t3, t4;
#
--echo #
--echo # WL3594 - transactional LOCK TABLE Testing - Functional tests
--echo # ============================================================
#
--echo # Prepare tables and connections.
--echo # Set AUTOCOMMIT= 0 in each connection.
CREATE TABLE t1 (c1 INT);
CREATE TABLE t2 (c2 INT);
connect (conn1,localhost,root,,);
--echo # connection conn1.
connection conn1;
SET AUTOCOMMIT= 0;
connect (conn2,localhost,root,,);
--echo # connection conn2.
connection conn2;
SET AUTOCOMMIT= 0;
--echo # connection default.
connection default;
SET AUTOCOMMIT= 0;
#
--echo #
--echo # Transactional lock behaviour:
--echo # LOCK TABLE does _not_ commit a transaction.
--echo # Insert a value.
INSERT INTO t1 VALUES (111);
--echo # Lock transactional.
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # Rollback.
ROLLBACK;
--echo # Show that the inserted value has gone.
SELECT * FROM t1;
--echo #
--echo # After LOCK TABLE one can access tables not mentioned in LOCK TABLE.
--echo # Lock t1 transactional.
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # Insert a value into t2.
INSERT INTO t2 VALUES (222);
--echo # Show that the inserted value is indeed in the table.
SELECT * FROM t2;
--echo #
--echo # One can issue LOCK TABLE many times, adding more tables.
--echo # Lock t2 transactional.
LOCK TABLE t2 IN EXCLUSIVE MODE;
--echo #
--echo # LOCK TABLE does not rollback a transaction.
--echo # Show that the inserted value is still in the table.
SELECT * FROM t2;
--echo # Rollback transaction.
ROLLBACK;
--echo # Show that the inserted value has gone.
SELECT * FROM t2;
--echo #
--echo # Tables are unlocked at the end of transaction (commit).
--echo # Take an exclusive lock.
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # Commit.
COMMIT;
--echo # connection conn1.
connection conn1;
--echo # Take an exclusive lock.
--echo # This would fail after timeout if t1 is still locked.
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # Commit.
COMMIT;
--echo # connection default.
connection default;
--echo #
--echo # Tables are unlocked at the end of transaction (rollback).
--echo # Take an exclusive lock.
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # Rollback.
ROLLBACK;
--echo # connection conn1.
connection conn1;
--echo # Take an exclusive lock.
--echo # This would fail after timeout if t1 is still locked.
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # Rollback.
ROLLBACK;
--echo # connection default.
connection default;
--echo #
--echo # UNLOCK TABLES does not touch a transaction when
--echo # no non-transactional table locks exist.
--echo # Take a transactional lock.
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # Insert a value.
INSERT INTO t1 VALUES(111);
--echo # Unlock (non-transactional) table locks.
UNLOCK TABLES;
--echo # Show that the inserted value is still in the table.
SELECT * FROM t1;
--echo # Rollback.
ROLLBACK;
--echo # Show that the inserted value has gone.
SELECT * FROM t1;
--echo #
--echo # UNLOCK TABLES commits a transaction when
--echo # non-transactional table locks exist.
--echo # Take a non-transactional lock.
LOCK TABLE t1 WRITE;
--echo # Insert a value.
INSERT INTO t1 VALUES(111);
--echo # Unlock (non-transactional) table locks.
UNLOCK TABLES;
--echo # Show that the inserted value is still in the table.
SELECT * FROM t1;
--echo # Rollback.
ROLLBACK;
--echo # Show that the inserted value is still in the table.
SELECT * FROM t1;
DELETE FROM t1;
--echo #
--echo # START TRANSACTION removes a previous lock.
--echo # Take an exclusive lock.
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # Start transaction.
START TRANSACTION;
--echo # connection conn1.
connection conn1;
--echo # Take an exclusive lock.
--echo # This would fail after timeout if t1 is still locked.
LOCK TABLE t1 IN EXCLUSIVE MODE;
COMMIT;
--echo # connection default.
connection default;
COMMIT;
--echo #
--echo # With Auto commit on, transactional locks will be ignored
--echo # Set AUTOCOMMIT= 1.
SET AUTOCOMMIT= 1;
--echo # Take an exclusive lock.
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # connection conn1.
connection conn1;
--echo # Set AUTOCOMMIT= 1.
SET AUTOCOMMIT= 1;
--echo # Take an exclusive lock.
--echo # This would fail after timeout if t1 is still locked.
LOCK TABLE t1 IN SHARE MODE;
SET AUTOCOMMIT= 0;
COMMIT;
--echo # connection default.
connection default;
UNLOCK TABLES;
SET AUTOCOMMIT= 0;
COMMIT;
--echo #
--echo # With Auto commit on, transactional locks can time out.
--echo # Default connection runs in transactional mode.
--echo # Set AUTOCOMMIT= 0.
SET AUTOCOMMIT= 0;
--echo # Take an exclusive lock, which persists.
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # connection conn1.
connection conn1;
--echo # conn1 runs in autocommit mode.
--echo # Set AUTOCOMMIT= 1.
SET AUTOCOMMIT= 1;
--echo # Try an exclusive lock,
--echo # which times out though running in autocommit mode.
--error ER_LOCK_WAIT_TIMEOUT
LOCK TABLE t1 IN SHARE MODE;
SET AUTOCOMMIT= 0;
COMMIT;
--echo # connection default.
connection default;
UNLOCK TABLES;
SET AUTOCOMMIT= 0;
COMMIT;
#
--echo #
--echo # Normal WRITE locks go before readers (autocommit).
--echo # Set AUTOCOMMIT= 1.
SET AUTOCOMMIT= 1;
--echo # Insert a value.
INSERT INTO t1 VALUES(111);
--echo # Take a non-transactional lock.
LOCK TABLE t1 READ;
--echo # connection conn1.
connection conn1;
--echo # Set AUTOCOMMIT= 1.
SET AUTOCOMMIT= 1;
--echo # Take a non-transactional WRITE lock,
--echo # which waits in background until first read lock is released.
send LOCK TABLE t1 WRITE;
--echo # connection default.
connection default;
--echo # Wait for the helper thread to sit on its lock.
while (`SELECT COUNT(*) < 1 FROM INFORMATION_SCHEMA.PROCESSLIST
WHERE STATE LIKE '%lock%'`)
{
#SELECT STATE, INFO FROM INFORMATION_SCHEMA.PROCESSLIST;
--sleep 0.1
}
--echo # connection conn2.
connection conn2;
--echo # Set AUTOCOMMIT= 1.
SET AUTOCOMMIT= 1;
--echo # Take a non-transactional READ lock,
--echo # which waits in background until the WRITE lock is released.
send LOCK TABLE t1 READ;
--echo # connection default.
connection default;
--echo # Wait for the helper threads to sit on their locks.
while (`SELECT COUNT(*) < 2 FROM INFORMATION_SCHEMA.PROCESSLIST
WHERE STATE LIKE '%lock%'`)
{
#SELECT STATE, INFO FROM INFORMATION_SCHEMA.PROCESSLIST;
--sleep 0.1
}
--echo # Unlock this connections non-transactional lock.
UNLOCK TABLES;
--echo # connection conn1.
connection conn1;
--echo # Now the WRITE lock is taken.
reap;
--echo # Insert a value.
INSERT INTO t1 VALUES(1111);
--echo # Unlock table.
UNLOCK TABLES;
--echo # connection conn2.
connection conn2;
--echo # Now the READ lock is taken.
reap;
--echo # Select from the table.
SELECT * FROM t1;
--echo # Unlock table.
UNLOCK TABLES;
--echo # connection default.
connection default;
DELETE FROM t1;
--echo #
--echo # LOW_PRIORITY WRITE locks wait for readers (autocommit).
--echo # Insert a value.
INSERT INTO t1 VALUES(111);
--echo # Take a non-transactional lock.
LOCK TABLE t1 READ;
--echo # connection conn1.
connection conn1;
--echo # Take a non-transactional LOW_PRIORITY WRITE lock,
--echo # which waits in background until all read locks are released.
send LOCK TABLE t1 LOW_PRIORITY WRITE;
--echo # connection default.
connection default;
--echo # Wait for the helper thread to sit on its lock.
while (`SELECT COUNT(*) < 1 FROM INFORMATION_SCHEMA.PROCESSLIST
WHERE STATE LIKE '%lock%'`)
{
#SELECT STATE, INFO FROM INFORMATION_SCHEMA.PROCESSLIST;
--sleep 0.1
}
--echo # connection conn2.
connection conn2;
--echo # Take a non-transactional READ lock,
--echo # which goes before the LOW_PRIORITY WRITE lock.
LOCK TABLE t1 READ;
--echo # The READ lock could be taken immediately.
--echo # Select from the table.
SELECT * FROM t1;
--echo # Unlock table.
UNLOCK TABLES;
SET AUTOCOMMIT= 0;
--echo # connection default.
connection default;
--echo # Unlock this connections non-transactional lock.
UNLOCK TABLES;
--echo # connection conn1.
connection conn1;
--echo # Now the LOW_PRIORITY WRITE lock is taken.
reap;
--echo # Insert a value.
INSERT INTO t1 VALUES(1111);
--echo # Unlock table.
UNLOCK TABLES;
SET AUTOCOMMIT= 0;
--echo # connection default.
connection default;
DELETE FROM t1;
SET AUTOCOMMIT= 0;
COMMIT;
--echo #
--echo # Normal WRITE locks go before readers (transaction).
--echo # Insert a value.
INSERT INTO t1 VALUES(111);
COMMIT;
--echo # Take a non-transactional lock.
LOCK TABLE t1 READ;
--echo # connection conn1.
connection conn1;
--echo # Take a non-transactional WRITE lock,
--echo # which waits in background until first read lock is released.
send LOCK TABLE t1 WRITE;
--echo # connection default.
connection default;
--echo # Wait for the helper thread to sit on its lock.
while (`SELECT COUNT(*) < 1 FROM INFORMATION_SCHEMA.PROCESSLIST
WHERE STATE LIKE '%lock%'`)
{
#SELECT STATE, INFO FROM INFORMATION_SCHEMA.PROCESSLIST;
--sleep 0.1
}
--echo # connection conn2.
connection conn2;
--echo # Take a non-transactional READ lock,
--echo # which waits in background until the WRITE lock is released.
send LOCK TABLE t1 READ;
--echo # connection default.
connection default;
--echo # Wait for the helper threads to sit on their locks.
while (`SELECT COUNT(*) < 2 FROM INFORMATION_SCHEMA.PROCESSLIST
WHERE STATE LIKE '%lock%'`)
{
#SELECT STATE, INFO FROM INFORMATION_SCHEMA.PROCESSLIST;
--sleep 0.1
}
--echo # Unlock this connections non-transactional lock.
UNLOCK TABLES;
--echo # connection conn1.
connection conn1;
--echo # Now the WRITE lock is taken.
reap;
--echo # Insert a value.
INSERT INTO t1 VALUES(1111);
--echo # Unlock table.
UNLOCK TABLES;
--echo # connection conn2.
connection conn2;
--echo # Now the READ lock is taken.
reap;
--echo # Select from the table.
SELECT * FROM t1;
--echo # Unlock table.
UNLOCK TABLES;
--echo # connection default.
connection default;
DELETE FROM t1;
COMMIT;
--echo #
--echo # LOW_PRIORITY WRITE behaves like WRITE in transaction mode.
--echo # Insert a value.
INSERT INTO t1 VALUES(111);
COMMIT;
--echo # Take a non-transactional lock.
LOCK TABLE t1 READ;
--echo # connection conn1.
connection conn1;
--echo # Take a non-transactional LOW_PRIORITY WRITE lock,
--echo # which waits in background until first read lock is released.
send LOCK TABLE t1 LOW_PRIORITY WRITE;
--echo # connection default.
connection default;
--echo # Wait for the helper thread to sit on its lock.
while (`SELECT COUNT(*) < 1 FROM INFORMATION_SCHEMA.PROCESSLIST
WHERE STATE LIKE '%lock%'`)
{
#SELECT STATE, INFO FROM INFORMATION_SCHEMA.PROCESSLIST;
--sleep 0.1
}
--echo # connection conn2.
connection conn2;
--echo # Take a non-transactional READ lock,
--echo # which waits in background for the LOW_PRIORITY WRITE lock.
send LOCK TABLE t1 READ;
--echo # connection default.
connection default;
--echo # Wait for the helper threads to sit on their locks.
while (`SELECT COUNT(*) < 2 FROM INFORMATION_SCHEMA.PROCESSLIST
WHERE STATE LIKE '%lock%'`)
{
#SELECT STATE, INFO FROM INFORMATION_SCHEMA.PROCESSLIST;
--sleep 0.1
}
--echo # Unlock this connections non-transactional lock.
UNLOCK TABLES;
--echo # connection conn1.
connection conn1;
--echo # Now the LOW_PRIORITY WRITE lock is taken.
reap;
--echo # Insert a value.
INSERT INTO t1 VALUES(1111);
--echo # Unlock table.
UNLOCK TABLES;
--echo # connection conn2.
connection conn2;
--echo # Now the READ lock is taken.
reap;
--echo # Select from the table.
SELECT * FROM t1;
--echo # Unlock table.
UNLOCK TABLES;
--echo # connection default.
connection default;
DELETE FROM t1;
COMMIT;
#
if ($nowait_support)
{
--echo #
--echo # NOWAIT.
--echo # Take an exclusive lock.
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # connection conn1.
connection conn1;
--echo # Try an exclusive lock,
--echo # which conflicts and cannot immediately be taken.
--error ER_LOCK_WAIT_TIMEOUT
LOCK TABLE t1 IN SHARE MODE NOWAIT;
--echo # connection default.
connection default;
--echo # Commit.
COMMIT;
}
#
--echo #
--echo # Transactional table locks do not interfere with the global read lock.
--echo # Take an exclusive lock on t1.
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # connection conn1.
connection conn1;
--echo # Try an exclusive lock, which conflicts.
--error ER_LOCK_WAIT_TIMEOUT
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # Can take the global read lock when an exclusive lock exist.
FLUSH TABLES WITH READ LOCK;
--echo # Show that the global read lock exists.
--error ER_CANT_UPDATE_WITH_READLOCK
LOCK TABLE t2 WRITE;
--echo # connection default.
connection default;
--echo # Can take an exclusive lock when the global read lock exists.
--echo # Take an exclusive lock on t2.
LOCK TABLE t2 IN EXCLUSIVE MODE;
--echo # connection conn1.
connection conn1;
--echo # Show that an exclusive lock on t1 exists.
--error ER_LOCK_WAIT_TIMEOUT
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # Show that an exclusive lock on t2 exists.
--error ER_LOCK_WAIT_TIMEOUT
LOCK TABLE t2 IN EXCLUSIVE MODE;
--echo # Show that the global read lock exists.
--error ER_CANT_UPDATE_WITH_READLOCK
LOCK TABLE t2 WRITE;
--echo # Release global read lock.
UNLOCK TABLES;
--echo # Commit.
COMMIT;
--echo # connection default.
connection default;
UNLOCK TABLES;
--echo # Commit.
COMMIT;
#
# Derived from a suggestion from Sergei:
--echo #
--echo # Access conflict on INSERT.
--echo # Take an share lock on t1.
LOCK TABLE t1 IN SHARE MODE;
--echo # connection conn1.
connection conn1;
SELECT GET_LOCK("mysqltest1", 10);
--echo # Try to insert a value,
--echo # which must wait in background for the lock to go away.
send INSERT INTO t1 VALUES (111);
--echo ## connection default.
connection default;
--echo ## Wait in background until the insert times out and releases lock.
send SELECT GET_LOCK("mysqltest1", 10);
--echo # connection conn1.
connection conn1;
--echo # Wait for INSERT to timeout.
--error ER_LOCK_WAIT_TIMEOUT
reap;
SELECT RELEASE_LOCK("mysqltest1");
COMMIT;
--echo # connection default.
connection default;
reap;
--echo # Commit.
COMMIT;
--echo # Show that the insert in conn1 failed.
SELECT * FROM t1;
#
# Derived from a suggestion from Sinisa (concurrent.inc):
--echo #
--echo # Access conflict on UPDATE with exclusive lock.
--echo # Insert a value.
INSERT INTO t1 VALUES (111);
COMMIT;
--echo # connection conn1.
connection conn1;
--echo # Take an exclusive lock.
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # connection default.
connection default;
--echo # Try a second exclusive lock, which fails due to the other lock.
--error ER_LOCK_WAIT_TIMEOUT
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # Try an update, which fails due to the exclusive lock.
--error ER_LOCK_WAIT_TIMEOUT
UPDATE t1 SET c1= 111222;
--echo # connection conn1.
connection conn1;
--echo # The exclusive table locker can still update.
UPDATE t1 SET c1= 111333;
--echo # connection default.
connection default;
--echo # Select is allowed despite the table lock, but sees old data.
SELECT * FROM t1;
--echo # connection conn1.
connection conn1;
--echo # Commit.
COMMIT;
--echo # connection default.
connection default;
--echo # It seems like the failed update began a transaction, so still old data.
SELECT * FROM t1;
--echo # Commit.
COMMIT;
--echo # Now select sees current data.
SELECT * FROM t1;
DELETE FROM t1;
COMMIT;
--echo #
--echo # Access conflict on UPDATE with share lock.
--echo # Insert a value.
INSERT INTO t1 VALUES (111);
COMMIT;
--echo # connection conn1.
connection conn1;
--echo # Take a share lock.
LOCK TABLE t1 IN SHARE MODE;
--echo # Update with a single share lock is possible.
UPDATE t1 SET c1= 111222;
--echo # Commit to get rid of the row lock.
COMMIT;
--echo # Take a share lock.
LOCK TABLE t1 IN SHARE MODE;
--echo # connection default.
connection default;
--echo # An exclusive lock is not possible on a share lock.
--error ER_LOCK_WAIT_TIMEOUT
LOCK TABLE t1 IN EXCLUSIVE MODE;
--echo # More share locks are possible.
LOCK TABLE t1 IN SHARE MODE;
--echo # Noone can update when multiple share locks exist.
--error ER_LOCK_WAIT_TIMEOUT
UPDATE t1 SET c1= 111333;
--echo # connection conn1.
connection conn1;
--echo # Noone can update when multiple share locks exist.
--error ER_LOCK_WAIT_TIMEOUT
UPDATE t1 SET c1= 111444;
COMMIT;
--echo # connection default.
connection default;
SELECT * FROM t1;
DELETE FROM t1;
COMMIT;
#
--echo #
--echo # LOCK TABLE is prohibited in stored procedure.
--error ER_SP_BADSTATEMENT
CREATE PROCEDURE lock_t1_excl()
LOCK TABLE t1 IN EXCLUSIVE MODE;
#
--echo #
--echo # LOCK TABLE is prohibited in trigger.
--error ER_SP_BADSTATEMENT
CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
LOCK TABLE t2 IN EXCLUSIVE MODE;
#
--echo ## Cleanup.
connection default;
SET AUTOCOMMIT= 1;
disconnect conn1;
UNLOCK TABLES;
DROP TABLE t1, t2;
#
--- New file ---
+++ mysql-test/r/locktrans_innodb.result 07/01/03 09:52:58
SET SESSION STORAGE_ENGINE = InnoDB;
DROP DATABASE IF EXISTS mysqltest;
DROP TABLE IF EXISTS t1, t2, t3, t4, v1;
DROP VIEW IF EXISTS t1, t2, t3, t4, v1;
DROP PROCEDURE IF EXISTS lock_t1_excl;
DROP PROCEDURE IF EXISTS lock_t4_excl;
#
# WL3561 - transactional LOCK TABLE - Syntax tests
# ================================================
CREATE TABLE t1 (c1 INT);
CREATE TABLE t2 (c2 INT);
CREATE TABLE t3 (c3 INT);
#
# Valid syntax for non-transactional locks.
LOCK TABLE t1 READ, t2 WRITE;
UNLOCK TABLES;
LOCK TABLE t1 READ LOCAL, t2 LOW_PRIORITY WRITE;
UNLOCK TABLES;
#
# Valid syntax for transactional locks.
LOCK TABLE t1 IN SHARE MODE, t2 IN EXCLUSIVE MODE;
UNLOCK TABLES;
#
# Valid syntax for transactional locks with NOWAIT option.
## In the preliminary reference implementation we expect these errors:
## NOWAIT+SHARE="timed out", NOWAIT+EXCLUSIVE="not supported".
## Statements abort on first error.
LOCK TABLE t1 IN SHARE MODE NOWAIT, t2 IN EXCLUSIVE MODE NOWAIT;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
LOCK TABLE t1 IN EXCLUSIVE MODE NOWAIT, t2 IN SHARE MODE NOWAIT;
ERROR 42000: Table 't1' uses an extension that doesn't exist in this MySQL version
LOCK TABLE t1 IN EXCLUSIVE MODE, t2 IN SHARE MODE NOWAIT;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
#
# Valid syntax for aliases with and without 'AS'.
LOCK TABLE t1 AS a1 READ, t2 a2 WRITE;
UNLOCK TABLES;
LOCK TABLE t1 AS a1 IN SHARE MODE, t2 a2 IN EXCLUSIVE MODE;
UNLOCK TABLES;
#
# Transactional locks taken on a view.
CREATE VIEW v1 AS SELECT * FROM t1, t2 WHERE t1.c1 = t2.c2;
LOCK TABLE v1 IN SHARE MODE;
LOCK TABLE v1 IN EXCLUSIVE MODE;
DROP VIEW v1;
#
# Locking INFORMATION_SCHEMA fails on missing privileges.
LOCK TABLE information_schema.tables IN SHARE MODE;
ERROR 42000: Access denied for user 'root'@'localhost' to database 'information_schema'
LOCK TABLE information_schema.tables IN EXCLUSIVE MODE;
ERROR 42000: Access denied for user 'root'@'localhost' to database 'information_schema'
LOCK TABLE information_schema.tables READ;
ERROR 42000: Access denied for user 'root'@'localhost' to database 'information_schema'
LOCK TABLE information_schema.tables WRITE;
ERROR 42000: Access denied for user 'root'@'localhost' to database 'information_schema'
#
# The new keywords EXCLUSIVE and NOWAIT are not reserved words.
CREATE TABLE t4 (exclusive INT, nowait INT);
DROP TABLE t4;
#
# Syntax errors for misspelled modes or left out symbols.
##-------------------------------------------------------
LOCK TABLE t1 IN SHARED MODE;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SHARED MODE' at line 1
LOCK TABLE t1 SHARE MODE;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MODE' at line 1
LOCK TABLE t1 IN SHARE;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
LOCK TABLE t1 IN MODE;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MODE' at line 1
LOCK TABLE t1 READ NOWAIT, t2 WRITE NOWAIT;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOWAIT, t2 WRITE NOWAIT' at line 1
LOCK TABLE t1 READ NOWAIT, t2 IN EXCLUSIVE MODE NOWAIT;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOWAIT, t2 IN EXCLUSIVE MODE NOWAIT' at line 1
LOCK TABLE t1 IN SHARE MODE NOWAIT, t2 WRITE NOWAIT;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOWAIT' at line 1
LOCK TABLE t1 IN SHARED MODE NOWAIT;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SHARED MODE NOWAIT' at line 1
LOCK TABLE t1 SHARE MODE NOWAIT;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MODE NOWAIT' at line 1
LOCK TABLE t1 IN SHARE NOWAIT;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOWAIT' at line 1
LOCK TABLE t1 IN MODE NOWAIT;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MODE NOWAIT' at line 1
##----------------------
## End of syntax errors.
#
#
# WL3561 - transactional LOCK TABLE - Lock method conversion
# ==========================================================
#
# Implicit lock method conversion due to mix in statement.
LOCK TABLE t1 READ, t2 IN EXCLUSIVE MODE;
Warnings:
Warning 1581 Converted to non-transactional lock on 't2'
UNLOCK TABLES;
# Lock t1 share (converted to read), t2 write.
LOCK TABLE t1 IN SHARE MODE, t2 WRITE;
Warnings:
Warning 1581 Converted to non-transactional lock on 't1'
# Show t1 is read locked, t2 write locked.
INSERT INTO t1 SELECT * FROM t2;
ERROR HY000: Table 't1' was locked with a READ lock and can't be updated
INSERT INTO t2 SELECT * FROM t1;
#
# Implicit lock method conversion due to existing non-transact. locks.
# Implicitly unlock existing non-transactional locks and take new ones.
# Lock t1 exclusive (converted to write), t2 share (converted to read).
LOCK TABLE t1 IN EXCLUSIVE MODE, t2 IN SHARE MODE;
Warnings:
Warning 1581 Converted to non-transactional lock on 't1'
Warning 1581 Converted to non-transactional lock on 't2'
# Show t1 is write locked, t2 read locked.
INSERT INTO t1 SELECT * FROM t2;
INSERT INTO t2 SELECT * FROM t1;
ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
UNLOCK TABLES;
#
# Reject lock method conversion in strict mode.
# Set strict mode.
SET @wl3561_save_sql_mode= @@SQL_MODE;
SET @@SQL_MODE= 'STRICT_ALL_TABLES';
# Try mixed mode locks.
LOCK TABLE t1 READ, t2 IN EXCLUSIVE MODE;
ERROR HY000: Cannot convert to non-transactional lock in strict mode on 't2'
LOCK TABLE t1 IN SHARE MODE, t2 WRITE;
ERROR HY000: Cannot convert to non-transactional lock in strict mode on 't1'
# Lock non-transactional.
LOCK TABLE t1 READ, t2 WRITE;
# Try transactional locks on top of the existing non-transactional locks.
LOCK TABLE t1 IN SHARE MODE, t2 IN EXCLUSIVE MODE;
ERROR HY000: Cannot convert to non-transactional lock in strict mode on 't1'
## Error is reported on first table only. Show both errors:
SHOW WARNINGS;
Level Code Message
Error 1582 Cannot convert to non-transactional lock in strict mode on 't1'
Error 1582 Cannot convert to non-transactional lock in strict mode on 't2'
UNLOCK TABLES;
SET @@SQL_MODE= @wl3561_save_sql_mode;
#
# Reject lock method conversion in an active transaction.
# Start transaction.
START TRANSACTION;
# Try mixed mode locks.
LOCK TABLE t1 READ, t2 IN EXCLUSIVE MODE;
ERROR HY000: Cannot convert to non-transactional lock in an active transaction on 't2'
LOCK TABLE t1 IN SHARE MODE, t2 WRITE;
ERROR HY000: Cannot convert to non-transactional lock in an active transaction on 't1'
COMMIT;
#
# Implicit lock method conversion for non-transactional storage engine.
# Create a non-transactional table.
CREATE TABLE t4 (c4 INT) ENGINE= MyISAM;
# Request a transactional lock, which is converted to non-transactional.
LOCK TABLE t4 IN SHARE MODE;
Warnings:
Warning 1581 Converted to non-transactional lock on 't4'
# Try a conflict with the existing non-transactional lock.
INSERT INTO t4 VALUES(444);
ERROR HY000: Table 't4' was locked with a READ lock and can't be updated
UNLOCK TABLES;
# Set strict mode.
SET @@SQL_MODE= 'STRICT_ALL_TABLES';
# Try a transactional lock, which would need a conversion.
LOCK TABLE t4 IN SHARE MODE;
ERROR HY000: Cannot convert to non-transactional lock in strict mode on 't4'
SET @@SQL_MODE= @wl3561_save_sql_mode;
#
# View with transactional and non-transactional storage engine.
CREATE VIEW v1 AS SELECT * FROM t3, t4 WHERE t3.c3 = t4.c4;
# Request a share lock on the view, which is converted to read locks.
LOCK TABLE v1 IN SHARE MODE;
Warnings:
Warning 1581 Converted to non-transactional lock on 't3'
Warning 1581 Converted to non-transactional lock on 't4'
# Show that read locks on the base tables prohibit writing ...
INSERT INTO t3 SELECT * FROM t4;
ERROR HY000: Table 't3' was locked with a READ lock and can't be updated
INSERT INTO t4 SELECT * FROM t3;
ERROR HY000: Table 't4' was locked with a READ lock and can't be updated
# ... but allow reading.
SELECT COUNT(*) FROM t3, t4 WHERE t3.c3 = t4.c4;
COUNT(*)
0
SELECT COUNT(*) FROM v1;
COUNT(*)
0
## Report conversion on view due to existing non-transactional locks.
LOCK TABLE v1 IN EXCLUSIVE MODE;
Warnings:
Warning 1581 Converted to non-transactional lock on 'v1'
INSERT INTO t3 VALUES(333);
INSERT INTO t4 VALUES(444);
INSERT INTO t1 VALUES(111);
ERROR HY000: Table 't1' was not locked with LOCK TABLES
UNLOCK TABLES;
## Now report conversion on base table again.
LOCK TABLE v1 IN EXCLUSIVE MODE;
Warnings:
Warning 1581 Converted to non-transactional lock on 't3'
Warning 1581 Converted to non-transactional lock on 't4'
INSERT INTO t3 VALUES(333);
INSERT INTO t4 VALUES(444);
INSERT INTO t1 VALUES(111);
ERROR HY000: Table 't1' was not locked with LOCK TABLES
UNLOCK TABLES;
DROP VIEW v1;
DELETE FROM t4;
#
# Insufficient privileges do not unlock tables nor end transactions.
# Prepare database, tables and an user with insufficient privileges.
# Make a new connection with this user.
CREATE DATABASE mysqltest;
CREATE TABLE mysqltest.t5 (c5 INT);
CREATE TABLE mysqltest.t6 (c6 INT);
CREATE USER mysqltest_1@localhost;
GRANT SELECT, INSERT ON mysqltest.* TO mysqltest_1@localhost;
# connection conn1.
# Show sufficient privileges to lock tables in the test database.
LOCK TABLE t1 READ, t2 WRITE;
# Show insufficient privileges in the mysqltest database.
LOCK TABLE mysqltest.t5 READ, mysqltest.t6 WRITE;
ERROR 42000: Access denied for user 'mysqltest_1'@'localhost' to database 'mysqltest'
# Show that the locks in 'test' still exist.
INSERT INTO t1 SELECT * FROM t2;
ERROR HY000: Table 't1' was locked with a READ lock and can't be updated
INSERT INTO t2 SELECT * FROM t1;
# Unlock tables.
UNLOCK TABLES;
# Start transaction.
START TRANSACTION;
# Insert a value.
INSERT INTO t1 VALUES(111);
# Try a lock that fails on privileges.
LOCK TABLE mysqltest.t5 READ;
ERROR 42000: Access denied for user 'mysqltest_1'@'localhost' to database 'mysqltest'
# Rollback transaction.
ROLLBACK;
# Show that the inserted value has gone.
SELECT * FROM t1;
c1
# Drop the connection with the unprivileged user.
# connection default.
#
# Sufficient privileges do unlock tables and end transactions.
# Grant sufficient privileges to the user.
# Make a new connection with this user.
GRANT SELECT, INSERT, LOCK TABLES ON mysqltest.* TO mysqltest_1@localhost;
# connection conn1.
# Lock tables in the test database.
LOCK TABLE t1 READ, t2 WRITE;
# Lock tables in the mysqltest database.
LOCK TABLE mysqltest.t5 READ, mysqltest.t6 WRITE;
# Show that the locks in 'test' have been replaced ...
INSERT INTO t1 SELECT * FROM t2;
ERROR HY000: Table 't1' was not locked with LOCK TABLES
INSERT INTO t2 SELECT * FROM t1;
ERROR HY000: Table 't2' was not locked with LOCK TABLES
# ... by the locks in 'mysqltest'.
INSERT INTO mysqltest.t5 SELECT * FROM mysqltest.t6;
ERROR HY000: Table 't5' was locked with a READ lock and can't be updated
INSERT INTO mysqltest.t6 SELECT * FROM mysqltest.t5;
# Unlock tables.
UNLOCK TABLES;
# Start transaction.
START TRANSACTION;
# Insert a value.
INSERT INTO t1 VALUES(111);
# Take a new lock.
LOCK TABLE mysqltest.t5 READ;
# Rollback transaction.
ROLLBACK;
UNLOCK TABLES;
# Show that the inserted value had been committed.
SELECT * FROM t1;
c1
111
DELETE FROM t1;
# connection default.
UNLOCK TABLES;
DROP USER mysqltest_1@localhost;
DROP DATABASE mysqltest;
DROP TABLE t1, t2, t3, t4;
#
# WL3594 - transactional LOCK TABLE Testing - Functional tests
# ============================================================
# Prepare tables and connections.
# Set AUTOCOMMIT= 0 in each connection.
CREATE TABLE t1 (c1 INT);
CREATE TABLE t2 (c2 INT);
# connection conn1.
SET AUTOCOMMIT= 0;
# connection conn2.
SET AUTOCOMMIT= 0;
# connection default.
SET AUTOCOMMIT= 0;
#
# Transactional lock behaviour:
# LOCK TABLE does _not_ commit a transaction.
# Insert a value.
INSERT INTO t1 VALUES (111);
# Lock transactional.
LOCK TABLE t1 IN EXCLUSIVE MODE;
# Rollback.
ROLLBACK;
# Show that the inserted value has gone.
SELECT * FROM t1;
c1
#
# After LOCK TABLE one can access tables not mentioned in LOCK TABLE.
# Lock t1 transactional.
LOCK TABLE t1 IN EXCLUSIVE MODE;
# Insert a value into t2.
INSERT INTO t2 VALUES (222);
# Show that the inserted value is indeed in the table.
SELECT * FROM t2;
c2
222
#
# One can issue LOCK TABLE many times, adding more tables.
# Lock t2 transactional.
LOCK TABLE t2 IN EXCLUSIVE MODE;
#
# LOCK TABLE does not rollback a transaction.
# Show that the inserted value is still in the table.
SELECT * FROM t2;
c2
222
# Rollback transaction.
ROLLBACK;
# Show that the inserted value has gone.
SELECT * FROM t2;
c2
#
# Tables are unlocked at the end of transaction (commit).
# Take an exclusive lock.
LOCK TABLE t1 IN EXCLUSIVE MODE;
# Commit.
COMMIT;
# connection conn1.
# Take an exclusive lock.
# This would fail after timeout if t1 is still locked.
LOCK TABLE t1 IN EXCLUSIVE MODE;
# Commit.
COMMIT;
# connection default.
#
# Tables are unlocked at the end of transaction (rollback).
# Take an exclusive lock.
LOCK TABLE t1 IN EXCLUSIVE MODE;
# Rollback.
ROLLBACK;
# connection conn1.
# Take an exclusive lock.
# This would fail after timeout if t1 is still locked.
LOCK TABLE t1 IN EXCLUSIVE MODE;
# Rollback.
ROLLBACK;
# connection default.
#
# UNLOCK TABLES does not touch a transaction when
# no non-transactional table locks exist.
# Take a transactional lock.
LOCK TABLE t1 IN EXCLUSIVE MODE;
# Insert a value.
INSERT INTO t1 VALUES(111);
# Unlock (non-transactional) table locks.
UNLOCK TABLES;
# Show that the inserted value is still in the table.
SELECT * FROM t1;
c1
111
# Rollback.
ROLLBACK;
# Show that the inserted value has gone.
SELECT * FROM t1;
c1
#
# UNLOCK TABLES commits a transaction when
# non-transactional table locks exist.
# Take a non-transactional lock.
LOCK TABLE t1 WRITE;
# Insert a value.
INSERT INTO t1 VALUES(111);
# Unlock (non-transactional) table locks.
UNLOCK TABLES;
# Show that the inserted value is still in the table.
SELECT * FROM t1;
c1
111
# Rollback.
ROLLBACK;
# Show that the inserted value is still in the table.
SELECT * FROM t1;
c1
111
DELETE FROM t1;
#
# START TRANSACTION removes a previous lock.
# Take an exclusive lock.
LOCK TABLE t1 IN EXCLUSIVE MODE;
# Start transaction.
START TRANSACTION;
# connection conn1.
# Take an exclusive lock.
# This would fail after timeout if t1 is still locked.
LOCK TABLE t1 IN EXCLUSIVE MODE;
COMMIT;
# connection default.
COMMIT;
#
# With Auto commit on, transactional locks will be ignored
# Set AUTOCOMMIT= 1.
SET AUTOCOMMIT= 1;
# Take an exclusive lock.
LOCK TABLE t1 IN EXCLUSIVE MODE;
# connection conn1.
# Set AUTOCOMMIT= 1.
SET AUTOCOMMIT= 1;
# Take an exclusive lock.
# This would fail after timeout if t1 is still locked.
LOCK TABLE t1 IN SHARE MODE;
SET AUTOCOMMIT= 0;
COMMIT;
# connection default.
UNLOCK TABLES;
SET AUTOCOMMIT= 0;
COMMIT;
#
# With Auto commit on, transactional locks can time out.
# Default connection runs in transactional mode.
# Set AUTOCOMMIT= 0.
SET AUTOCOMMIT= 0;
# Take an exclusive lock, which persists.
LOCK TABLE t1 IN EXCLUSIVE MODE;
# connection conn1.
# conn1 runs in autocommit mode.
# Set AUTOCOMMIT= 1.
SET AUTOCOMMIT= 1;
# Try an exclusive lock,
# which times out though running in autocommit mode.
LOCK TABLE t1 IN SHARE MODE;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
SET AUTOCOMMIT= 0;
COMMIT;
# connection default.
UNLOCK TABLES;
SET AUTOCOMMIT= 0;
COMMIT;
#
# Normal WRITE locks go before readers (autocommit).
# Set AUTOCOMMIT= 1.
SET AUTOCOMMIT= 1;
# Insert a value.
INSERT INTO t1 VALUES(111);
# Take a non-transactional lock.
LOCK TABLE t1 READ;
# connection conn1.
# Set AUTOCOMMIT= 1.
SET AUTOCOMMIT= 1;
# Take a non-transactional WRITE lock,
# which waits in background until first read lock is released.
LOCK TABLE t1 WRITE;
# connection default.
# Wait for the helper thread to sit on its lock.
# connection conn2.
# Set AUTOCOMMIT= 1.
SET AUTOCOMMIT= 1;
# Take a non-transactional READ lock,
# which waits in background until the WRITE lock is released.
LOCK TABLE t1 READ;
# connection default.
# Wait for the helper threads to sit on their locks.
# Unlock this connections non-transactional lock.
UNLOCK TABLES;
# connection conn1.
# Now the WRITE lock is taken.
# Insert a value.
INSERT INTO t1 VALUES(1111);
# Unlock table.
UNLOCK TABLES;
# connection conn2.
# Now the READ lock is taken.
# Select from the table.
SELECT * FROM t1;
c1
111
1111
# Unlock table.
UNLOCK TABLES;
# connection default.
DELETE FROM t1;
#
# LOW_PRIORITY WRITE locks wait for readers (autocommit).
# Insert a value.
INSERT INTO t1 VALUES(111);
# Take a non-transactional lock.
LOCK TABLE t1 READ;
# connection conn1.
# Take a non-transactional LOW_PRIORITY WRITE lock,
# which waits in background until all read locks are released.
LOCK TABLE t1 LOW_PRIORITY WRITE;
# connection default.
# Wait for the helper thread to sit on its lock.
# connection conn2.
# Take a non-transactional READ lock,
# which goes before the LOW_PRIORITY WRITE lock.
LOCK TABLE t1 READ;
# The READ lock could be taken immediately.
# Select from the table.
SELECT * FROM t1;
c1
111
# Unlock table.
UNLOCK TABLES;
SET AUTOCOMMIT= 0;
# connection default.
# Unlock this connections non-transactional lock.
UNLOCK TABLES;
# connection conn1.
# Now the LOW_PRIORITY WRITE lock is taken.
# Insert a value.
INSERT INTO t1 VALUES(1111);
# Unlock table.
UNLOCK TABLES;
SET AUTOCOMMIT= 0;
# connection default.
DELETE FROM t1;
SET AUTOCOMMIT= 0;
COMMIT;
#
# Normal WRITE locks go before readers (transaction).
# Insert a value.
INSERT INTO t1 VALUES(111);
COMMIT;
# Take a non-transactional lock.
LOCK TABLE t1 READ;
# connection conn1.
# Take a non-transactional WRITE lock,
# which waits in background until first read lock is released.
LOCK TABLE t1 WRITE;
# connection default.
# Wait for the helper thread to sit on its lock.
# connection conn2.
# Take a non-transactional READ lock,
# which waits in background until the WRITE lock is released.
LOCK TABLE t1 READ;
# connection default.
# Wait for the helper threads to sit on their locks.
# Unlock this connections non-transactional lock.
UNLOCK TABLES;
# connection conn1.
# Now the WRITE lock is taken.
# Insert a value.
INSERT INTO t1 VALUES(1111);
# Unlock table.
UNLOCK TABLES;
# connection conn2.
# Now the READ lock is taken.
# Select from the table.
SELECT * FROM t1;
c1
111
1111
# Unlock table.
UNLOCK TABLES;
# connection default.
DELETE FROM t1;
COMMIT;
#
# LOW_PRIORITY WRITE behaves like WRITE in transaction mode.
# Insert a value.
INSERT INTO t1 VALUES(111);
COMMIT;
# Take a non-transactional lock.
LOCK TABLE t1 READ;
# connection conn1.
# Take a non-transactional LOW_PRIORITY WRITE lock,
# which waits in background until first read lock is released.
LOCK TABLE t1 LOW_PRIORITY WRITE;
# connection default.
# Wait for the helper thread to sit on its lock.
# connection conn2.
# Take a non-transactional READ lock,
# which waits in background for the LOW_PRIORITY WRITE lock.
LOCK TABLE t1 READ;
# connection default.
# Wait for the helper threads to sit on their locks.
# Unlock this connections non-transactional lock.
UNLOCK TABLES;
# connection conn1.
# Now the LOW_PRIORITY WRITE lock is taken.
# Insert a value.
INSERT INTO t1 VALUES(1111);
# Unlock table.
UNLOCK TABLES;
# connection conn2.
# Now the READ lock is taken.
# Select from the table.
SELECT * FROM t1;
c1
111
1111
# Unlock table.
UNLOCK TABLES;
# connection default.
DELETE FROM t1;
COMMIT;
#
# NOWAIT.
# Take an exclusive lock.
LOCK TABLE t1 IN EXCLUSIVE MODE;
# connection conn1.
# Try an exclusive lock,
# which conflicts and cannot immediately be taken.
LOCK TABLE t1 IN SHARE MODE NOWAIT;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
# connection default.
# Commit.
COMMIT;
#
# Transactional table locks do not interfere with the global read lock.
# Take an exclusive lock on t1.
LOCK TABLE t1 IN EXCLUSIVE MODE;
# connection conn1.
# Try an exclusive lock, which conflicts.
LOCK TABLE t1 IN EXCLUSIVE MODE;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
# Can take the global read lock when an exclusive lock exist.
FLUSH TABLES WITH READ LOCK;
# Show that the global read lock exists.
LOCK TABLE t2 WRITE;
ERROR HY000: Can't execute the query because you have a conflicting read lock
# connection default.
# Can take an exclusive lock when the global read lock exists.
# Take an exclusive lock on t2.
LOCK TABLE t2 IN EXCLUSIVE MODE;
# connection conn1.
# Show that an exclusive lock on t1 exists.
LOCK TABLE t1 IN EXCLUSIVE MODE;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
# Show that an exclusive lock on t2 exists.
LOCK TABLE t2 IN EXCLUSIVE MODE;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
# Show that the global read lock exists.
LOCK TABLE t2 WRITE;
ERROR HY000: Can't execute the query because you have a conflicting read lock
# Release global read lock.
UNLOCK TABLES;
# Commit.
COMMIT;
# connection default.
UNLOCK TABLES;
# Commit.
COMMIT;
#
# Access conflict on INSERT.
# Take an share lock on t1.
LOCK TABLE t1 IN SHARE MODE;
# connection conn1.
SELECT GET_LOCK("mysqltest1", 10);
GET_LOCK("mysqltest1", 10)
1
# Try to insert a value,
# which must wait in background for the lock to go away.
INSERT INTO t1 VALUES (111);
## connection default.
## Wait in background until the insert times out and releases lock.
SELECT GET_LOCK("mysqltest1", 10);
# connection conn1.
# Wait for INSERT to timeout.
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
SELECT RELEASE_LOCK("mysqltest1");
RELEASE_LOCK("mysqltest1")
1
COMMIT;
# connection default.
GET_LOCK("mysqltest1", 10)
1
# Commit.
COMMIT;
# Show that the insert in conn1 failed.
SELECT * FROM t1;
c1
#
# Access conflict on UPDATE with exclusive lock.
# Insert a value.
INSERT INTO t1 VALUES (111);
COMMIT;
# connection conn1.
# Take an exclusive lock.
LOCK TABLE t1 IN EXCLUSIVE MODE;
# connection default.
# Try a second exclusive lock, which fails due to the other lock.
LOCK TABLE t1 IN EXCLUSIVE MODE;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
# Try an update, which fails due to the exclusive lock.
UPDATE t1 SET c1= 111222;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
# connection conn1.
# The exclusive table locker can still update.
UPDATE t1 SET c1= 111333;
# connection default.
# Select is allowed despite the table lock, but sees old data.
SELECT * FROM t1;
c1
111
# connection conn1.
# Commit.
COMMIT;
# connection default.
# It seems like the failed update began a transaction, so still old data.
SELECT * FROM t1;
c1
111
# Commit.
COMMIT;
# Now select sees current data.
SELECT * FROM t1;
c1
111333
DELETE FROM t1;
COMMIT;
#
# Access conflict on UPDATE with share lock.
# Insert a value.
INSERT INTO t1 VALUES (111);
COMMIT;
# connection conn1.
# Take a share lock.
LOCK TABLE t1 IN SHARE MODE;
# Update with a single share lock is possible.
UPDATE t1 SET c1= 111222;
# Commit to get rid of the row lock.
COMMIT;
# Take a share lock.
LOCK TABLE t1 IN SHARE MODE;
# connection default.
# An exclusive lock is not possible on a share lock.
LOCK TABLE t1 IN EXCLUSIVE MODE;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
# More share locks are possible.
LOCK TABLE t1 IN SHARE MODE;
# Noone can update when multiple share locks exist.
UPDATE t1 SET c1= 111333;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
# connection conn1.
# Noone can update when multiple share locks exist.
UPDATE t1 SET c1= 111444;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
COMMIT;
# connection default.
SELECT * FROM t1;
c1
111222
DELETE FROM t1;
COMMIT;
#
# LOCK TABLE is prohibited in stored procedure.
CREATE PROCEDURE lock_t1_excl()
LOCK TABLE t1 IN EXCLUSIVE MODE;
ERROR 0A000: LOCK is not allowed in stored procedures
#
# LOCK TABLE is prohibited in trigger.
CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
LOCK TABLE t2 IN EXCLUSIVE MODE;
ERROR 0A000: LOCK is not allowed in stored procedures
## Cleanup.
SET AUTOCOMMIT= 1;
UNLOCK TABLES;
DROP TABLE t1, t2;
--- New file ---
+++ mysql-test/t/locktrans_innodb-master.opt 07/01/03 09:52:58
--innodb_lock_wait_timeout=1
--- New file ---
+++ mysql-test/t/locktrans_innodb.test 07/01/03 09:52:58
# t/locktrans_innodb.test
#
# Transactional LOCK TABLE tests
# Transactional lock tests require multiple sessions.
--source include/not_embedded.inc
--source include/have_innodb.inc
let $engine_type= InnoDB;
# Storage engine supports the NOWAIT option.
let $nowait_support= 1;
# Define a storage engine that does not support transactional locking.
let $other_non_trans_engine_type= MyISAM;
--source include/locktrans.inc
--- 1.131/storage/innobase/handler/ha_innodb.h 2007-01-03 09:53:11 +01:00
+++ 1.132/storage/innobase/handler/ha_innodb.h 2007-01-03 09:53:11 +01:00
@@ -149,6 +149,22 @@ class ha_innobase: public handler
int discard_or_import_tablespace(my_bool discard);
int extra(enum ha_extra_function operation);
int reset();
+ int lock_table(THD *thd, int lock_type, int lock_timeout)
+ {
+ /*
+ Preliminarily call the pre-existing internal method for
+ transactional locking and ignore non-transactional locks.
+ */
+ if (!lock_timeout)
+ {
+ /* Preliminarily show both possible errors for NOWAIT. */
+ if (lock_type == F_WRLCK)
+ return HA_ERR_UNSUPPORTED;
+ else
+ return HA_ERR_LOCK_WAIT_TIMEOUT;
+ }
+ return transactional_table_lock(thd, lock_type);
+ }
int external_lock(THD *thd, int lock_type);
int transactional_table_lock(THD *thd, int lock_type);
int start_stmt(THD *thd, thr_lock_type lock_type);
| Thread |
|---|
| • bk commit into 5.1 tree (istruewing:1.2367) | ingo | 3 Jan |