List:Internals« Previous MessageNext Message »
From:konstantin Date:July 8 2005 8:35pm
Subject:bk commit into 5.0 tree (konstantin:1.1890) BUG#10760
View as plain text  
Below is the list of changes that have just been committed into a local
5.0 repository of kostja. When kostja 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
  1.1890 05/07/08 22:34:51 konstantin@stripped +28 -0
  A fix and a test case for Bug#10760 and complementary cleanups.
  (see comments to the changed files).

  tests/mysql_client_test.c
    1.131 05/07/08 22:34:44 konstantin@stripped +131 -23
    A test case for Bug#10760 and complementary issues: test a simple
    deadlock case too.

  sql/sql_select.h
    1.92 05/07/08 22:34:44 konstantin@stripped +3 -1
    - add an own lock_id and commit/rollback status flag to class Cursor

  sql/sql_select.cc
    1.332 05/07/08 22:34:43 konstantin@stripped +16 -7
    - extend class Cursor to support specific at-COMMIT/ROLLBACK behavior.
    If a cursor uses tables of a storage engine that 
    invalidates all open tables at COMMIT/ROLLBACK, it must be closed
    before COMMIT/ROLLBACK is executed.

  sql/sql_prepare.cc
    1.135 05/07/08 22:34:43 konstantin@stripped +39 -21
    - implement Prepared_statement::close_cursor, to close transient
      cursors at COMMIT/ROLLBACK. 
    - implicitly close an open cursor in mysql_stmt_execute instead of 
      issuing an error (to reduce the need to explicitly close cursors
      and save network bandwidth).

  sql/sql_insert.cc
    1.167 05/07/08 22:34:43 konstantin@stripped +1 -1
    - don't check for ETIME (see comment for item_func.cc)

  sql/sql_class.h
    1.247 05/07/08 22:34:43 konstantin@stripped +18 -3
    - add support for Cursors registry to Statement map. 

  sql/sql_class.cc
    1.193 05/07/08 22:34:43 konstantin@stripped +17 -0
    - implement Statement_map::close_transient_cursors

  sql/slave.cc
    1.249 05/07/08 22:34:43 konstantin@stripped +1 -1
    - don't check for ETIME (see comment for item_func.cc)

  sql/set_var.cc
    1.124 05/07/08 22:34:43 konstantin@stripped +4 -0
    - add new global variable 'table_lock_wait_timeout' to specify
    a wait timeout for table-level locks of MySQL (in seconds). The default
    timeout is 50 seconds.

  sql/mysqld.cc
    1.479 05/07/08 22:34:43 konstantin@stripped +8 -1
    - add server option --table_lock_wait_timeout (in seconds)

  sql/lock.cc
    1.69 05/07/08 22:34:43 konstantin@stripped +16 -3
    - extend mysql_lock_tables to send error to the client if 
      thr_multi_lock returns a timeout or a deadlock error.

  sql/item_func.cc
    1.228 05/07/08 22:34:43 konstantin@stripped +5 -5
    Get rid of checking for ETIME return value of pthread_cond_timedwait.
    ETIME was returned by cond_timedwait (sic, the pre-POSIX1001b function) on 
    Solaris 2.6 and 2.7. pthread_cond_timedwait on Solaris returns ETIMEDOUT.
    The stadnard requirement is that the only additional return value
    of pthred_cond_timedwait compared to pthread_cond_wait is ETIMEDOUT.
    Let us not bloat the application code with redundant checks,
    and if we're ever to work on a platform that returns a non-standard 
    value, we should write a wrapper for that platform (like we do, e.g., for
    Windows).

  sql/handler.h
    1.145 05/07/08 22:34:43 konstantin@stripped +4 -1
    - extend class handlerton with cursor info
    - add handlerton *ht to every handler instance.

  sql/handler.cc
    1.179 05/07/08 22:34:43 konstantin@stripped +7 -7
    - drop support for ISAM storage engine, which was removed from 5.0
    - close all "transient" cursors at COMMIT/ROLLBACK. A "transient"
      SQL level cursor is a cursor that uses tables that have a transaction-
      specific state.

  sql/ha_ndbcluster.cc
    1.192 05/07/08 22:34:42 konstantin@stripped +8 -1
    - extend the handlerton of NDB storage engine with the info
    describing cursor behaviour at commit.
    - initialize handler::ht in ha_ndbcluster constructor.

  sql/ha_myisam.h
    1.68 05/07/08 22:34:42 konstantin@stripped +1 -7
    - move ha_myisam constructor to .cc (need to use a static variable in 
      it).

  sql/ha_myisam.cc
    1.154 05/07/08 22:34:42 konstantin@stripped +39 -0
    - add a handlerton for MyISAM (stubs only), init handler::ht
      from it in the constructor.

  sql/ha_innodb.h
    1.97 05/07/08 22:34:42 konstantin@stripped +1 -13
    - move ha_innobase constructor to .cc file (need to use a static
      variable in it).

  sql/ha_innodb.cc
    1.226 05/07/08 22:34:42 konstantin@stripped +28 -1
    - extend the handlerton of the InnoDB storage engine with the info
    describing cursor behaviour at COMMIT/ROLLBACK.
    - init handler::ht in ha_innobase constructor

  sql/ha_berkeley.h
    1.71 05/07/08 22:34:42 konstantin@stripped +1 -6
    - move constructor to .cc (need to use a static variable in it).

  sql/ha_berkeley.cc
    1.152 05/07/08 22:34:42 konstantin@stripped +15 -1
    - extend the handlerton of BDB storage engine with the info
    describing cursor behaviour at commit. 
    - init handler::ht in ha_berkeley constructor.

  sql/examples/ha_archive.cc
    1.42 05/07/08 22:34:42 konstantin@stripped +3 -2
    - extend the handlerton of Archive storage engine with the info
      describing cursor behaviour at commit.

  mysys/thr_lock.c
    1.43 05/07/08 22:34:42 konstantin@stripped +119 -50
    Better support for cursors:
    - use THR_LOCK_OWNER * as lock identifier, not pthread_t.
    - check and return an error for a trivial deadlock case, when an
      update statement is issued to a table locked by a cursor which has 
      been previously opened in the same connection.
    - add support for locking timeouts: with use of cursors, trivial 
      deadlocks can occur. For now the only remedy is the lock wait timeout,
      which is initialized from a new global variable 'table_lock_wait_timeout'
      Example of a deadlock (assuming the storage engine does not downgrade 
      locks):
      con1: open cursor for select * from t1;
      con2: open cursor for select * from t2;
      con1: update t2 set id=id*2;  -- blocked
      con2: update t1 set id=id*2;  -- deadlock
    - the check in the wait_for_lock loop has been changed from
      data->cond != cond to data->cond != 0. data->cond is zeroed
      in every place it's changed that I could find. 
      If there is a case that I missed, then it should be documented.
      All tests pass, although TLL is a part that is not tested
      well by the regression test suite.
    - added comments

  mysys/my_os2cond.c
    1.4 05/07/08 22:34:42 konstantin@stripped +1 -1
    - fix our implementation of pthread_cond_timedwait on OS2 to return
      ETIMEDOUT instead of ETIME.

  mysql-test/t/lock.test
    1.14 05/07/08 22:34:42 konstantin@stripped +18 -0
    - add test coverage for table_lock_wait_timeout system variable.

  mysql-test/r/lock.result
    1.17 05/07/08 22:34:42 konstantin@stripped +9 -0
    - test results fixed (a new test case for table_lock_wait_timeout)

  libmysql/libmysql.c
    1.221 05/07/08 22:34:41 konstantin@stripped +2 -3
    - reset_stmt_handle(): don't reset the server side just because we have 
      an open cursor: the server will close the cursor automatically if 
      needed (MarkM optimization request to avoid redundnat packet exchange).

  include/thr_lock.h
    1.15 05/07/08 22:34:41 konstantin@stripped +19 -4
    - add a notion of lock owner to table level locking. When using
      cursors, lock owner can not be identified by a thread id any more, 
      as we must protect cursors from updates issued within the same 
      connection (thread). So, each cursor has its own lock identifier to 
      use with table level locking.
    - extend return values of thr_lock and thr_multi_lock with
      THR_LOCK_TIMEOUT and THR_LOCK_DEADLOCK, since these conditions
      are now possible (see comments to thr_lock.c)

# 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:	konstantin
# Host:	dragonfly.local
# Root:	/opt/local/work/mysql-5.0-10760

--- 1.14/include/thr_lock.h	2005-05-13 13:08:02 +04:00
+++ 1.15/include/thr_lock.h	2005-07-08 22:34:41 +04:00
@@ -62,17 +62,28 @@
 		     /* Abort new lock request with an error */
 		     TL_WRITE_ONLY};
 
+enum enum_thr_lock_result { THR_LOCK_SUCCESS= 0, THR_LOCK_ABORTED= 1,
+                            THR_LOCK_WAIT_TIMEOUT= 2, THR_LOCK_DEADLOCK= 3 };
+
+
 extern ulong max_write_lock_count;
