@@ -233,7 +233,7 @@ void decr_mysql2_client(mysql_client_wrapper *wrapper)
233233
234234 if (wrapper -> refcount == 0 ) {
235235#ifndef _WIN32
236- if (wrapper -> connected ) {
236+ if (wrapper -> connected && ! wrapper -> automatic_close ) {
237237 /* The client is being garbage collected while connected. Prevent
238238 * mysql_close() from sending a mysql-QUIT or from calling shutdown() on
239239 * the socket by invalidating it. invalidate_fd() will drop this
@@ -260,6 +260,11 @@ static VALUE allocate(VALUE klass) {
260260 obj = Data_Make_Struct (klass , mysql_client_wrapper , rb_mysql_client_mark , rb_mysql_client_free , wrapper );
261261 wrapper -> encoding = Qnil ;
262262 MARK_CONN_INACTIVE (self );
263+ #ifndef _WIN32
264+ wrapper -> automatic_close = 0 ;
265+ #else
266+ wrapper -> automatic_close = 1 ;
267+ #endif
263268 wrapper -> server_version = 0 ;
264269 wrapper -> reconnect_enabled = 0 ;
265270 wrapper -> connect_timeout = 0 ;
@@ -382,12 +387,9 @@ static VALUE rb_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE po
382387
383388/*
384389 * Terminate the connection; call this when the connection is no longer needed.
385- * The garbage collector can close the connection, but doing so emits an
386- * "Aborted connection" error on the server and increments the Aborted_clients
387- * status variable.
390+ * To have the garbage collector close the connection, enable +automatic_close+.
388391 *
389- * @see http://dev.mysql.com/doc/en/communication-errors.html
390- * @return [void]
392+ * @return [nil]
391393 */
392394static VALUE rb_mysql_client_close (VALUE self ) {
393395 GET_CLIENT (self );
@@ -1085,6 +1087,37 @@ static VALUE rb_mysql_client_encoding(VALUE self) {
10851087}
10861088#endif
10871089
1090+ /* call-seq:
1091+ * client.automatic_close?
1092+ *
1093+ * @return [Boolean]
1094+ */
1095+ static VALUE get_automatic_close (VALUE self ) {
1096+ GET_CLIENT (self );
1097+ return wrapper -> automatic_close ? Qtrue : Qfalse ;
1098+ }
1099+
1100+ /* call-seq:
1101+ * client.automatic_close = true
1102+ *
1103+ * Set this to +true+ to let the garbage collector close this connection.
1104+ */
1105+ static VALUE set_automatic_close (VALUE self , VALUE value ) {
1106+ GET_CLIENT (self );
1107+ if (RTEST (value )) {
1108+ value = Qtrue ;
1109+ wrapper -> automatic_close = 1 ;
1110+ } else {
1111+ #ifndef _WIN32
1112+ value = Qfalse ;
1113+ wrapper -> automatic_close = 0 ;
1114+ #else
1115+ rb_raise (cMysql2Error , "Connections are always closed by garbage collector on Windows" );
1116+ #endif
1117+ }
1118+ return value ;
1119+ }
1120+
10881121/* call-seq:
10891122 * client.reconnect = true
10901123 *
@@ -1268,6 +1301,8 @@ void init_mysql2_client() {
12681301 rb_define_method (cMysql2Client , "more_results?" , rb_mysql_client_more_results , 0 );
12691302 rb_define_method (cMysql2Client , "next_result" , rb_mysql_client_next_result , 0 );
12701303 rb_define_method (cMysql2Client , "store_result" , rb_mysql_client_store_result , 0 );
1304+ rb_define_method (cMysql2Client , "automatic_close?" , get_automatic_close , 0 );
1305+ rb_define_method (cMysql2Client , "automatic_close=" , set_automatic_close , 1 );
12711306 rb_define_method (cMysql2Client , "reconnect=" , set_reconnect , 1 );
12721307 rb_define_method (cMysql2Client , "warning_count" , rb_mysql_client_warning_count , 0 );
12731308 rb_define_method (cMysql2Client , "query_info_string" , rb_mysql_info , 0 );
0 commit comments