Tha*_*ham 2 jsf jpa exception-handling java-ee communicationexception
首先,我的框架是带有JSF,托管bean,EJB和JPA的Java EE 6.我编写了一个简单的程序来查询数据库中的信息.因此,当我单击一个按钮时,它会触发一个事件到托管bean,其中事件侦听器方法将访问EJB方法.EJB方法将对select实体进行简单查询.如果数据库在I之前或期间关闭select,我会收到异常
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 51,460 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
Error Code: 0
Run Code Online (Sandbox Code Playgroud)
如何防止此异常?肯定是try, catch在这里,但不知道在哪里放.当我em.createNamedQuery还是em.remove,我试图抓住com.mysql.jdbc.exceptions.jdbc4.CommunicationsException,但我得到了一个错误说:Exception com.mysql.jdbc.exceptions.jdbc4.CommunicationsException is never thrown in body of corresponding try statement.
以下是我的代码,我会在哪里捕获异常?
这是我的 EJB
@Stateless
@LocalBean
public class DocumentSBean {
@PersistenceContext
private EntityManager em;
public List<User> listUser(){
Query query = em.createNamedQuery("User.listUser");
query.returnResultList();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 ManagedBean
@ManagedBean(name="document")
@ViewScoped
public class DisplayListController implements Serializable {
@EJB
DocumentSBean sBean;
List<User> users = null;
public void foo(){
users = sBean.listUser();
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
我尝试下面列出的两种方式,但仍然在firebug上返回状态200而不是500
<p:commandButton update="myform" actionListener="#{document.setDisplayFacility}" rendered="#{utility.admin}" value="Facilities"/>
Run Code Online (Sandbox Code Playgroud)
要么
<p:commandButton update="myform" actionListener="#{document.setDisplayFacility}" rendered="#{utility.admin}" value="Facilities">
<p:ajax actionListener="#{document.setDisplayFacility}" update="myform" event="click"/>
</p:commandButton>
Run Code Online (Sandbox Code Playgroud)
这是setDisplayFacility()
public void setDisplayFacility(){
facilities = sBean.getAllFacilities(); //sBean is EJB
displayFacility = true;
}
Run Code Online (Sandbox Code Playgroud)
如何防止此异常?绝对是一个尝试,抓住这里,但不知道在哪里放.
只能在那里以合理的方式处理异常.
我试着捕获com.mysql.jdbc.exceptions.jdbc4.CommunicationsException,但是我得到一个错误说:异常com.mysql.jdbc.exceptions.jdbc4.CommunicationsException永远不会在相应的try语句的主体中抛出.
该CommunicationsException是在这种情况下,嵌套的例外DatabaseException.的EclipseLink是盖已经捕捉下CommunicationsException,并重新抛出它DatabaseException与CommunicationsException作为根本原因.就像是:
try {
// Execute query.
} catch (Exception e) {
throw new DatabaseException("Internal Exception: " + e, e);
}
Run Code Online (Sandbox Code Playgroud)
从理论上讲,您只能按如下方式处理:
try {
// Let EclipseLink execute query.
} catch (DatabaseException e) {
if (e.getCause() instanceof CommunicationsException) {
// Handle.
}
}
Run Code Online (Sandbox Code Playgroud)
然而,这是丑陋的,在这种特殊情况下不推荐.
以下是我的代码,我会在哪里捕获异常?
取决于您希望如何处理异常.如果你想在一般的错误页面中显示它,那么你不应该自己捕获它,而只是放手.然后servletcontainer将自己捕获并处理它.它会查找最佳匹配<error-page>中web.xml并显示它.
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/generic-error.xhtml</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
这一次将显示/generic-error.xhtml为所有的子类java.lang.Exception.
如果要在特定的错误页面中显示它,则需要声明<exception-type>更具体的内容以匹配实际的异常类型.例如:
<error-page>
<exception-type>org.eclipse.persistence.exceptions.DatabaseException</exception-type>
<location>/database-error.xhtml</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
但是,虽然未在您的问题中明确指出,但我根据您的问题历史知道您正在使用PrimeFaces的 JSF .您需要记住,当ajax发出初始请求时,PrimeFaces 不会显示错误页面.相反,它的ajax视图处理程序已经捕获了异常本身,它将委托给<p:ajaxStatus>视图中的组件.尝试添加ajax="false"到PrimeFaces命令组件,您最终将看到servletcontainer的默认错误页面(或者如果web.xml找到匹配的错误页面,则会显示).
如果你想在PrimeFaces收到ajax错误时显示一些通用的JSF UI,那么使用errorfacet <p:ajaxStatus>.例如
<p:ajaxStatus>
<f:facet name="start"><h:graphicImage value="images/ajax-loader.gif" /></f:facet>
<f:facet name="success"><h:outputText value="" /></f:facet>
<f:facet name="error">
<h:panelGroup layout="block" styleClass="ui-message-error ui-widget ui-corner-all">
<h:outputText value="An error has occurred!" /><br />
<h:outputLink value="#" onclick="window.location.reload(true)"><h:outputText value="Please reload page and retry" /></h:outputLink><br />
<h:outputLink value="mailto:support@example.com?subject=Ajax%20Error"><h:outputText value="If in vain, please contact support" /></h:outputLink>
</h:panelGroup>
</f:facet>
</p:ajaxStatus>
Run Code Online (Sandbox Code Playgroud)
(然而,PrimeFaces 2.2 RC1中存在一些错误导致显示错误方面失败,它在PrimeFaces 2.1中正常工作)
| 归档时间: |
|
| 查看次数: |
2942 次 |
| 最近记录: |