4

I am using JSF 2 on JBoss 6. I made a sample application which use JSF as the View, EJB for logic and JPA for Persistence. The bean in JSF 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 a few hundreds 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 session? I used RequestScope and Stateless bean, how come they end up 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 is slowed down but at least it is not halted like this.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • What does `use only servlet and JSP` mean ? – Rong Nguyen May 27 '13 at 04:58
  • it means "if I use Servlet and JSP only instead of using JSF". There's nothing unclear, isn't there? –  May 27 '13 at 05:55
  • Post this in JBoss forums they could evaluate it better. – Luiggi Mendoza May 27 '13 at 06:15
  • What are you using to send the requests? If you're using a tool it needs to return the JSESSIONID cookie with every subsequent request. from: https://community.jboss.org/thread/228632 – Karl Kildén May 27 '13 at 07:09
  • @KarlKildén I posted the question you quoted –  May 27 '13 at 07:33
  • Sorry about that. I find it weird nobody else had this issue. Good luck – Karl Kildén May 27 '13 at 08:36
  • They key here is com.sun.faces.application.view.FaceletViewHandlingStrategy.getSession(FaceletViewHandlingStrategy.java:494) - why JSF calls getSession with true. My JSF sources don't have this call (2.0.6). – mrembisz May 27 '13 at 11:22

1 Answers1

3

why does the server create too many session?

Because you sent a few hundred requests to it and the server is apparently configured to create max X sessions.


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

They didn't. The JSF view state did. When setting JSF state saving method to server, which is the default setting, then JSF will store the view state in HTTP session.


What can I do to overcome this situation?

Set JSF state saving method to client by 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 JSF view state will in serialized form be stored in the javax.faces.ViewState hidden input field of the JSF form instead of in the HTTP session.

Or, when using Mojarra 2.1.19 or newer, just turn off JSF state saving on a per-view basis by setting transient attribute of the <f:view> to true.

<f:view transient="true">

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

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555