Introduce Mysql#reconnected?

This commit is contained in:
Lourens Naude 2008-10-08 17:09:47 +01:00
parent f57c2a6576
commit 8e6f300f9d
3 changed files with 29 additions and 2 deletions

View File

@ -851,6 +851,14 @@ static VALUE interrupted( VALUE obj )
return ( vio_was_interrupted( m->net.vio ) == 1 ? Qtrue : Qfalse );
}
/* reconnected */
static VALUE reconnected( VALUE obj ){
MYSQL* m = GetHandler(obj);
int current_connection_id = mysql_thread_id( m );
mysql_ping(m);
return ( current_connection_id == mysql_thread_id( m ) ) ? Qfalse : Qtrue;
}
static void validate_async_query( VALUE obj )
{
MYSQL* m = GetHandler(obj);
@ -2240,6 +2248,7 @@ void Init_mysql(void)
rb_define_method(cMysql, "async_in_progress=", async_in_progress_set, 1);
rb_define_method(cMysql, "send_query", send_query, 1);
rb_define_method(cMysql, "simulate_disconnect", simulate_disconnect, 0);
rb_define_method(cMysql, "reconnected?", reconnected, 0);
rb_define_method(cMysql, "get_result", get_result, 0);
rb_define_method(cMysql, "readable?", readable, -1);
rb_define_method(cMysql, "retry?", retry, 0);

View File

@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/test_helper'
m = Mysql.real_connect('localhost','root')
m = Mysql.real_connect('localhost','root','','mysql')
m.c_async_query( 'SELECT SLEEP(1)' ) do |result|
m.c_async_query( 'SELECT * FROM user' ) do |result|
puts result.inspect
end

18
test/reconnected_test.rb Normal file
View File

@ -0,0 +1,18 @@
require File.dirname(__FILE__) + '/test_helper'
$m = Mysql.real_connect('localhost','root')
#$m.reconnect = true
def assert_reconnected
puts $m.reconnected?().inspect
sleep 1
yield
puts $m.reconnected?().inspect
end
assert_reconnected do
$m.simulate_disconnect
end
assert_reconnected do
$m.close
end