How do I keep Glassfish from creating a new session for each request?
Glassfish 3.1.1 Jersey 1.8
I have the following stateless session bean exposed as a JAX-RS resource. VehicleResource is contained in a WAR, with the other EJBs in an EJB-JAR. The entire app is packaged and deployed as an EAR.
@Stateless
@Path("/")
@DeclareRoles({"production"})
public class VehicleResource {
private static final Logger logger = Logger.getLogger(VehicleResource.class.getName());
private static final long serialVersionUID = 1L;
@Context
private UriInfo uriInfo;
@EJB
private VehicleManagementLocal vehicles;
@EJB
private VehicleAliases aliases;
....
I have a Jersey Client making requests to the restful service. A session is created for each request. Is this normal/expected? With many requests, the server quickly runs out of memory. Why is this happening, and is there any configuration/code changes I can make to prevent a new session per request?
Edit Actuall开发者_如何转开发y, only one instance of the session bean is created. I am simulating a load by repeatedly hitting a URI. I have changed the session timeout to 5 minutes (from 30). The sessions now expire and can be garbage collected fast enough to keep the server from running out of memory. "Web Container: Session Statistics" in glassfish monitoring section shows I have ~50,000 active sessions.
A session is created for each request
Do you mean a new instance of the stateless session bean is created for each request? If so, and if you don't want this to happen, you can annotate your session bean with @Singleton
.
Also if the exposed service is read only, you can annotate it with @javax.ejb.Lock(READ)
to improve concurrency and performance.
Finally, if you don't want use singleton but want to limit the number of instances the container creates for you have a look at the pool size settings: http://download.oracle.com/docs/cd/E18930_01/html/821-2418/beahx.html
精彩评论