From 4c63fba632c0aafd8e4661bdcc2cbaa533decbe1 Mon Sep 17 00:00:00 2001 From: Roowe Date: Thu, 30 Apr 2015 12:40:10 +0800 Subject: [PATCH 1/3] fix conn_test_period use app env conf --- include/emysql.hrl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/emysql.hrl b/include/emysql.hrl index 1320289f..0a8f9d8e 100644 --- a/include/emysql.hrl +++ b/include/emysql.hrl @@ -38,7 +38,7 @@ locked=gb_trees:empty() :: gb_tree(), waiting=queue:new() :: queue(), start_cmds=[] :: string(), - conn_test_period=0 :: number(), + conn_test_period=emysql_app:conn_test_period() :: number(), connect_timeout=infinity :: number() | infinity, warnings=false :: boolean()}). From 6fdc66dc834ba5e15d1c9e2fffd3a03c47477345 Mon Sep 17 00:00:00 2001 From: Roowe Date: Thu, 30 Apr 2015 15:43:09 +0800 Subject: [PATCH 2/3] reset conn when it is not alive --- src/emysql.erl | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/emysql.erl b/src/emysql.erl index a373de9a..76c0e683 100644 --- a/src/emysql.erl +++ b/src/emysql.erl @@ -746,12 +746,22 @@ as_record(Res, Recname, Fields, Fun) -> emysql_conv:as_record(Res, Recname, Fiel %% @end doc: hd feb 11 %% monitor_work(Connection0, Timeout, Args) when is_record(Connection0, emysql_connection) -> - Connection = case emysql_conn:need_test_connection(Connection0) of - true -> - emysql_conn:test_connection(Connection0, keep); - false -> - Connection0 - end, + Connection = if + Connection0#emysql_connection.alive =:= false -> + case emysql_conn:reset_connection(emysql_conn_mgr:pools(), Connection0, keep) of + NewConn when is_record(NewConn, emysql_connection) -> + NewConn; + {error, FailedReset0} -> + exit({connection_down, {and_conn_reset_failed, FailedReset0}}) + end; + true -> + case emysql_conn:need_test_connection(Connection0) of + true -> + emysql_conn:test_connection(Connection0, keep); + false -> + Connection0 + end + end, %% spawn a new process to do work, then monitor that process until %% it either dies, returns data or times out. From d93269dc2a18f8ba8d99df260603ea3aa19b2b5f Mon Sep 17 00:00:00 2001 From: Roowe Date: Thu, 16 Jul 2015 15:56:43 +0800 Subject: [PATCH 3/3] fix compile error R18 --- include/emysql.hrl | 6 +++--- src/emysql.erl | 2 +- src/emysql_conn.erl | 2 +- src/emysql_conn_mgr.erl | 14 +++++++++++--- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/include/emysql.hrl b/include/emysql.hrl index 0a8f9d8e..a9a02ed2 100644 --- a/include/emysql.hrl +++ b/include/emysql.hrl @@ -34,9 +34,9 @@ port :: number(), database :: string(), encoding :: utf8 | latin1 | {utf8, utf8_unicode_ci} | {utf8, utf8_general_ci}, - available=queue:new() :: queue(), - locked=gb_trees:empty() :: gb_tree(), - waiting=queue:new() :: queue(), + available=queue:new() :: queue:queue(), + locked=gb_trees:empty() :: gb_tree:tree(), + waiting=queue:new() :: queue:queue(), start_cmds=[] :: string(), conn_test_period=emysql_app:conn_test_period() :: number(), connect_timeout=infinity :: number() | infinity, diff --git a/src/emysql.erl b/src/emysql.erl index 76c0e683..0f6d0f66 100644 --- a/src/emysql.erl +++ b/src/emysql.erl @@ -669,7 +669,7 @@ result_type(#eof_packet{}) -> eof. -spec as_dict(Result) -> Dict when Result :: #result_packet{}, - Dict :: dict(). + Dict :: dict:dict(). as_dict(Res) -> emysql_conv:as_dict(Res). diff --git a/src/emysql_conn.erl b/src/emysql_conn.erl index 01a5c74b..595c41d6 100644 --- a/src/emysql_conn.erl +++ b/src/emysql_conn.erl @@ -334,7 +334,7 @@ need_test_connection(Conn) -> (Conn#emysql_connection.last_test_time + Conn#emysql_connection.test_period < now_seconds()). now_seconds() -> - {M, S, _} = erlang:now(), + {M, S, _} = os:timestamp(), M * 1000000 + S. %%-------------------------------------------------------------------- diff --git a/src/emysql_conn_mgr.erl b/src/emysql_conn_mgr.erl index b14a08d6..5a4c5212 100644 --- a/src/emysql_conn_mgr.erl +++ b/src/emysql_conn_mgr.erl @@ -40,7 +40,7 @@ -include("emysql.hrl"). --record(state, {pools, lockers = dict:new() :: dict()}). +-record(state, {pools, lockers = dict:new() :: dict:dict()}). %%==================================================================== %% API @@ -419,8 +419,16 @@ lock_next_connection(Available ,Locked, Who) -> end. connection_locked_at(Conn, MonitorRef) -> - Conn#emysql_connection{locked_at=lists:nth(2, tuple_to_list(now())), - monitor_ref = MonitorRef}. + Conn#emysql_connection{ + locked_at = case catch erlang:unique_integer() of + {'EXIT', _} -> + lists:nth(2, tuple_to_list(apply(erlang, now, []))); + V -> + V + end, + monitor_ref = MonitorRef + }. + serve_waiting_pids(Pool) -> {Waiting, Available, Locked, NewRefs} = serve_waiting_pids(Pool#pool.waiting, Pool#pool.available, Pool#pool.locked, []),