+extern ulong table_lock_wait_timeout;
 extern my_bool thr_lock_inited;
 extern enum thr_lock_type thr_upgraded_concurrent_insert_lock;
 
-typedef struct st_thr_lock_data {
+typedef struct st_thr_lock_owner
+{
   pthread_t thread;
+  ulong thread_id;
+} THR_LOCK_OWNER;
+
+
+typedef struct st_thr_lock_data {
+  THR_LOCK_OWNER *owner;
   struct st_thr_lock_data *next,**prev;
   struct st_thr_lock *lock;
   pthread_cond_t *cond;
   enum thr_lock_type type;
-  ulong thread_id;
   void *status_param;			/* Param to status functions */
   void *debug_print_param;
 } THR_LOCK_DATA;
@@ -102,13 +113,17 @@
 extern pthread_mutex_t THR_LOCK_lock;
 
 my_bool init_thr_lock(void);		/* Must be called once/thread */
+void thr_lock_owner_init(THR_LOCK_OWNER *owner);
 void thr_lock_init(THR_LOCK *lock);
 void thr_lock_delete(THR_LOCK *lock);
 void thr_lock_data_init(THR_LOCK *lock,THR_LOCK_DATA *data,
 			void *status_param);
-int thr_lock(THR_LOCK_DATA *data,enum thr_lock_type lock_type);
+enum enum_thr_lock_result thr_lock(THR_LOCK_DATA *data,
+                                   THR_LOCK_OWNER *owner,
+                                   enum thr_lock_type lock_type);
 void thr_unlock(THR_LOCK_DATA *data);
-int thr_multi_lock(THR_LOCK_DATA **data,uint count);
+enum enum_thr_lock_result thr_multi_lock(THR_LOCK_DATA **data,
+                                         uint count, THR_LOCK_OWNER *owner);
 void thr_multi_unlock(THR_LOCK_DATA **data,uint count);
 void thr_abort_locks(THR_LOCK *lock);
 void thr_abort_locks_for_thread(THR_LOCK *lock, pthread_t thread);

--- 1.220/libmysql/libmysql.c	2005-07-05 20:46:01 +04:00
+++ 1.221/libmysql/libmysql.c	2005-07-08 22:34:41 +04:00
@@ -4892,13 +4892,12 @@
   {
     MYSQL *mysql= stmt->mysql;
     MYSQL_DATA *result= &stmt->result;
-    my_bool has_cursor= stmt->read_row_func == stmt_read_row_from_cursor;
 
     /*
       Reset stored result set if so was requested or it's a part
       of cursor fetch.
     */
-    if (result->data && (has_cursor || (flags & RESET_STORE_RESULT)))
+    if (result->data && (flags & RESET_STORE_RESULT))
     {
       /* Result buffered */
       free_root(&result->alloc, MYF(MY_KEEP_PREALLOC));
@@ -4929,7 +4928,7 @@
           mysql->status= MYSQL_STATUS_READY;
         }
       }
-      if (has_cursor || (flags & RESET_SERVER_SIDE))
+      if (flags & RESET_SERVER_SIDE)
       {
         /*
           Reset the server side statement and close the server side

--- 1.42/mysys/thr_lock.c	2005-06-05 18:01:07 +04:00
+++ 1.43/mysys/thr_lock.c	2005-07-08 22:34:42 +04:00
@@ -84,6 +84,7 @@
 
 my_bool thr_lock_inited=0;
 ulong locks_immediate = 0L, locks_waited = 0L;
+ulong table_lock_wait_timeout;
 enum thr_lock_type thr_upgraded_concurrent_insert_lock = TL_WRITE;
 
 /* The following constants are only for debug output */
@@ -109,16 +110,23 @@
   return 0;
 }
 
+static inline my_bool
+thr_lock_owner_equal(THR_LOCK_OWNER *rhs, THR_LOCK_OWNER *lhs)
+{
+  return rhs == lhs;
+}
+
+
 #ifdef EXTRA_DEBUG
 #define MAX_FOUND_ERRORS	10		/* Report 10 first errors */
 static uint found_errors=0;
 
 static int check_lock(struct st_lock_list *list, const char* lock_type,
-		      const char *where, my_bool same_thread, bool no_cond)
+		      const char *where, my_bool same_owner, bool no_cond)
 {
   THR_LOCK_DATA *data,**prev;
   uint count=0;
-  pthread_t first_thread;
+  THR_LOCK_OWNER *first_owner;
   LINT_INIT(first_thread);
 
   prev= &list->data;
@@ -126,8 +134,8 @@
   {
     enum thr_lock_type last_lock_type=list->data->type;
 
-    if (same_thread && list->data)
-      first_thread=list->data->thread;
+    if (same_owner && list->data)
+      first_owner= list->data->owner;
     for (data=list->data; data && count++ < MAX_LOCKS ; data=data->next)
     {
       if (data->type != last_lock_type)
@@ -139,7 +147,8 @@
 		count, lock_type, where);
 	return 1;
       }
-      if (same_thread && ! pthread_equal(data->thread,first_thread) &&
+      if (same_owner &&
+          !thr_lock_owner_equal(data->owner, first_owner) &&
 	  last_lock_type != TL_WRITE_ALLOW_WRITE)
       {
 	fprintf(stderr,
@@ -255,8 +264,8 @@
 	}
 	if (lock->read.data)
 	{
-	  if (!pthread_equal(lock->write.data->thread,
-			     lock->read.data->thread) &&
+          if (!thr_lock_owner_equal(lock->write.data->owner,
+                                    lock->read.data->owner) &&
 	      ((lock->write.data->type > TL_WRITE_DELAYED &&
 		lock->write.data->type != TL_WRITE_ONLY) ||
 	       ((lock->write.data->type == TL_WRITE_CONCURRENT_INSERT ||
@@ -330,24 +339,31 @@
   DBUG_VOID_RETURN;
 }
 
+
+void thr_lock_owner_init(THR_LOCK_OWNER *owner)
+{
+  owner->thread= pthread_self();
+  owner->thread_id= my_thread_id();	/* for debugging */
+}
+
 	/* Initialize a lock instance */
 
 void thr_lock_data_init(THR_LOCK *lock,THR_LOCK_DATA *data, void *param)
 {
   data->lock=lock;
   data->type=TL_UNLOCK;
-  data->thread=pthread_self();
-  data->thread_id=my_thread_id();		/* for debugging */
+  data->owner= 0;                               /* no owner yet */
   data->status_param=param;
   data->cond=0;
 }
 
 
-static inline my_bool have_old_read_lock(THR_LOCK_DATA *data,pthread_t thread)
+static inline my_bool
+have_old_read_lock(THR_LOCK_DATA *data, THR_LOCK_OWNER *owner)
 {
   for ( ; data ; data=data->next)
   {
-    if ((pthread_equal(data->thread,thread)))
+    if (thr_lock_owner_equal(data->owner, owner))
       return 1;					/* Already locked by thread */
   }
   return 0;
@@ -365,12 +381,15 @@
 }
 
 
-static my_bool wait_for_lock(struct st_lock_list *wait, THR_LOCK_DATA *data,
-			     my_bool in_wait_list)
+static enum enum_thr_lock_result
+wait_for_lock(struct st_lock_list *wait, THR_LOCK_DATA *data,
+              my_bool in_wait_list)
 {
-  pthread_cond_t *cond=get_cond();
   struct st_my_thread_var *thread_var=my_thread_var;
-  int result;
+  pthread_cond_t *cond= &thread_var->suspend;
+  struct timeval now;
+  struct timespec wait_timeout;
+  enum enum_thr_lock_result result= THR_LOCK_ABORTED;
 
   if (!in_wait_list)
   {
@@ -383,30 +402,51 @@
   thread_var->current_mutex= &data->lock->mutex;
   thread_var->current_cond=  cond;
 
+  gettimeofday(&now, 0);
+  wait_timeout.tv_sec= now.tv_sec + table_lock_wait_timeout;
+  wait_timeout.tv_nsec= now.tv_usec * 1000;
   data->cond=cond;
+
   while (!thread_var->abort || in_wait_list)
   {
-    pthread_cond_wait(cond,&data->lock->mutex);
-    if (data->cond != cond)
+    int rc= pthread_cond_timedwait(cond, &data->lock->mutex,
&wait_timeout);
+    /*
+      We must break the wait if one of the following occurs:
+      - the connection has been aborted (!thread_var->abort), but
+        this is not a delayed insert thread (in_wait_list). For a delayed
+        insert thread the proper action at shutdown is, apparently, to
+        acquire the lock and complete the insert.
+      - the lock has been granted (data->cond is set to NULL by the granter),
+        or the waiting has been aborted (additionally data->type is set to
+        TL_UNLOCK).
+      - the wait has timed out (rc == ETIMEDOUT)
+      Order of checks below is important to not report about timeout
+      if the predicate is true.
+    */
+    if (data->cond == 0)
+      break;
+    if (rc == ETIMEDOUT)
+    {
+      result= THR_LOCK_WAIT_TIMEOUT;
       break;
+    }
   }
 
   if (data->cond || data->type == TL_UNLOCK)
   {
-    if (data->cond)				/* aborted */
+    if (data->cond)                             /* aborted or timed out */
     {
       if (((*data->prev)=data->next))		/* remove from wait-list */
 	data->next->prev= data->prev;
       else
 	wait->last=data->prev;
+      data->type= TL_UNLOCK;                    /* No lock */
     }
-    data->type=TL_UNLOCK;			/* No lock */
-    result=1;					/* Didn't get lock */
     check_locks(data->lock,"failed wait_for_lock",0);
   }
   else
   {
-    result=0;
+    result= THR_LOCK_SUCCESS;
     statistic_increment(locks_waited, &THR_LOCK_lock);
     if (data->lock->get_status)
       (*data->lock->get_status)(data->status_param, 0);
@@ -423,20 +463,23 @@
 }
 
 
-int thr_lock(THR_LOCK_DATA *data,enum thr_lock_type lock_type)
+enum enum_thr_lock_result
+thr_lock(THR_LOCK_DATA *data, THR_LOCK_OWNER *owner,
+         enum thr_lock_type lock_type)
 {
   THR_LOCK *lock=data->lock;
-  int result=0;
+  enum enum_thr_lock_result result= THR_LOCK_SUCCESS;
+  struct st_lock_list *wait_queue;
+  THR_LOCK_DATA *lock_owner;
   DBUG_ENTER("thr_lock");
 
   data->next=0;
   data->cond=0;					/* safety */
   data->type=lock_type;
-  data->thread=pthread_self();			/* Must be reset ! */
-  data->thread_id=my_thread_id();		/* Must be reset ! */
+  data->owner= owner;                           /* Must be reset ! */
   VOID(pthread_mutex_lock(&lock->mutex));
   DBUG_PRINT("lock",("data: 0x%lx  thread: %ld  lock: 0x%lx  type: %d",
-		      data,data->thread_id,lock,(int) lock_type));
+		      data,data->owner->thread_id, lock, (int) lock_type));
   check_locks(lock,(uint) lock_type <= (uint) TL_READ_NO_INSERT ?
 	      "enter read_lock" : "enter write_lock",0);
   if ((int) lock_type <= (int) TL_READ_NO_INSERT)
@@ -454,8 +497,8 @@
       */
 
       DBUG_PRINT("lock",("write locked by thread: %ld",
-			 lock->write.data->thread_id));
-      if (pthread_equal(data->thread,lock->write.data->thread) ||
+			 lock->write.data->owner->thread_id));
+      if (thr_lock_owner_equal(data->owner, lock->write.data->owner) ||
 	  (lock->write.data->type <= TL_WRITE_DELAYED &&
 	   (((int) lock_type <= (int) TL_READ_HIGH_PRIORITY) ||
 	    (lock->write.data->type != TL_WRITE_CONCURRENT_INSERT &&
@@ -476,14 +519,14 @@
       {
 	/* We are not allowed to get a READ lock in this case */
 	data->type=TL_UNLOCK;
-	result=1;				/* Can't wait for this one */
+        result= THR_LOCK_ABORTED;               /* Can't wait for this one */
 	goto end;
       }
     }
     else if (!lock->write_wait.data ||
 	     lock->write_wait.data->type <= TL_WRITE_LOW_PRIORITY ||
 	     lock_type == TL_READ_HIGH_PRIORITY ||
-	     have_old_read_lock(lock->read.data,data->thread))
+	     have_old_read_lock(lock->read.data, data->owner))
     {						/* No important write-locks */
       (*lock->read.last)=data;			/* Add to running FIFO */
       data->prev=lock->read.last;
@@ -496,8 +539,12 @@
       statistic_increment(locks_immediate,&THR_LOCK_lock);
       goto end;
     }
-    /* Can't get lock yet;  Wait for it */
-    DBUG_RETURN(wait_for_lock(&lock->read_wait,data,0));
+    /*
+      We're here if there is an active write lock  or no write
+      lock but a high priority write waiting in the write_wait queue.
+      In the latter case we should yield the lock to the writer.
+    */
+    wait_queue= &lock->read_wait;
   }
   else						/* Request for WRITE lock */
   {
@@ -506,7 +553,7 @@
       if (lock->write.data && lock->write.data->type == TL_WRITE_ONLY)
       {
 	data->type=TL_UNLOCK;
-	result=1;				/* Can't wait for this one */
+        result= THR_LOCK_ABORTED;               /* Can't wait for this one */
 	goto end;
       }
       /*
@@ -540,7 +587,7 @@
       {
 	/* We are not allowed to get a lock in this case */
 	data->type=TL_UNLOCK;
-	result=1;				/* Can't wait for this one */
+        result= THR_LOCK_ABORTED;               /* Can't wait for this one */
 	goto end;
       }
 
@@ -549,7 +596,7 @@
 	TL_WRITE_ALLOW_WRITE, TL_WRITE_ALLOW_READ or TL_WRITE_DELAYED in
 	the same thread, but this will never happen within MySQL.
       */
-      if (pthread_equal(data->thread,lock->write.data->thread) ||
+      if (thr_lock_owner_equal(data->owner, lock->write.data->owner) ||
 	  (lock_type == TL_WRITE_ALLOW_WRITE &&
 	   !lock->write_wait.data &&
 	   lock->write.data->type == TL_WRITE_ALLOW_WRITE))
@@ -572,7 +619,7 @@
 	goto end;
       }
       DBUG_PRINT("lock",("write locked by thread: %ld",
-			 lock->write.data->thread_id));
+			 lock->write.data->owner->thread_id));
     }
     else
     {
@@ -608,10 +655,25 @@
 	}
       }
       DBUG_PRINT("lock",("write locked by thread: %ld, type: %ld",
-			 lock->read.data->thread_id,data->type));
+			 lock->read.data->owner->thread_id,data->type));
     }
-    DBUG_RETURN(wait_for_lock(&lock->write_wait,data,0));
+    wait_queue= &lock->write_wait;
+  }
+  /*
+    Try to detect a trivial deadlock when using cursors: attempt to
+    lock a table that is already locked by an open cursor within the
+    same connection. lock_owner can be zero if we succumbed to a high
+    priority writer in the write_wait queue.
+  */
+  lock_owner= lock->read.data ? lock->read.data : lock->write.data;
+  if (lock_owner &&
+      pthread_equal(lock_owner->owner->thread, owner->thread))
+  {
+    result= THR_LOCK_DEADLOCK;
+    goto end;
   }
+  /* Can't get lock yet;  Wait for it */
+  DBUG_RETURN(wait_for_lock(wait_queue, data, 0));
 end:
   pthread_mutex_unlock(&lock->mutex);
   DBUG_RETURN(result);
@@ -656,7 +718,7 @@
       lock->read_no_write_count++;
     }      
     DBUG_PRINT("lock",("giving read lock to thread: %ld",
-		       data->thread_id));
+		       data->owner->thread_id));
     data->cond=0;				/* Mark thread free */
     VOID(pthread_cond_signal(cond));
   } while ((data=data->next));
