Also an improvement to that would be using some kind of LRU or weak/soft reference map with concurrent behavior, so that the ids map does not cause a memory leak as it fills with data. Java's ConcurrentHashMap doesn't have the LRU/weak/soft reference ability. If you have a big database, you may need to use Google Guava library to build a map with the desired behavior:
http://code.google.com/p/guava-libraries/ http://docs.guava-libraries.googlecode.com/git-history/v9.0/javadoc/com/google/common/collect/MapMaker.html I haven't tried that myself, but there are lots of people who seem to be very happy with it. Andrus On Sep 9, 2011, at 9:01 AM, Andrus Adamchik wrote: > class PerObjectLockManager { > > private ConcurrentMap<ObjectId, ObjectId > ids = ... > > Object objectLock(Persistent object) { > > ObjectId newId = object.getObjectId(); > ObjectId existingId = ids.putIfAbsent(newId, newId); > return existing != null ? existingId : newId; > } > }
