19
19
select_updates_between /2 ,
20
20
get_enabled_dynamic /0 ,
21
21
delete_events_older_than /1 ,
22
- insert_dummy_event /1 ]).
22
+ insert_dummy_events /1 ]).
23
23
24
24
% % interfaces only for integration tests
25
25
-export ([prepare_test_queries /0 ,
30
30
-ignore_xref ([erase_database / 1 , prepare_test_queries / 0 , get_enabled_dynamic / 0 ,
31
31
insert_full_event / 2 , insert_domain_settings_without_event / 2 ]).
32
32
33
- -import (mongoose_rdbms , [prepare /4 , execute_successfully /3 ]).
33
+ -import (mongoose_rdbms , [prepare /4 ]).
34
+ -include (" mongoose_logger.hrl" ).
34
35
35
36
-type event_id () :: non_neg_integer ().
36
37
-type domain () :: jid :lserver ().
@@ -258,19 +259,24 @@ delete_events_older_than(Id) ->
258
259
execute_successfully (Pool , domain_events_delete_older_than , [Id ])
259
260
end ).
260
261
261
- insert_dummy_event ( EventId ) ->
262
- insert_full_event ( EventId , <<>>).
262
+ insert_dummy_events ( EventIds ) ->
263
+ insert_full_events ([{ EventId , <<>>} || EventId <- EventIds ] ).
263
264
264
265
insert_full_event (EventId , Domain ) ->
266
+ [Res ] = insert_full_events ([{EventId , Domain }]),
267
+ Res .
268
+
269
+ insert_full_events (EventIdDomain ) ->
265
270
case mongoose_rdbms :db_type () of
266
271
mssql ->
267
- insert_full_event_mssql ( EventId , Domain );
272
+ insert_full_events_mssql ( EventIdDomain );
268
273
_ ->
269
274
Pool = get_db_pool (),
270
- execute_successfully (Pool , domain_insert_full_event , [EventId , Domain ])
275
+ [catch execute_successfully (Pool , domain_insert_full_event , [EventId , Domain ])
276
+ || {EventId , Domain } <- EventIdDomain ]
271
277
end .
272
278
273
- insert_full_event_mssql ( EventId , Domain ) ->
279
+ insert_full_events_mssql ( EventIdDomain ) ->
274
280
% % MSSQL does not allow to specify ids,
275
281
% % that are supposed to be autoincremented, easily
276
282
% % https://docs.microsoft.com/pl-pl/sql/t-sql/statements/set-identity-insert-transact-sql
@@ -281,7 +287,8 @@ insert_full_event_mssql(EventId, Domain) ->
281
287
% % when trying to execute
282
288
mongoose_rdbms :sql_query (Pool , <<" SET IDENTITY_INSERT domain_events ON" >>),
283
289
try
284
- execute_successfully (Pool , domain_insert_full_event , [EventId , Domain ])
290
+ [catch execute_successfully (Pool , domain_insert_full_event , [EventId , Domain ])
291
+ || {EventId , Domain } <- EventIdDomain ]
285
292
after
286
293
mongoose_rdbms :sql_query (Pool , <<" SET IDENTITY_INSERT domain_events OFF" >>)
287
294
end
@@ -372,16 +379,26 @@ transaction(F) ->
372
379
% % (there is no logic, that would suffer by a restart of a transaction).
373
380
transaction (_F , 0 , Errors ) ->
374
381
{error , {db_error , Errors }};
375
- transaction (F , Retries , Errors ) when Retries > 0 ->
382
+ transaction (F , Tries , Errors ) when Tries > 0 ->
376
383
Pool = get_db_pool (),
377
384
Result = rdbms_queries :sql_transaction (Pool , fun () -> F (Pool ) end ),
378
385
case Result of
379
386
{aborted , _ } -> % % Restart any rolled back transaction
387
+ put (debug_last_transaction_error , Result ),
380
388
timer :sleep (100 ), % % Small break before retry
381
- transaction (F , Retries - 1 , [Result |Errors ]);
389
+ transaction (F , Tries - 1 , [Result |Errors ]);
382
390
_ ->
391
+ erase (debug_last_transaction_error ),
383
392
simple_result (Result )
384
393
end .
385
394
386
395
simple_result ({atomic , Result }) -> Result ;
387
396
simple_result (Other ) -> {error , {db_error , Other }}.
397
+
398
+ execute_successfully (Pool , StatementName , Args ) ->
399
+ {Time , Res } = timer :tc (fun () -> mongoose_rdbms :execute_successfully (Pool , StatementName , Args ) end ),
400
+ % % Convert args to tuple, because Erlang formats list as a string for domain_event_ids_between
401
+ ? LOG_DEBUG (#{what => domain_sql_execute ,
402
+ statement_name => StatementName , args => list_to_tuple (Args ), result => Res ,
403
+ duration => round (Time / 1000 )}),
404
+ Res .
0 commit comments