@@ -674,7 +736,7 @@
   enum thr_lock_type lock_type=data->type;
   DBUG_ENTER("thr_unlock");
   DBUG_PRINT("lock",("data: 0x%lx  thread: %ld  lock: 0x%lx",
-		     data,data->thread_id,lock));
+		     data, data->owner->thread_id, lock));
   pthread_mutex_lock(&lock->mutex);
   check_locks(lock,"start of release lock",0);
 
@@ -734,7 +796,7 @@
 	      (*lock->check_status)(data->status_param))
 	    data->type=TL_WRITE;			/* Upgrade lock */
 	  DBUG_PRINT("lock",("giving write lock of type %d to thread: %ld",
-			     data->type,data->thread_id));
+			     data->type, data->owner->thread_id));
 	  {
 	    pthread_cond_t *cond=data->cond;
 	    data->cond=0;				/* Mark thread free */
@@ -842,7 +904,8 @@
 }
 
 
-int thr_multi_lock(THR_LOCK_DATA **data,uint count)
+enum enum_thr_lock_result
+thr_multi_lock(THR_LOCK_DATA **data, uint count, THR_LOCK_OWNER *owner)
 {
   THR_LOCK_DATA **pos,**end;
   DBUG_ENTER("thr_multi_lock");
@@ -852,10 +915,11 @@
   /* lock everything */
   for (pos=data,end=data+count; pos < end ; pos++)
   {
-    if (thr_lock(*pos,(*pos)->type))
+    enum enum_thr_lock_result result= thr_lock(*pos, owner, (*pos)->type);
+    if (result != THR_LOCK_SUCCESS)
     {						/* Aborted */
       thr_multi_unlock(data,(uint) (pos-data));
-      DBUG_RETURN(1);
+      DBUG_RETURN(result);
     }
 #ifdef MAIN
     printf("Thread: %s  Got lock: 0x%lx  type: %d\n",my_thread_name(),
@@ -909,7 +973,7 @@
     } while (pos != data);
   }
 #endif
-  DBUG_RETURN(0);
+  DBUG_RETURN(THR_LOCK_SUCCESS);
 }
 
   /* free all locks */
