diff --git a/include/emysql.hrl b/include/emysql.hrl index 1320289f..a9a02ed2 100644 --- a/include/emysql.hrl +++ b/include/emysql.hrl @@ -34,11 +34,11 @@ 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=0 :: number(), + conn_test_period=emysql_app:conn_test_period() :: number(), connect_timeout=infinity :: number() | infinity, warnings=false :: boolean()}). diff --git a/src/emysql.erl b/src/emysql.erl index a373de9a..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). @@ -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. 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, []),