まず、私のフレームワークは、JSF、マネージドBean、EJB、およびJPAを備えたJavaEE6です。データベースから情報を照会する簡単なプログラムを作成します。したがって、ボタンをクリックすると、マネージド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
この例外からどのように保護しますか?間違いなく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();
}
}
これは私のManagedBean
@ManagedBean(name="document")
@ViewScoped
public class DisplayListController implements Serializable {
@EJB
DocumentSBean sBean;
List<User> users = null;
public void foo(){
users = sBean.listUser();
}
}
編集
以下にリストされているように両方の方法を試しますが、firebugで500ではなく200のステータスを返します
<p:commandButton update="myform" actionListener="#{document.setDisplayFacility}" rendered="#{utility.admin}" value="Facilities"/>
また
<p:commandButton update="myform" actionListener="#{document.setDisplayFacility}" rendered="#{utility.admin}" value="Facilities">
<p:ajax actionListener="#{document.setDisplayFacility}" update="myform" event="click"/>
</p:commandButton>
これがsetDisplayFacility()です
public void setDisplayFacility(){
facilities = sBean.getAllFacilities(); //sBean is EJB
displayFacility = true;
}