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;
}
java.lang.Exception
, then it work but when I catchorg.eclipse.persistence.exceptions.DatabaseException
, it does not work. Maybe it got nested again to a more general Exception. I have a question that is quite off the topics. I shut down the mysql server to test this, however when I turn it back on, things does not get back to normal. Meaning, when I click on component that query from the database, it still give me the error page. I have to restart both glassfish and mysql server for it to work normally again. Is this normal, BalusC? – Ovenware