@@ -932,7 +996,7 @@
     else
     {
       DBUG_PRINT("lock",("Free lock: data: 0x%lx  thread: %ld  lock: 0x%lx",
-			 *pos,(*pos)->thread_id,(*pos)->lock));
+			 *pos, (*pos)->owner->thread_id, (*pos)->lock));
     }
   }
   DBUG_VOID_RETURN;
@@ -952,6 +1016,7 @@
   for (data=lock->read_wait.data; data ; data=data->next)
   {
     data->type=TL_UNLOCK;			/* Mark killed */
+    /* It's safe to signal the cond first: we're still holding the mutex. */
     pthread_cond_signal(data->cond);
     data->cond=0;				/* Removed from list */
   }
@@ -985,10 +1050,11 @@
   pthread_mutex_lock(&lock->mutex);
   for (data= lock->read_wait.data; data ; data= data->next)
   {
-    if (pthread_equal(thread, data->thread))
+    if (pthread_equal(thread, data->owner->thread))
     {
       DBUG_PRINT("info",("Aborting read-wait lock"));
       data->type= TL_UNLOCK;			/* Mark killed */
+      /* It's safe to signal the cond first: we're still holding the mutex. */
       pthread_cond_signal(data->cond);
       data->cond= 0;				/* Removed from list */
 
@@ -1000,7 +1066,7 @@
   }
   for (data= lock->write_wait.data; data ; data= data->next)
   {
-    if (pthread_equal(thread, data->thread))
+    if (pthread_equal(thread, data->owner->thread))
     {
       DBUG_PRINT("info",("Aborting write-wait lock"));
       data->type= TL_UNLOCK;
@@ -1117,7 +1183,8 @@
     prev= &list->data;
     for (data=list->data; data && count++ < MAX_LOCKS ; data=data->next)
     {
-      printf("0x%lx (%lu:%d); ",(ulong) data,data->thread_id,(int) data->type);
+      printf("0x%lx (%lu:%d); ", (ulong) data, data->owner->thread_id,
+             (int) data->type);
       if (data->prev != prev)
 	printf("\nWarning: prev didn't point at previous lock\n");
       prev= &data->next;
@@ -1250,11 +1317,13 @@
 {
   int i,j,param=*((int*) arg);
   THR_LOCK_DATA data[MAX_LOCK_COUNT];
+  THR_LOCK_OWNER owner;
   THR_LOCK_DATA *multi_locks[MAX_LOCK_COUNT];
   my_thread_init();
 
   printf("Thread %s (%d) started\n",my_thread_name(),param); fflush(stdout);
 
+  thr_lock_owner_init(&owner);
   for (i=0; i < lock_counts[param] ; i++)
     thr_lock_data_init(locks+tests[param][i].lock_nr,data+i,NULL);
   for (j=1 ; j < 10 ; j++)		/* try locking 10 times */
@@ -1264,7 +1333,7 @@
       multi_locks[i]= &data[i];
       data[i].type= tests[param][i].lock_type;
     }
-    thr_multi_lock(multi_locks,lock_counts[param]);
+    thr_multi_lock(multi_locks,lock_counts[param], &owner);
     pthread_mutex_lock(&LOCK_thread_count);
     {
       int tmp=rand() & 7;			/* Do something from 0-2 sec */

--- 1.151/sql/ha_berkeley.cc	2005-06-09 17:48:47 +04:00
+++ 1.152/sql/ha_berkeley.cc	2005-07-08 22:34:42 +04:00
@@ -120,7 +120,8 @@
   NULL, /* prepare */
   NULL, /* recover */
   NULL, /* commit_by_xid */
-  NULL /* rollback_by_xid */
+  NULL, /* rollback_by_xid */
+  TRUE  /* cursors_must_be_closed_at_commit */
 };
 
 typedef struct st_berkeley_trx_data {
@@ -372,6 +373,19 @@
 /*****************************************************************************
 ** Berkeley DB tables
 *****************************************************************************/
+
+ha_berkeley::ha_berkeley(TABLE *table)
+  :handler(table), alloc_ptr(0), rec_buff(0), file(0),
+  int_table_flags(HA_REC_NOT_IN_SEQ | HA_FAST_KEY_READ |
+                  HA_NULL_IN_KEY | HA_CAN_INDEX_BLOBS | HA_NOT_EXACT_COUNT |
+                  HA_PRIMARY_KEY_IN_READ_INDEX | HA_FILE_BASED |
+                  HA_AUTO_PART_KEY | HA_TABLE_SCAN_ON_INDEX),
+  changed_rows(0), last_dup_key((uint) -1), version(0), using_ignore(0)
+{
+  ht= &berkeley_hton;
+}
+
+
 static const char *ha_berkeley_exts[] = {
   ha_berkeley_ext,
   NullS

--- 1.70/sql/ha_berkeley.h	2005-05-09 13:26:46 +04:00
+++ 1.71/sql/ha_berkeley.h	2005-07-08 22:34:42 +04:00
@@ -85,12 +85,7 @@
   DBT *get_pos(DBT *to, byte *pos);
 
  public:
-  ha_berkeley(TABLE *table): handler(table), alloc_ptr(0),rec_buff(0), file(0),
-    int_table_flags(HA_REC_NOT_IN_SEQ | HA_FAST_KEY_READ |
-		    HA_NULL_IN_KEY | HA_CAN_INDEX_BLOBS | HA_NOT_EXACT_COUNT |
-		    HA_PRIMARY_KEY_IN_READ_INDEX | HA_FILE_BASED |
-		    HA_AUTO_PART_KEY | HA_TABLE_SCAN_ON_INDEX),
-    changed_rows(0),last_dup_key((uint) -1),version(0),using_ignore(0) {}
+  ha_berkeley(TABLE *table);
   ~ha_berkeley() {}
   const char *table_type() const { return "BerkeleyDB"; }
   ulong index_flags(uint idx, uint part, bool all_parts) const;

--- 1.153/sql/ha_myisam.cc	2005-06-13 14:41:07 +04:00
+++ 1.154/sql/ha_myisam.cc	2005-07-08 22:34:42 +04:00
@@ -44,6 +44,32 @@
 ** MyISAM tables
 *****************************************************************************/
 
+/*
+  MyISAM handlerton: for now used only for SQL level cursors,
+  the rest of the members have dummy placeholders.
+*/
+
+static handlerton myisam_handlerton= {
+  "MyISAM",
+  0,       /* slot */
+  0,       /* savepoint size. */
+  0,       /* close_connection */
+  0,       /* savepoint */
+  0,       /* rollback to savepoint */
+  0,       /* release savepoint */
+  0,       /* commit */
+  0,       /* rollback */
+  0,       /* prepare */
+  0,       /* recover */
+  0,       /* commit_by_xid */
+  0,       /* rollback_by_xid */
+  /*
+    MyISAM doesn't support transactions and doesn't have
+    transaction-dependent context.
+  */
+  FALSE    /* cursors_must_be_closed_at_commit */
+};
+
 // collect errors printed by mi_check routines
 
 static void mi_check_print_msg(MI_CHECK *param,	const char* msg_type,
@@ -122,6 +148,19 @@
 }
 
 }
+
+
+ha_myisam::ha_myisam(TABLE *table)
+  :handler(table), file(0),
+  int_table_flags(HA_NULL_IN_KEY | HA_CAN_FULLTEXT | HA_CAN_SQL_HANDLER |
+                  HA_DUPP_POS | HA_CAN_INDEX_BLOBS | HA_AUTO_PART_KEY |
+                  HA_FILE_BASED | HA_CAN_GEOMETRY | HA_READ_RND_SAME |
+                  HA_CAN_INSERT_DELAYED | HA_CAN_BIT_FIELD),
+  can_enable_indexes(1)
+{
+  ht= &myisam_handlerton;
+}
+
 
 static const char *ha_myisam_exts[] = {
   ".MYI",

--- 1.67/sql/ha_myisam.h	2005-05-09 13:26:46 +04:00
+++ 1.68/sql/ha_myisam.h	2005-07-08 22:34:42 +04:00
@@ -43,13 +43,7 @@
   int repair(THD *thd, MI_CHECK &param, bool optimize);
 
  public:
-  ha_myisam(TABLE *table): handler(table), file(0),
-    int_table_flags(HA_NULL_IN_KEY | HA_CAN_FULLTEXT | HA_CAN_SQL_HANDLER |
-		    HA_DUPP_POS | HA_CAN_INDEX_BLOBS | HA_AUTO_PART_KEY |
-		    HA_FILE_BASED | HA_CAN_GEOMETRY | HA_READ_RND_SAME |
-                    HA_CAN_INSERT_DELAYED | HA_CAN_BIT_FIELD),
-    can_enable_indexes(1)
-  {}
+  ha_myisam(TABLE *table);
   ~ha_myisam() {}
   const char *table_type() const { return "MyISAM"; }
   const char *index_type(uint key_number);

--- 1.178/sql/handler.cc	2005-07-04 04:50:00 +04:00
+++ 1.179/sql/handler.cc	2005-07-08 22:34:43 +04:00
@@ -208,15 +208,8 @@
   case DB_TYPE_HASH:
     return new ha_hash(table);
 #endif
-#ifdef HAVE_ISAM
-  case DB_TYPE_MRG_ISAM:
-    return new ha_isammrg(table);
-  case DB_TYPE_ISAM:
-    return new ha_isam(table);
-#else
   case DB_TYPE_MRG_ISAM:
     return new ha_myisammrg(table);
-#endif
 #ifdef HAVE_BERKELEY_DB
   case DB_TYPE_BERKELEY_DB:
     return new ha_berkeley(table);
@@ -634,6 +627,10 @@
       DBUG_RETURN(1);
     }
     DBUG_EXECUTE_IF("crash_commit_before", abort(););
+
+    /* Close all cursors that can not survive COMMIT */
+    thd->stmt_map.close_transient_cursors();
+
     if (!trans->no_2pc && trans->nht > 1)
     {
       for (; *ht && !error; ht++)
@@ -735,6 +732,9 @@
 #ifdef USING_TRANSACTIONS
   if (trans->nht)
   {
+    /* Close all cursors that can not survive ROLLBACK */
+    thd->stmt_map.close_transient_cursors();
+
     for (handlerton **ht=trans->ht; *ht; ht++)
     {
       int err;

--- 1.144/sql/handler.h	2005-06-22 16:09:59 +04:00
+++ 1.145/sql/handler.h	2005-07-08 22:34:43 +04:00
@@ -349,6 +349,8 @@
    int  (*recover)(XID *xid_list, uint len);
    int  (*commit_by_xid)(XID *xid);
    int  (*rollback_by_xid)(XID *xid);
+   /* True if SQL level cursors must be closed at COMMIT or ROLLBACK */
+   my_bool cursors_must_be_closed_at_commit;
 } handlerton;
 
 typedef struct st_thd_trans
@@ -445,6 +447,7 @@
   virtual int rnd_end() { return 0; }
 
 public:
+  const handlerton *ht;                 /* storage engine of this handler */
   byte *ref;				/* Pointer to current row */
   byte *dupp_ref;			/* Pointer to dupp row */
   ulonglong data_file_length;		/* Length off data file */
@@ -486,7 +489,7 @@
   bool implicit_emptied;                /* Can be !=0 only if HEAP */
   const COND *pushed_cond;
 
-  handler(TABLE *table_arg) :table(table_arg),
+  handler(TABLE *table_arg) :table(table_arg), ht(0),
     ref(0), data_file_length(0), max_data_file_length(0), index_file_length(0),
     delete_length(0), auto_increment_value(0),
     records(0), deleted(0), mean_rec_length(0),

--- 1.227/sql/item_func.cc	2005-07-04 04:44:30 +04:00
+++ 1.228/sql/item_func.cc	2005-07-08 22:34:43 +04:00
@@ -3133,7 +3133,7 @@
   THD* thd=current_thd;
   User_level_lock* ull;
   struct timespec abstime;
-  int lock_name_len,error=0;
+  int lock_name_len;
   lock_name_len=strlen(lock_name);
   pthread_mutex_lock(&LOCK_user_locks);
 
@@ -3167,8 +3167,8 @@
 
   set_timespec(abstime,lock_timeout);
   while (!thd->killed &&
-	 (error=pthread_cond_timedwait(&ull->cond,&LOCK_user_locks,&abstime))
-	 != ETIME && error != ETIMEDOUT && ull->locked) ;
+	 pthread_cond_timedwait(&ull->cond, &LOCK_user_locks,
+                                &abstime) != ETIMEDOUT && ull->locked) ;
   if (ull->locked)
   {
     if (!--ull->count)
@@ -3270,14 +3270,14 @@
   set_timespec(abstime,timeout);
   while (!thd->killed &&
 	 (error=pthread_cond_timedwait(&ull->cond,&LOCK_user_locks,&abstime))
-	 != ETIME && error != ETIMEDOUT && error != EINVAL &&
ull->locked) ;
+	 != ETIMEDOUT && error != EINVAL && ull->locked) ;
   if (thd->killed)
     error=EINTR;				// Return NULL
   if (ull->locked)
   {
     if (!--ull->count)
       delete ull;				// Should never happen
-    if (error != ETIME && error != ETIMEDOUT)
+    if (error != ETIMEDOUT)
     {
       error=1;
       null_value=1;				// Return NULL

--- 1.68/sql/lock.cc	2005-06-06 22:21:25 +04:00
+++ 1.69/sql/lock.cc	2005-07-08 22:34:43 +04:00
@@ -103,6 +103,10 @@
 {
   MYSQL_LOCK *sql_lock;
   TABLE *write_lock_used;
+  int rc;
+  /* Map the return value of thr_lock to an error from errmsg.txt */
+  const static int thr_lock_errno_to_mysql[]=
+  { 0, 1, ER_LOCK_WAIT_TIMEOUT, ER_LOCK_DEADLOCK };
   DBUG_ENTER("mysql_lock_tables");
 
   for (;;)
@@ -135,15 +139,24 @@
     {
       my_free((gptr) sql_lock,MYF(0));
       sql_lock=0;
-      thd->proc_info=0;
       break;
     }
     thd->proc_info="Table lock";
     thd->locked=1;
-    if (thr_multi_lock(sql_lock->locks,sql_lock->lock_count))
+    rc= thr_lock_errno_to_mysql[(int) thr_multi_lock(sql_lock->locks,
+                                                     sql_lock->lock_count,
+                                                     thd->lock_id)];
+    if (rc > 1)                                 /* a timeout or a deadlock */
+    {
+      my_error(rc, MYF(0));
+      my_free((gptr) sql_lock,MYF(0));
+      sql_lock= 0;
+      break;
+    }
+    else if (rc == 1)                           /* aborted */
     {
       thd->some_tables_deleted=1;		// Try again
-      sql_lock->lock_count=0;			// Locks are alread freed
+      sql_lock->lock_count= 0;                  // Locks are already freed
     }
     else if (!thd->some_tables_deleted || (flags & MYSQL_LOCK_IGNORE_FLUSH))
     {

--- 1.478/sql/mysqld.cc	2005-07-04 04:44:31 +04:00
+++ 1.479/sql/mysqld.cc	2005-07-08 22:34:43 +04:00
@@ -4336,7 +4336,8 @@
   OPT_ENABLE_LARGE_PAGES,
   OPT_TIMED_MUTEXES,
   OPT_OLD_STYLE_USER_LIMITS,
-  OPT_LOG_SLOW_ADMIN_STATEMENTS
+  OPT_LOG_SLOW_ADMIN_STATEMENTS,
+  OPT_TABLE_LOCK_WAIT_TIMEOUT
 };
 
 
@@ -5589,6 +5590,10 @@
    "The number of open tables for all threads.", (gptr*) &table_cache_size,
    (gptr*) &table_cache_size, 0, GET_ULONG, REQUIRED_ARG, 64, 1, 512*1024L,
    0, 1, 0},
+  {"table_lock_wait_timeout", OPT_TABLE_LOCK_WAIT_TIMEOUT, "Timeout in "
+    "seconds to wait for a table level lock before returning an error.",
+   (gptr*) &table_lock_wait_timeout, (gptr*) &table_lock_wait_timeout,
+   0, GET_ULONG, REQUIRED_ARG, 50, 1, 1024 * 1024 * 1024, 0, 1, 0},
   {"thread_cache_size", OPT_THREAD_CACHE_SIZE,
    "How many threads we should keep in a cache for reuse.",
    (gptr*) &thread_cache_size, (gptr*) &thread_cache_size, 0, GET_ULONG,
@@ -7077,4 +7082,6 @@
 template class I_List<i_string>;
 template class I_List<i_string_pair>;
 template class I_List<NAMED_LIST>;
+template class I_List<Statement>;
+template class I_List_iterator<Statement>;
 #endif

--- 1.248/sql/slave.cc	2005-06-22 13:08:21 +04:00
+++ 1.249/sql/slave.cc	2005-07-08 22:34:43 +04:00
@@ -2753,7 +2753,7 @@
     else
       pthread_cond_wait(&data_cond, &data_lock);
     DBUG_PRINT("info",("Got signal of master update or timed out"));
-    if (error == ETIMEDOUT || error == ETIME)
+    if (error == ETIMEDOUT)
     {
       error= -1;
       break;

--- 1.192/sql/sql_class.cc	2005-07-05 20:46:01 +04:00
+++ 1.193/sql/sql_class.cc	2005-07-08 22:34:43 +04:00
@@ -164,6 +164,7 @@
 
 THD::THD()
   :Statement(CONVENTIONAL_EXECUTION, 0, ALLOC_ROOT_MIN_BLOCK_SIZE, 0),
+   lock_id(&main_lock_id),
    user_time(0), global_read_lock(0), is_fatal_error(0),
    rand_used(0), time_zone_used(0),
    last_insert_id_used(0), insert_id_used(0), clear_next_insert_id(0),
@@ -260,6 +261,7 @@
   ulong tmp=sql_rnd_with_mutex();
   randominit(&rand, tmp + (ulong) &rand, tmp + (ulong) ::query_id);
   prelocked_mode= NON_PRELOCKED;
+  thr_lock_owner_init(&main_lock_id); /* safety: will be reset after start */
 }
 
 
@@ -513,6 +515,7 @@
     if this is the slave SQL thread.
   */
   variables.pseudo_thread_id= thread_id;
+  thr_lock_owner_init(&main_lock_id);
   return 0;
 }
 
@@ -1558,6 +1561,12 @@
 }
 
 
+void Statement::close_cursor()
+{
+  DBUG_ASSERT("Statement::close_cursor()" == "not implemented");
+}
+
+
 void THD::end_statement()
 {
   /* Cleanup SQL processing state to resuse this statement in next query. */
@@ -1675,6 +1684,14 @@
       hash_delete(&st_hash, (byte*)statement);
   }
   return rc;
+}
+
+
+void Statement_map::close_transient_cursors()
+{
+  Statement *stmt;
+  while ((stmt= transient_cursor_list.head()))
+    stmt->close_cursor();                 /* deletes itself from the list */
 }
 
 

--- 1.246/sql/sql_class.h	2005-07-04 04:42:15 +04:00
+++ 1.247/sql/sql_class.h	2005-07-08 22:34:43 +04:00
@@ -756,7 +756,7 @@
   be used explicitly.
 */
 
-class Statement: public Query_arena
+class Statement: public ilink, public Query_arena
 {
   Statement(const Statement &rhs);              /* not implemented: */
   Statement &operator=(const Statement &rhs);   /* non-copyable */
@@ -833,6 +833,8 @@
   void restore_backup_statement(Statement *stmt, Statement *backup);
   /* return class type */
   virtual Type type() const;
+  /* Close the cursor open for this statement, if there is one */
+  virtual void close_cursor();
 };
 
 
@@ -884,11 +886,21 @@
     }
     hash_delete(&st_hash, (byte *) statement);
   }
+  void add_transient_cursor(Statement *stmt)
+  { transient_cursor_list.append(stmt); }
+  void erase_transient_cursor(Statement *stmt) { stmt->unlink(); }
+  /*
+    Close all cursors of this connection that use tables of a storage
+    engine that has transaction-specific state and therefore can not
+    survive COMMIT or ROLLBACK. Currently all but MyISAM cursors are closed.
+  */
+  void close_transient_cursors();
   /* Erase all statements (calls Statement destructor) */
   void reset()
   {
     my_hash_reset(&names_hash);
     my_hash_reset(&st_hash);
+    transient_cursor_list.empty();
     last_found_statement= 0;
   }
 
@@ -900,6 +912,7 @@
 private:
   HASH st_hash;
   HASH names_hash;
+  I_List<Statement> transient_cursor_list;
   Statement *last_found_statement;
 };
 
@@ -937,8 +950,7 @@
   a thread/connection descriptor
 */
 
-class THD :public ilink,
-           public Statement
+class THD :public Statement
 {
 public:
 #ifdef EMBEDDED_LIBRARY
@@ -963,6 +975,9 @@
   struct  rand_struct rand;		// used for authentication
   struct  system_variables variables;	// Changeable local variables
   struct  system_status_var status_var; // Per thread statistic vars
+  THR_LOCK_OWNER main_lock_id;          // To use for conventional queries
+  THR_LOCK_OWNER *lock_id;              // If not main_lock_id, points to
+                                        // the lock_id of a cursor.
   pthread_mutex_t LOCK_delete;		// Locked before thd is deleted
   /* all prepared statements and cursors of this connection */
   Statement_map stmt_map; 

--- 1.166/sql/sql_insert.cc	2005-07-04 04:24:21 +04:00
+++ 1.167/sql/sql_insert.cc	2005-07-08 22:34:43 +04:00
@@ -1730,7 +1730,7 @@
 #endif
 	if (thd->killed || di->status)
 	  break;
-	if (error == ETIME || error == ETIMEDOUT)
+	if (error == ETIMEDOUT)
 	{
 	  thd->killed= THD::KILL_CONNECTION;
 	  break;

--- 1.331/sql/sql_select.cc	2005-07-04 04:44:31 +04:00
+++ 1.332/sql/sql_select.cc	2005-07-08 22:34:43 +04:00
@@ -1702,10 +1702,12 @@
 
 Cursor::Cursor(THD *thd)
   :Query_arena(&main_mem_root, INITIALIZED),
-   join(0), unit(0)
+   join(0), unit(0),
+   must_be_closed_at_commit(FALSE)
 {
   /* We will overwrite it at open anyway. */
   init_sql_alloc(&main_mem_root, ALLOC_ROOT_MIN_BLOCK_SIZE, 0);
+  thr_lock_owner_init(&lock_id);
 }
 
 
@@ -1739,6 +1741,19 @@
   free_list=	  thd->free_list;
   change_list=    thd->change_list;
   reset_thd(thd);
+
+  must_be_closed_at_commit= FALSE; /* reset in case we're reusing the cursor */
+  for (TABLE *table= open_tables; table; table= table->next)
+  {
+    const handlerton *ht= table->file->ht;
+    if (ht)
+      must_be_closed_at_commit|= ht->cursors_must_be_closed_at_commit;
+    else
+    {
+      must_be_closed_at_commit= TRUE;     /* handler status is unknown */
+      break;
+    }
+  }
   /*
     XXX: thd->locked_tables is not changed.
     What problems can we have with it if cursor is open?
@@ -1914,12 +1929,6 @@
   DBUG_VOID_RETURN;
 }
 
-
-Cursor::~Cursor()
-{
-  if (is_open())
-    close(FALSE);
-}
 
 /*********************************************************************/
 

--- 1.91/sql/sql_select.h	2005-07-01 15:45:09 +04:00
+++ 1.92/sql/sql_select.h	2005-07-08 22:34:44 +04:00
@@ -392,6 +392,8 @@
 public:
   Item_change_list change_list;
   select_send result;
+  THR_LOCK_OWNER lock_id;
+  my_bool must_be_closed_at_commit;
 
   /* Temporary implementation as now we replace THD state by value */
   /* Save THD state into cursor */
@@ -408,7 +410,7 @@
 
   void set_unit(SELECT_LEX_UNIT *unit_arg) { unit= unit_arg; }
   Cursor(THD *thd);
-  ~Cursor();
+  ~Cursor() {}
 };
 
 

--- 1.41/sql/examples/ha_archive.cc	2005-06-06 22:21:28 +04:00
+++ 1.42/sql/examples/ha_archive.cc	2005-07-08 22:34:42 +04:00
@@ -143,13 +143,14 @@
   0,       /* close_connection */
   0,       /* savepoint */
   0,       /* rollback to savepoint */
-  0,       /* releas savepoint */
+  0,       /* release savepoint */
   0,       /* commit */
   0,       /* rollback */
   0,       /* prepare */
   0,       /* recover */
   0,       /* commit_by_xid */
-  0        /* rollback_by_xid */
+  0,       /* rollback_by_xid */
+  TRUE     /* cursors_must_be_closed_at_commit (TODO: test and set to FALSE) */
 };
 
 

--- 1.191/sql/ha_ndbcluster.cc	2005-07-06 13:39:44 +04:00
+++ 1.192/sql/ha_ndbcluster.cc	2005-07-08 22:34:42 +04:00
@@ -62,7 +62,13 @@
   NULL, /* prepare */
   NULL, /* recover */
   NULL, /* commit_by_xid */
-  NULL  /* rollback_by_xid */
+  NULL, /* rollback_by_xid */
+  /*
+    NDB does not support consistent read views for now, and COMMIT
+    or ROLLBACK destroys the scan object of the transaction.
+    Therefore all active cursors must be closed.
+   */
+  TRUE /* cursors_must_be_closed_at_commit */
 };
 
 #define NDB_HIDDEN_PRIMARY_KEY_LENGTH 8
@@ -4214,6 +4220,7 @@
  
   DBUG_ENTER("ha_ndbcluster");
 
+  ht= &ndbcluster_hton;
   m_tabname[0]= '\0';
   m_dbname[0]= '\0';
 

--- 1.225/sql/ha_innodb.cc	2005-07-06 10:38:21 +04:00
+++ 1.226/sql/ha_innodb.cc	2005-07-08 22:34:42 +04:00
@@ -215,7 +215,14 @@
   innobase_xa_prepare,		/* prepare */
   innobase_xa_recover,		/* recover */
   innobase_commit_by_xid,	/* commit_by_xid */
-  innobase_rollback_by_xid	/* rollback_by_xid */
+  innobase_rollback_by_xid,     /* rollback_by_xid */
+  /*
+    For now when one opens a cursor, MySQL does not create an own
+    InnoDB consistent read view for it, and uses the view of the
+    currently active transaction. Therefore, cursors can not
+    survive COMMIT or ROLLBACK statements, which free this view.
+  */
+  TRUE                          /* cursors_must_be_closed_at_commit */
 };
 
 /*********************************************************************
@@ -763,6 +770,26 @@
 	}
 
 	return(trx);
+}
+
+
+/*************************************************************************
+Construct ha_innobase handler. */
+
+ha_innobase::ha_innobase(TABLE *table)
+  :handler(table),
+  int_table_flags(HA_REC_NOT_IN_SEQ |
+                  HA_NULL_IN_KEY |
+                  HA_CAN_INDEX_BLOBS |
+                  HA_CAN_SQL_HANDLER |
+                  HA_NOT_EXACT_COUNT |
+                  HA_PRIMARY_KEY_IN_READ_INDEX |
+                  HA_TABLE_SCAN_ON_INDEX),
+  last_dup_key((uint) -1),
+  start_of_scan(0),
+  num_write_row(0)
+{
+  ht= &innobase_hton;
 }
 
 /*************************************************************************

--- 1.96/sql/ha_innodb.h	2005-06-21 08:36:03 +04:00
+++ 1.97/sql/ha_innodb.h	2005-07-08 22:34:42 +04:00
@@ -81,19 +81,7 @@
 
 	/* Init values for the class: */
  public:
-  	ha_innobase(TABLE *table): handler(table),
-	  int_table_flags(HA_REC_NOT_IN_SEQ |
-			  HA_NULL_IN_KEY |
-			  HA_CAN_INDEX_BLOBS |
-			  HA_CAN_SQL_HANDLER |
-			  HA_NOT_EXACT_COUNT |
-			  HA_PRIMARY_KEY_IN_READ_INDEX |
-			  HA_TABLE_SCAN_ON_INDEX),
-	  last_dup_key((uint) -1),
-	  start_of_scan(0),
-	  num_write_row(0)
-  	{
-  	}
+  	ha_innobase(TABLE *table);
   	~ha_innobase() {}
 	/*
 	  Get the row type from the storage engine.  If this method returns

--- 1.16/mysql-test/r/lock.result	2005-05-24 18:41:34 +04:00
+++ 1.17/mysql-test/r/lock.result	2005-07-08 22:34:42 +04:00
@@ -57,3 +57,12 @@
 delete t2 from t1,t2 where t1.a=t2.a;
 ERROR HY000: Table 't2' was locked with a READ lock and can't be updated
 drop table t1,t2;
+create table t1 (a int);
+insert into t1 (a) values (1);
+lock table t1 write;
+set global table_lock_wait_timeout=1;
+select * from t1;
+ERROR HY000: Lock wait timeout exceeded; try restarting transaction
+drop table t1;
+unlock tables;
+set global table_lock_wait_timeout=default;

--- 1.13/mysql-test/t/lock.test	2005-05-24 18:41:34 +04:00
+++ 1.14/mysql-test/t/lock.test	2005-07-08 22:34:42 +04:00
@@ -73,3 +73,21 @@
 --error 1099
 delete t2 from t1,t2 where t1.a=t2.a;
 drop table t1,t2;
+
+#
+# Test global variable `table_lock_wait_timeout'
+# 
+
+create table t1 (a int);
+insert into t1 (a) values (1);
+lock table t1 write;
+connect (con1,localhost,root,,);
+connection con1;
+set global table_lock_wait_timeout=1;
+--error 1205
+select * from t1;
+connection default;
+drop table t1;
+unlock tables;
+set global table_lock_wait_timeout=default;
+

--- 1.3/mysys/my_os2cond.c	2003-01-28 09:38:22 +03:00
+++ 1.4/mysys/my_os2cond.c	2005-07-08 22:34:42 +04:00
@@ -100,7 +100,7 @@
 
    rc = DosWaitEventSem(cond->semaphore, timeout);
    if (rc != 0)
-      rval = ETIME;
+      rval= ETIMEDOUT;
 
    if (mutex) pthread_mutex_lock(mutex);
 

--- 1.123/sql/set_var.cc	2005-07-05 20:46:01 +04:00
+++ 1.124/sql/set_var.cc	2005-07-08 22:34:43 +04:00
@@ -375,6 +375,8 @@
 sys_var_bool_ptr	sys_sync_frm("sync_frm", &opt_sync_frm);
 sys_var_long_ptr	sys_table_cache_size("table_cache",
 					     &table_cache_size);
+sys_var_long_ptr	sys_table_lock_wait_timeout("table_lock_wait_timeout",
+                                                    &table_lock_wait_timeout);
 sys_var_long_ptr	sys_thread_cache_size("thread_cache_size",
 					      &thread_cache_size);
 sys_var_thd_enum	sys_tx_isolation("tx_isolation",
@@ -682,6 +684,7 @@
 #endif
   &sys_sync_frm,
   &sys_table_cache_size,
+  &sys_table_lock_wait_timeout,
   &sys_table_type,
   &sys_thread_cache_size,
   &sys_time_format,
@@ -972,6 +975,7 @@
   {"system_time_zone",        system_time_zone,                     SHOW_CHAR},
 #endif
   {"table_cache",             (char*) &table_cache_size,            SHOW_LONG},
+  {"table_lock_wait_timeout", (char*) &table_lock_wait_timeout,     SHOW_LONG },
   {sys_table_type.name,	      (char*) &sys_table_type,	            SHOW_SYS},
   {sys_thread_cache_size.name,(char*) &sys_thread_cache_size,       SHOW_SYS},
 #ifdef HAVE_THR_SETCONCURRENCY

--- 1.134/sql/sql_prepare.cc	2005-07-04 04:24:21 +04:00
+++ 1.135/sql/sql_prepare.cc	2005-07-08 22:34:43 +04:00
@@ -106,6 +106,7 @@
   virtual ~Prepared_statement();
   void setup_set_params();
   virtual Query_arena::Type type() const;
+  virtual void close_cursor();
 };
 
 static void execute_stmt(THD *thd, Prepared_statement *stmt,
@@ -1983,10 +1984,7 @@
 
   cursor= stmt->cursor;
   if (cursor && cursor->is_open())
-  {
-    my_error(ER_EXEC_STMT_WITH_OPEN_CURSOR, MYF(0));
-    DBUG_VOID_RETURN;
-  }
+    stmt->close_cursor();
 
   DBUG_ASSERT(thd->free_list == NULL);
   mysql_reset_thd_for_next_command(thd);
@@ -2011,6 +2009,7 @@
         DBUG_VOID_RETURN;
       /* If lex->result is set, mysql_execute_command will use it */
       stmt->lex->result= &cursor->result;
+      thd->lock_id= &cursor->lock_id;
     }
   }
 #ifndef EMBEDDED_LIBRARY
