Skip to content

Commit 755ec4b

Browse files
committed
Avoiding synchronized as it would pin the threads when using virtual threads
JGRP-2861
1 parent 930bd20 commit 755ec4b

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/org/jgroups/protocols/JDBC_PING2.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.sql.*;
1616
import java.util.LinkedList;
1717
import java.util.List;
18+
import java.util.concurrent.locks.Lock;
19+
import java.util.concurrent.locks.ReentrantLock;
1820
import java.util.function.Function;
1921
import java.util.stream.Collectors;
2022

@@ -28,6 +30,7 @@
2830
* @since 5.4, 5.3.7
2931
*/
3032
public class JDBC_PING2 extends FILE_PING {
33+
protected final Lock lock=new ReentrantLock();
3134

3235
/* ----------------------------------------- Properties -------------------------------------------------- */
3336

@@ -169,14 +172,17 @@ protected void write(List<PingData> list, String clustername) {
169172
// This synchronization should not be a performance problem as this is just a Discovery protocol.
170173
// Many SQL dialects have some "insert or update" expression, but that would need
171174
// 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();
173177
try(Connection connection=getConnection()) {
174178
if(call_insert_sp != null && insert_sp != null)
175179
callInsertStoredProcedure(connection, data, clustername);
176180
else {
177181
delete(connection, clustername, data.getAddress());
178182
insert(connection, data, clustername);
179183
}
184+
} finally {
185+
lock.unlock();
180186
}
181187
}
182188

@@ -309,7 +315,8 @@ protected Connection getConnection() throws SQLException {
309315
DriverManager.getConnection(connection_url, connection_username, connection_password);
310316
}
311317

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();
313320
try(PreparedStatement ps=connection.prepareStatement(insert_single_sql)) {
314321
Address address=data.getAddress();
315322
String addr=Util.addressToString(address);
@@ -325,10 +332,13 @@ protected synchronized void insert(Connection connection, PingData data, String
325332
log.trace("%s: SQL for insertion: %s", local_addr, ps);
326333
ps.executeUpdate();
327334
log.debug("%s: inserted %s for cluster %s", local_addr, address, clustername);
335+
} finally {
336+
lock.unlock();
328337
}
329338
}
330339

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();
332342
try(PreparedStatement ps=connection.prepareStatement(call_insert_sp)) {
333343
Address address=data.getAddress();
334344
String addr=Util.addressToString(address);
@@ -344,17 +354,22 @@ protected synchronized void callInsertStoredProcedure(Connection connection, Pin
344354
log.trace("%s: SQL for insertion: %s", local_addr, ps);
345355
ps.executeUpdate();
346356
log.debug("%s: inserted %s for cluster %s", local_addr, address, clustername);
357+
} finally {
358+
lock.lock();
347359
}
348360
}
349361

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();
351364
try(PreparedStatement ps=conn.prepareStatement(delete_single_sql)) {
352365
String addr=Util.addressToString(addressToDelete);
353366
ps.setString(1, addr);
354367
if(log.isTraceEnabled())
355368
log.trace("%s: SQL for deletion: %s", local_addr, ps);
356369
ps.executeUpdate();
357370
log.debug("%s: removed %s for cluster %s from database", local_addr, addressToDelete, clustername);
371+
} finally {
372+
lock.unlock();
358373
}
359374
}
360375

0 commit comments

Comments
 (0)