Too many active sessions

I use JSF 2 on JBoss 6. I made an example application that uses JSF as View, EJB for logic and JPA for Persistence. JSF bean has RequestScoped. EJB is stateless:

<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:c="http://java.sun.com/jsp/jstl/core"> <h:head> <title>Long HDi</title> </h:head> <h:body> <c:forEach var="tweet" items="#{tweets.getAll}"> <p> #{tweet.content}<br /> </p> </c:forEach> </h:body> </html> 

Bean:

 @Named("tweets") @RequestScoped public class Tweets implements Serializable { @EJB private TweetServiceLocal tweetService; private List<Tweet> tweets; public List<Tweet> getGetAll() { return tweetService.findAllSortedByTimeDesc(); } } 

EJB:

 @Stateless public class TweetService implements TweetServiceLocal { @PersistenceContext(unitName = "LongHDi-ejbPU") private EntityManager em; @Override public Tweet create(final String content, final Date postTime) throws ContentTooLargeException { if (content.length() > 480) throw new ContentTooLargeException("Content must have less than 480 charaters!"); else { try { Tweet tweet = new Tweet(); tweet.setContent(content); tweet.setPostTime(postTime); em.persist(tweet); return tweet; } catch (Exception e) { return null; } } } @Override public java.util.List<Tweet> findAllSortedByTimeDesc() { return em.createNamedQuery("Tweet.findAllSortedByTimeDesc").getResultList(); } } 

When I send several hundred requests to it, the JBoss 6 server throws this exception:

 JBWEB000065: HTTP Status 500 - JBWEB000209: Session creation failed due to too many active sessions JBWEB000309: type JBWEB000066: Exception report JBWEB000068: message JBWEB000209: Session creation failed due to too many active sessions JBWEB000069: description JBWEB000145: The server encountered an internal error that prevented it from fulfilling this request. JBWEB000070: exception javax.servlet.ServletException: JBWEB000209: Session creation failed due to too many active sessions javax.faces.webapp.FacesServlet.service(FacesServlet.java:606) JBWEB000071: root cause java.lang.IllegalStateException: JBWEB000209: Session creation failed due to too many active sessions org.apache.catalina.session.StandardManager.createSession(StandardManager.java:297) org.apache.catalina.connector.Request.doGetSession(Request.java:2651) org.apache.catalina.connector.Request.getSession(Request.java:2357) org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:790) com.sun.faces.context.ExternalContextImpl.getSession(ExternalContextImpl.java:157) com.sun.faces.application.view.FaceletViewHandlingStrategy.getSession(FaceletViewHandlingStrategy.java:494) com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:400) com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:124) javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:286) com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) javax.faces.webapp.FacesServlet.service(FacesServlet.java:594) 

My question is: why does the server create too many sessions? I used RequestScope and Stateless bean, how do they end in session? What can I do to overcome this situation? If I use only servlet and JSP when there are too many requests, the server slows down, but at least it does not stop like this.

+4
source share
1 answer

why does the server create too many sessions?

Since you sent several hundred requests to it, and the server appears to be configured to create max X sessions.


I used RequestScope and Stateless bean, how do they end up in a session?

They did not do this. JSF view state completed. When setting the JSF state persistence mode to server , which is the default setting, JSF will save the view state in an HTTP session.


What can I do to overcome this situation?

Set the method for saving JSF state to client using this context parameter in web.xml .

 <context-param> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param> 

This way, the state of the JSF view will be stored in serialized form in a hidden input field javax.faces.ViewState of the JSF form, and not in an HTTP session.

Or, using Mojarra 2.1.19 or later, just turn off JSF state persistence for each view by setting the transient attribute in <f:view> to true .

 <f:view transient="true"> 

(you can wrap <f:view> around <h:head> / <h:body> ; this is also what JSF implicitly does - it represents the UIViewRoot component)

See also:

+3
source

Source: https://habr.com/ru/post/1482882/


All Articles