diff --git a/innobase/include/row0mysql.h b/innobase/include/row0mysql.h
index 8152c534f4859f43fd71fd2018664d25e626e427..75c163844589a4f67b2409956b87c113fda72e06 100644
--- a/innobase/include/row0mysql.h
+++ b/innobase/include/row0mysql.h
@@ -393,6 +393,7 @@ struct row_prebuilt_struct {
 					an SQL statement: we may have to set
 					an intention lock on the table,
 					create a consistent read view etc. */
+        ibool           mysql_has_locked;
 	ibool		clust_index_was_generated;
 					/* if the user did not define a
 					primary key in MySQL, then Innobase
diff --git a/innobase/row/row0mysql.c b/innobase/row/row0mysql.c
index ebb3cbe8dc8b36939bdf4530a628068fad93a8ea..705ded785fca63545076b36a86ea760ebfd1197b 100644
--- a/innobase/row/row0mysql.c
+++ b/innobase/row/row0mysql.c
@@ -320,6 +320,7 @@ row_create_prebuilt(
 	prebuilt->trx = NULL;
 
 	prebuilt->sql_stat_start = TRUE;
+	prebuilt->mysql_has_locked = FALSE;
 
 	prebuilt->index = NULL;
 	prebuilt->n_template = 0;
diff --git a/sql/ha_innobase.cc b/sql/ha_innobase.cc
index 6b5ba7d841e84c2bef7a942e69c2c44d7357cede..2f79f8d6ba9b7cb24b2e5da893705f0e5e685d65 100644
--- a/sql/ha_innobase.cc
+++ b/sql/ha_innobase.cc
@@ -3453,7 +3453,7 @@ ha_innobase::start_stmt(
   prebuilt->hint_no_need_to_fetch_extra_cols = TRUE;
   prebuilt->read_just_key = 0;
 
-  if (prebuilt->select_lock_type == LOCK_NONE) {
+  if (!prebuilt->mysql_has_locked) {
           /* This handle is for a temporary table created inside
           this same LOCK TABLES; since MySQL does NOT call external_lock
           in this case, we must use x-row locks inside InnoDB to be
@@ -3510,6 +3510,7 @@ ha_innobase::external_lock(
 
 		thd->transaction.all.innodb_active_trans = 1;
 		trx->n_mysql_tables_in_use++;
+		prebuilt->mysql_has_locked = TRUE;
 
 		if (thd->tx_isolation == ISO_SERIALIZABLE
 		    && prebuilt->select_lock_type == LOCK_NONE) {
@@ -3527,6 +3528,7 @@ ha_innobase::external_lock(
 		}
 	} else {
 		trx->n_mysql_tables_in_use--;
+		prebuilt->mysql_has_locked = FALSE;
 		auto_inc_counter_for_this_stat = 0;
 
 		if (trx->n_mysql_tables_in_use == 0) {