First of all, my framework is Java EE 6 with JSF, managed bean, EJB and JPA. I write a simple program to query information from the database. So when I click a button, it trigger an event to a managed bean, where an event listener method will access EJB method. The EJB method will do a simple select
query to an Entity. If the database is shutdown before or during the time I select
, I get an exception
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
How do I safeguard away from this Exception? Definitely a try, catch
here, but not sure where to put it. When I do em.createNamedQuery
or em.remove
, I try to catch com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
, but I got an error said: Exception com.mysql.jdbc.exceptions.jdbc4.CommunicationsException is never thrown in body of corresponding try statement
.
Below are my codes, where would I catch the Exception?
This is my EJB
@Stateless
@LocalBean
public class DocumentSBean {
@PersistenceContext
private EntityManager em;
public List<User> listUser(){
Query query = em.createNamedQuery("User.listUser");
query.returnResultList();
}
}
This is my ManagedBean
@ManagedBean(name="document")
@ViewScoped
public class DisplayListController implements Serializable {
@EJB
DocumentSBean sBean;
List<User> users = null;
public void foo(){
users = sBean.listUser();
}
}
EDIT
I try both ways as listed below, but still return status 200 instead of 500 on firebug
<p:commandButton update="myform" actionListener="#{document.setDisplayFacility}" rendered="#{utility.admin}" value="Facilities"/>
OR
<p:commandButton update="myform" actionListener="#{document.setDisplayFacility}" rendered="#{utility.admin}" value="Facilities">
<p:ajax actionListener="#{document.setDisplayFacility}" update="myform" event="click"/>
</p:commandButton>
Here is setDisplayFacility()
public void setDisplayFacility(){
facilities = sBean.getAllFacilities(); //sBean is EJB
displayFacility = true;
}
In my view at least, a database becoming unavailable is a serious error that needs to be handled and usually ASAP. The general approach on safeguarding against such situation is enforcing high-avaialbility and failover mechanisms through database clustering.
If you don't have this option however nor you want the user to see a big-fat-exception, what you might try to do is route him in some user-friendly page when this particular error happens. To do so, put the following in your web.xml (assuming that your FacesServlet is mapped to *.faces):
Then, in the error.xhtml page the general approach is to put some user-friendly and largely apologetic message indicating how sorry the administrator feels for the user's inconvenience...
At any rate, dealing with such situations by catching exceptions and not acting upon them, either by re-throwing them or dealing with them in the catch block-if possible, is generally considered as a bad practice at critical errors might go un-noticed.
Regarding your "is never thrown in body of corresponding try statement" question you might want to check this article.
Cheers!
The CommunicationsException is wrapped in a EclipseLink DatabaseException, which is a runtime Exception. If you are using JPA or JTA, then this may also get wrapped in a PersistenceException, or a TransactionRolledbackException. So, try catching one of these, or worst case RuntimeException. The CommunicationsException will be in the caused by chain.
EclipseLink will automatically try to reconnect dead connections, but if your database is down, this will fail (you may see the error logged 3 times), so you will need to report an error to your user.
Only catch the exception there where you can handle it in a reasonable manner.
The
CommunicationsException
is in this case a nested exception ofDatabaseException
. EclipseLink is under the covers already catching theCommunicationsException
and rethrowing it asDatabaseException
with theCommunicationsException
as root cause. Something like:In theory, you can only handle it as follows:
Which is however ugly and not recommended in this particular case.
Depends on how you would like to handle the exception. If you want to display it in a generic error page, then you should not catch it yourself, but just let it go. The servletcontainer will then catch and handle it itself. It will lookup the best matching
<error-page>
in theweb.xml
and display it.This one will display
/generic-error.xhtml
for all subclasses ofjava.lang.Exception
.If you want to display it in a specific error page, then you need to declare the
<exception-type>
more specific to match the actual exception type. E.g.:However, although not explicitly specified in your question, I know based on your question history that you're using JSF with PrimeFaces. You need to keep in mind that PrimeFaces will not display the error page when the initial request was made by ajax. Instead, its ajax view handler has already catched the exception itself and it will delegate to the
<p:ajaxStatus>
component in the view. Try addingajax="false"
to the PrimeFaces command component and you'll finally see the servletcontainer's default error page being displayed (or any of yours if a matching one inweb.xml
is found).If you want to display some generic JSF UI when PrimeFaces received an ajax error, then use the
error
facet of<p:ajaxStatus>
. E.g.(there's however some bug in PrimeFaces 2.2 RC1 which causes displaying the error facet to fail, it works correctly in PrimeFaces 2.1)