Skip to content

Commit

Permalink
ATS-12
Browse files Browse the repository at this point in the history
  • Loading branch information
madness-inc committed Aug 23, 2019
1 parent 232cc3a commit 49a49a2
Showing 1 changed file with 13 additions and 4 deletions.
Expand Up @@ -19,7 +19,6 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;

import org.apache.catalina.Session;
import org.apache.catalina.Store;
Expand All @@ -37,6 +36,7 @@
import com.hazelcast.config.NetworkConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.core.ReplicatedMap;

/**
Expand Down Expand Up @@ -158,7 +158,7 @@ public void save(Session session) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
((StandardSession) session).writeObjectData(oos);
getSessions().put(session.getId(), bos.toByteArray());
getSessions().set(session.getId(), bos.toByteArray());
log.debug("saved: " + session.getId());
}
}
Expand All @@ -180,6 +180,15 @@ public StandardSession load(String id) throws ClassNotFoundException, IOExceptio
StandardSession session = (StandardSession) this.manager.createEmptySession();
session.readObjectData(ois);
log.debug("loaded: " + id);

// pessimistic lock block to prevent concurrency problems whilst finding sessions
getSessions().lock(id);
try {
getSessions().remove(id);
getSessions().set(id, data);
} finally {
getSessions().unlock(id);
}
return session;
}
}
Expand All @@ -201,8 +210,8 @@ public void clear() throws IOException {
getSessions().clear();
}

private Map<String, byte[]> getSessions() {
return instance.getReplicatedMap(getManager().getName() + mapName);
private IMap<String, byte[]> getSessions() {
return instance.getMap(getManager().getName() + mapName);
}

// getters and setters
Expand Down

0 comments on commit 49a49a2

Please sign in to comment.