@@ -36,9 +36,6 @@ static VALUE rb_hash_dup(VALUE other) {
36
36
rb_raise(cMysql2Error, "MySQL connection is already open"); \
37
37
}
38
38
39
- #define MARK_CONN_INACTIVE (conn ) \
40
- wrapper->active_thread = Qnil;
41
-
42
39
/*
43
40
* compatability with mysql-connector-c, where LIBMYSQL_VERSION is the correct
44
41
* variable to use, but MYSQL_SERVER_VERSION gives the correct numbers when
@@ -407,7 +404,7 @@ static VALUE do_send_query(void *args) {
407
404
mysql_client_wrapper * wrapper = query_args -> wrapper ;
408
405
if ((VALUE )rb_thread_call_without_gvl (nogvl_send_query , args , RUBY_UBF_IO , 0 ) == Qfalse ) {
409
406
/* an error occurred, we're not active anymore */
410
- MARK_CONN_INACTIVE ( self ) ;
407
+ wrapper -> active_thread = Qnil ;
411
408
return rb_raise_mysql2_error (wrapper );
412
409
}
413
410
return Qnil ;
@@ -590,6 +587,25 @@ static VALUE finish_and_mark_inactive(void *args) {
590
587
}
591
588
#endif
592
589
590
+ void rb_mysql_client_set_active_thread (VALUE self ) {
591
+ VALUE thread_current = rb_thread_current ();
592
+ GET_CLIENT (self );
593
+
594
+ // see if this connection is still waiting on a result from a previous query
595
+ if (NIL_P (wrapper -> active_thread )) {
596
+ // mark this connection active
597
+ wrapper -> active_thread = thread_current ;
598
+ } else if (wrapper -> active_thread == thread_current ) {
599
+ rb_raise (cMysql2Error , "This connection is still waiting for a result, try again once you have the result" );
600
+ } else {
601
+ VALUE inspect = rb_inspect (wrapper -> active_thread );
602
+ const char * thr = StringValueCStr (inspect );
603
+
604
+ rb_raise (cMysql2Error , "This connection is in use by: %s" , thr );
605
+ RB_GC_GUARD (inspect );
606
+ }
607
+ }
608
+
593
609
/* call-seq:
594
610
* client.abandon_results!
595
611
*
@@ -633,7 +649,6 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) {
633
649
struct nogvl_send_query_args args ;
634
650
int async = 0 ;
635
651
VALUE opts , current ;
636
- VALUE thread_current = rb_thread_current ();
637
652
#ifdef HAVE_RUBY_ENCODING_H
638
653
rb_encoding * conn_enc ;
639
654
#endif
@@ -663,23 +678,10 @@ static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) {
663
678
#endif
664
679
args .sql_ptr = StringValuePtr (args .sql );
665
680
args .sql_len = RSTRING_LEN (args .sql );
666
-
667
- /* see if this connection is still waiting on a result from a previous query */
668
- if (NIL_P (wrapper -> active_thread )) {
669
- /* mark this connection active */
670
- wrapper -> active_thread = thread_current ;
671
- } else if (wrapper -> active_thread == thread_current ) {
672
- rb_raise (cMysql2Error , "This connection is still waiting for a result, try again once you have the result" );
673
- } else {
674
- VALUE inspect = rb_inspect (wrapper -> active_thread );
675
- const char * thr = StringValueCStr (inspect );
676
-
677
- rb_raise (cMysql2Error , "This connection is in use by: %s" , thr );
678
- RB_GC_GUARD (inspect );
679
- }
680
-
681
681
args .wrapper = wrapper ;
682
682
683
+ rb_mysql_client_set_active_thread (self );
684
+
683
685
#ifndef _WIN32
684
686
rb_rescue2 (do_send_query , (VALUE )& args , disconnect_and_raise , self , rb_eException , (VALUE )0 );
685
687
0 commit comments