Skip to content

Commit 3d8acad

Browse files
author
Alex Saluk
committed
synchronizing on the same object
1 parent 847e4dd commit 3d8acad

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

javalite-async/src/main/java/org/javalite/async/SessionPool.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
*/
2121
public class SessionPool {
2222

23-
private Logger LOGGER;
23+
private final Logger LOGGER;
2424

2525
private static final int MAX_AGE_MILLIS = 5000;
2626

2727
private final LinkedList<PooledSession> sessions = new LinkedList<>();
28-
private Connection connection;
28+
private final Connection connection;
2929
private boolean closed = false;
30-
private SessionCleaner sessionCleaner = new SessionCleaner();
30+
private final SessionCleaner sessionCleaner = new SessionCleaner();
3131

3232

3333
public SessionPool(String name, Connection connection) {
@@ -58,14 +58,16 @@ public Session getSession() throws JMSException {
5858
/**
5959
* Closes all underlying JMS sessions.
6060
*/
61-
public synchronized void close() {
62-
closed = true;
63-
sessionCleaner.close();
64-
sessions.stream().forEach(PooledSession::reallyClose);
61+
public void close() {
62+
synchronized (sessions) {
63+
closed = true;
64+
sessionCleaner.close();
65+
sessions.stream().forEach(PooledSession::reallyClose);
66+
}
6567
}
6668

6769

68-
public void reclaim(PooledSession session) {
70+
void reclaim(PooledSession session) {
6971
session.markLastUsedTime();
7072
synchronized (sessions){
7173
sessions.add(session);
@@ -118,9 +120,11 @@ protected void close(){
118120
cleanupExecutor.shutdown();
119121
}
120122

121-
protected synchronized void start(){
122-
cleanupExecutor.scheduleAtFixedRate(command, 5, 5, TimeUnit.SECONDS);
123-
LOGGER.info("Starting to clean stale sessions...");
123+
protected void start() {
124+
synchronized (sessions) {
125+
cleanupExecutor.scheduleAtFixedRate(command, 5, 5, TimeUnit.SECONDS);
126+
LOGGER.info("Starting to clean stale sessions...");
127+
}
124128
}
125129
}
126130
}

0 commit comments

Comments
 (0)