gpt4 book ai didi

jsf - 事件 session 过多

转载 作者:行者123 更新时间:2023-12-04 19:20:44 30 4
gpt4 key购买 nike

我在 JBoss 6 上使用 JSF 2。我制作了一个示例应用程序,它使用 JSF 作为 View ,使用 EJB 作为逻辑,使用 JPA 作为持久性。 JSF 中的 bean 具有 RequestScoped。 EJB 是无状态的:

<?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 角,扁 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();
}

}

当我向它发送数百个请求时,JBoss 6 服务器抛出此异常:
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)

我的问题是:为什么服务器会创建太多 session ?我使用了 RequestScope 和 Stateless bean,它们怎么会在 session 中结束?我能做些什么来克服这种情况?如果我只使用servlet和JSP,当请求太多时,服务器会变慢,但至少不会像这样停止。

最佳答案

why does the server create too many session?


因为您向它发送了几百个请求,并且服务器显然配置为创建最多 X 个 session 。

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


他们没有。 JSF View 状态做到了。将 JSF 状态保存方法设置为 server 时,这是默认设置,然后 JSF 将在 HTTP session 中存储 View 状态。

What can I do to overcome this situation?


将 JSF 状态保存方法设置为 client通过 web.xml 中的上下文参数.
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
这样,JSF View 状态将以序列化形式存储在 javax.faces.ViewState 中。 JSF 表单的隐藏输入字段,而不是在 HTTP session 中。
或者,当使用 Mojarra 2.1.19 或更新版本时,只需通过设置 transient 关闭基于每个 View 的 JSF 状态保存。 <f:view> 的属性至 true .
<f:view transient="true">
(您可以将 <f:view> 包裹在 <h:head>/ <h:body> 周围;这也是 JSF 隐式执行的操作——它代表 UIViewRoot 组件)
也可以看看:
  • Why JSF saves the state of UI components on server?
  • 关于jsf - 事件 session 过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16766049/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com