@@ -2060,6 +2059,9 @@
       Cursor::open is buried deep in JOIN::exec of the top level join.
     */
     cursor->init_from_thd(thd);
+
+    if (cursor->must_be_closed_at_commit)
+      thd->stmt_map.add_transient_cursor(stmt);
   }
   else
   {
@@ -2069,6 +2071,7 @@
   }
 
   thd->set_statement(&stmt_backup);
+  thd->lock_id= &thd->main_lock_id;
   thd->current_arena= thd;
   DBUG_VOID_RETURN;
 
@@ -2243,6 +2246,8 @@
       the previous calls.
     */
     free_root(cursor->mem_root, MYF(0));
+    if (cursor->must_be_closed_at_commit)
+      thd->stmt_map.erase_transient_cursor(stmt);
   }
 
   thd->restore_backup_statement(stmt, &stmt_backup);
@@ -2281,23 +2286,10 @@
   if (!(stmt= find_prepared_statement(thd, stmt_id, "mysql_stmt_reset")))
     DBUG_VOID_RETURN;
 
-  cursor= stmt->cursor;
-  if (cursor && cursor->is_open())
-  {
-    thd->change_list= cursor->change_list;
-    cursor->close(FALSE);
-    cleanup_stmt_and_thd_after_use(stmt, thd);
-    free_root(cursor->mem_root, MYF(0));
-  }
+  stmt->close_cursor();                    /* will reset statement params */
 
   stmt->state= Query_arena::PREPARED;
 
