Thursday, August 18, 2011

EJB3.1; Caching with session facade using @Singleton


@Singleton in the default configuration is the perfect bottleneck. Only one thread a time can access a @Singleton instance. 

The annotation @ConcurrencyManagement(ConcurrencyManagementType.BEAN) deactivates this limitation and makes a Singleton instance accessible from multiple threads:

import javax.ejb.Singleton;
import javax.annotation.PostConstruct;
import javax.ejb.ConcurrencyManagement;
import java.util.concurrent.ConcurrentHashMap;
import javax.ejb.ConcurrencyManagementType;

@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class Hits {

    private ConcurrentHashMap hits = null;
 
 @PostConstruct
    public void initialize() {
        this.hits = new ConcurrentHashMap();
 }
//cache accessors omitted
}


java.util.concurrent.ConcurrentHashMap can be accessed by multiple threads consistently without any locking or synchronization required.


uses a ConcurrentHashMap for deferred writes and is able to handle 1.400 writes/transactions a second.



No comments :

Post a Comment