15
15
import java .sql .*;
16
16
import java .util .LinkedList ;
17
17
import java .util .List ;
18
+ import java .util .concurrent .locks .Lock ;
19
+ import java .util .concurrent .locks .ReentrantLock ;
18
20
import java .util .function .Function ;
19
21
import java .util .stream .Collectors ;
20
22
28
30
* @since 5.4, 5.3.7
29
31
*/
30
32
public class JDBC_PING2 extends FILE_PING {
33
+ protected final Lock lock =new ReentrantLock ();
31
34
32
35
/* ----------------------------------------- Properties -------------------------------------------------- */
33
36
@@ -169,14 +172,17 @@ protected void write(List<PingData> list, String clustername) {
169
172
// This synchronization should not be a performance problem as this is just a Discovery protocol.
170
173
// Many SQL dialects have some "insert or update" expression, but that would need
171
174
// additional configuration and testing on each database. See JGRP-1440
172
- protected synchronized void writeToDB (PingData data , String clustername ) throws SQLException {
175
+ protected void writeToDB (PingData data , String clustername ) throws SQLException {
176
+ lock .lock ();
173
177
try (Connection connection =getConnection ()) {
174
178
if (call_insert_sp != null && insert_sp != null )
175
179
callInsertStoredProcedure (connection , data , clustername );
176
180
else {
177
181
delete (connection , clustername , data .getAddress ());
178
182
insert (connection , data , clustername );
179
183
}
184
+ } finally {
185
+ lock .unlock ();
180
186
}
181
187
}
182
188
@@ -309,7 +315,8 @@ protected Connection getConnection() throws SQLException {
309
315
DriverManager .getConnection (connection_url , connection_username , connection_password );
310
316
}
311
317
312
- protected synchronized void insert (Connection connection , PingData data , String clustername ) throws SQLException {
318
+ protected void insert (Connection connection , PingData data , String clustername ) throws SQLException {
319
+ lock .lock ();
313
320
try (PreparedStatement ps =connection .prepareStatement (insert_single_sql )) {
314
321
Address address =data .getAddress ();
315
322
String addr =Util .addressToString (address );
@@ -325,10 +332,13 @@ protected synchronized void insert(Connection connection, PingData data, String
325
332
log .trace ("%s: SQL for insertion: %s" , local_addr , ps );
326
333
ps .executeUpdate ();
327
334
log .debug ("%s: inserted %s for cluster %s" , local_addr , address , clustername );
335
+ } finally {
336
+ lock .unlock ();
328
337
}
329
338
}
330
339
331
- protected synchronized void callInsertStoredProcedure (Connection connection , PingData data , String clustername ) throws SQLException {
340
+ protected void callInsertStoredProcedure (Connection connection , PingData data , String clustername ) throws SQLException {
341
+ lock .lock ();
332
342
try (PreparedStatement ps =connection .prepareStatement (call_insert_sp )) {
333
343
Address address =data .getAddress ();
334
344
String addr =Util .addressToString (address );
@@ -344,17 +354,22 @@ protected synchronized void callInsertStoredProcedure(Connection connection, Pin
344
354
log .trace ("%s: SQL for insertion: %s" , local_addr , ps );
345
355
ps .executeUpdate ();
346
356
log .debug ("%s: inserted %s for cluster %s" , local_addr , address , clustername );
357
+ } finally {
358
+ lock .lock ();
347
359
}
348
360
}
349
361
350
- protected synchronized void delete (Connection conn , String clustername , Address addressToDelete ) throws SQLException {
362
+ protected void delete (Connection conn , String clustername , Address addressToDelete ) throws SQLException {
363
+ lock .lock ();
351
364
try (PreparedStatement ps =conn .prepareStatement (delete_single_sql )) {
352
365
String addr =Util .addressToString (addressToDelete );
353
366
ps .setString (1 , addr );
354
367
if (log .isTraceEnabled ())
355
368
log .trace ("%s: SQL for deletion: %s" , local_addr , ps );
356
369
ps .executeUpdate ();
357
370
log .debug ("%s: removed %s for cluster %s from database" , local_addr , addressToDelete , clustername );
371
+ } finally {
372
+ lock .unlock ();
358
373
}
359
374
}
360
375
0 commit comments