-  /*
-    Clear parameters from data which could be set by
-    mysql_stmt_send_long_data() call.
-  */
-  reset_stmt_params(stmt);
-
   mysql_reset_thd_for_next_command(thd);
   send_ok(thd);
 
@@ -2445,10 +2437,17 @@
 Prepared_statement::~Prepared_statement()
 {
   if (cursor)
+  {
+    if (cursor->is_open())
+    {
+      cursor->close(FALSE);
+      free_items();
+      free_root(cursor->mem_root, MYF(0));
+    }
     cursor->Cursor::~Cursor();
-  free_items();
-  if (cursor)
-    free_root(cursor->mem_root, MYF(0));
+  }
+  else
+    free_items();
   delete lex->result;
 }
 
@@ -2456,4 +2455,23 @@
 Query_arena::Type Prepared_statement::type() const
 {
   return PREPARED_STATEMENT;
+}
+
+
+void Prepared_statement::close_cursor()
+{
+  if (cursor && cursor->is_open())
+  {
+    thd->change_list= cursor->change_list;
+    cursor->close(FALSE);
+    cleanup_stmt_and_thd_after_use(this, thd);
+    free_root(cursor->mem_root, MYF(0));
+    if (cursor->must_be_closed_at_commit)
+      thd->stmt_map.erase_transient_cursor(this);
+  }
+  /*
+    Clear parameters from data which could be set by
+    mysql_stmt_send_long_data() call.
+  */
+  reset_stmt_params(this);
 }

--- 1.130/tests/mysql_client_test.c	2005-07-05 20:47:17 +04:00
+++ 1.131/tests/mysql_client_test.c	2005-07-08 22:34:44 +04:00
@@ -55,6 +55,7 @@
 static unsigned int test_count= 0;
 static unsigned int opt_count= 0;
 static unsigned int iter_count= 0;
+static my_bool have_innodb= FALSE;
 
 static const char *opt_basedir= "./";
 
@@ -212,6 +213,28 @@
   }
 }
 
+/* Check if the connection has InnoDB tables */
+
+static my_bool check_have_innodb(MYSQL *conn)
+{
+  MYSQL_RES *res;
+  MYSQL_ROW row;
+  int rc;
+  my_bool result;
+
+  rc= mysql_query(conn, "show variables like 'have_innodb'");
+  myquery(rc);
+  res= mysql_use_result(conn);
+  DIE_UNLESS(res);
+
+  row= mysql_fetch_row(res);
+  DIE_UNLESS(row);
+
+  result= strcmp(row[1], "YES") == 0;
+  mysql_free_result(res);
+  return result;
+}
+
 
 /*
   This is to be what mysql_query() is for mysql_real_query(), for
@@ -282,6 +305,7 @@
   strxmov(query, "USE ", current_db, NullS);
   rc= mysql_query(mysql, query);
   myquery(rc);
+  have_innodb= check_have_innodb(mysql);
 
   if (!opt_silent)
     fprintf(stdout, " OK");
@@ -13040,27 +13064,6 @@
     check_execute(stmt, rc);
     if (!opt_silent && i == 0)
       printf("Fetched row: %s\n", a);
-    /*
-      Although protocol-wise an attempt to execute a statement which
-      already has an open cursor associated with it will yield an error,
-      the client library behavior tested here is consistent with
-      the non-cursor execution scenario: mysql_stmt_execute will
-      silently close the cursor if necessary.
-    */
-    {
-      char buff[9];
-      /* Fill in the execute packet */
-      int4store(buff, stmt->stmt_id);
-      buff[4]= 0;                               /* Flag */
-      int4store(buff+5, 1);                     /* Reserved for array bind */
-      rc= ((*mysql->methods->advanced_command)(mysql, COM_STMT_EXECUTE, buff,
-                                               sizeof(buff), 0,0,1) ||
-           (*mysql->methods->read_query_result)(mysql));
-      DIE_UNLESS(rc);
-      if (!opt_silent && i == 0)
-        printf("Got error (as expected): %s\n", mysql_error(mysql));
-    }
-
     rc= mysql_stmt_execute(stmt);
     check_execute(stmt, rc);
 
@@ -13419,7 +13422,7 @@
   bind[1].length= &a_len;
   rc= mysql_stmt_bind_param(stmt, bind);
   check_execute(stmt, rc);
-  for (i= 0; i < 34; i++)
+  for (i= 0; i < 42; i++)
   {
     id_val= (i+1)*10;
     sprintf(a, "a%d", i);
@@ -13606,7 +13609,6 @@
 
 static void test_bug10214()
 {
-  MYSQL_RES* res ;
   int   len;
   char  out[8];
 
@@ -13627,6 +13629,111 @@
 }
 
 
+/* Bug#10760: cursors, crash in a fetch after rollback. */
+
+static void test_bug10760()
+{
+  MYSQL_STMT *stmt;
+  MYSQL_BIND bind[1];
+  int rc;
+  const char *stmt_text;
+  char id_buf[20];
+  ulong id_len;
+  int i= 0;
+  ulong type;
+
+  myheader("test_bug10760");
+
+  mysql_query(mysql, "drop table if exists t1, t2");
+
+  /* create tables */
+  rc= mysql_query(mysql, "create table t1 (id integer not null primary key)"
+                         " engine=MyISAM");
+  myquery(rc);
+  for (; i < 42; ++i)
+  {
+    char buf[100];
+    sprintf(buf, "insert into t1 (id) values (%d)", i+1);
+    rc= mysql_query(mysql, buf);
+    myquery(rc);
+  }
+  mysql_autocommit(mysql, FALSE);
+  /* create statement */
+  stmt= mysql_stmt_init(mysql);
+  type= (ulong) CURSOR_TYPE_READ_ONLY;
+  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
+
+  /*
+    1: check that a deadlock within the same connection
+    is resolved and an error is returned. The deadlock is modelled
+    as follows:
+    con1: open cursor for select * from t1;
+    con1: insert into t1 (id) values (1)
+  */
+  stmt_text= "select id from t1 order by 1";
+  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
+  check_execute(stmt, rc);
+  rc= mysql_stmt_execute(stmt);
+  check_execute(stmt, rc);
+  rc= mysql_query(mysql, "update t1 set id=id+100");
+  DIE_UNLESS(rc);
+  if (!opt_silent)
+    printf("Got error (as expected): %s\n", mysql_error(mysql));
+  /*
+    2: check that MyISAM tables used in cursors survive
+    COMMIT/ROLLBACK.
+  */
+  rc= mysql_rollback(mysql);                  /* should not close the cursor */
+  myquery(rc);
+  rc= mysql_stmt_fetch(stmt);
+  check_execute(stmt, rc);
+
+  /*
+    3: check that cursors to InnoDB tables are closed (for now) by
+    COMMIT/ROLLBACK.
+  */
+  if (! have_innodb)
+  {
+    if (!opt_silent)
+      printf("Testing that cursors are closed at COMMIT/ROLLBACK requires "
+             "InnoDB.\n");
+  }
+  else
+  {
+    stmt_text= "select id from t1 order by 1";
+    rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
+    check_execute(stmt, rc);
+
+    rc= mysql_query(mysql, "alter table t1 engine=InnoDB");
+    myquery(rc);
+
+    bzero(bind, sizeof(bind));
+    bind[0].buffer_type= MYSQL_TYPE_STRING;
+    bind[0].buffer= (void*) id_buf;
+    bind[0].buffer_length= sizeof(id_buf);
+    bind[0].length= &id_len;
+    check_execute(stmt, rc);
+    mysql_stmt_bind_result(stmt, bind);
+
+    rc= mysql_stmt_execute(stmt);
+    rc= mysql_stmt_fetch(stmt);
+    DIE_UNLESS(rc == 0);
+    if (!opt_silent)
+      printf("Fetched row %s\n", id_buf);
+    rc= mysql_rollback(mysql);                  /* should close the cursor */
+    myquery(rc);
+    rc= mysql_stmt_fetch(stmt);
+    DIE_UNLESS(rc);
+    if (!opt_silent)
+      printf("Got error (as expected): %s\n", mysql_error(mysql));
+  }
+
+  mysql_stmt_close(stmt);
+  rc= mysql_query(mysql, "drop table t1");
+  myquery(rc);
+  mysql_autocommit(mysql, TRUE);                /* restore default */
+}
+
 /*
   Read and parse arguments and MySQL options from my.cnf
 */
@@ -13867,6 +13974,7 @@
   { "test_bug11172", test_bug11172 },
   { "test_bug11656", test_bug11656 },
   { "test_bug10214", test_bug10214 },
+  { "test_bug10760", test_bug10760 },
   { 0, 0 }
 };
 
Thread
bk commit into 5.0 tree (konstantin:1.1890) BUG#10760konstantin11 Jul