JPA中更好的异常处理 [英] Better Exception Handling in JPA

查看:477
本文介绍了JPA中更好的异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我保留实体时,我使用了EJB3 / JPA,我很高兴它能够管理与数据库相关的
任务。
我唯一关心的是异常处理。保存实体时,我的示例代码总是具有这种风格。
我在网上阅读的大多数教程都具有这种风格,而与异常处理无关。

I used EJB3/JPA when persisting my entities and I am happy on how it is able to manage my DB related task. My only concern is on the exception handling. My sample code when saving entity always comes in this flavor. Most of the tutorials that I read on the net comes in this flavor also with no regards to exception handling.

@Stateless
public class StudentFacade{
    @PersistenceContext(unitName = "MyDBPU")
    private EntityManager em;

    public void save(Student student) {
        em.persist(student);
    }
}

但是我不知道异常处理的最佳方法是什么在EJB应用程序中?
处理异常时最好的方法应该是什么?

But I dont know whats the best way of exception handling in an EJB app? What should be the best way when handling exception?

这是其他人处理异常的方式吗?

Is this how others is handling the exception? A try catch block on your session facade?

@Stateless
public class StudentFacade{
    @PersistenceContext(unitName = "MyDBPU")
    private EntityManager em;

    public void save(Student student) {
        try {
            em.persist(student);
        } catch(Exception e) {
            //log it or do something
        }
    }
}

还是让方法抛出异常?

public void save(Student student) throws Exception {
    em.persist(student);
}

我不知道我的理解是否正确,因为我仍在学习EJB。
谢谢

I dont know if my understanding is correct since I am still learning EJB. Thanks

推荐答案

异常处理的想法是在发生任何故障时在一点上做一些逻辑。
尝试捕获将在需要处理异常或将异常转换为另一个异常的最终点使用

The idea of exception handling is doing some logic at a single point in case of any failure. The try catch will be used at the final point where you need to handle exception or you need to convert an exception to another exception

说您的应用有很多层,即动作,外观,持久性

Say your app has many layers namely Action, Facade, Persist

委托异常
在这种情况下,可以将抛出在Facade上的任何异常抛出动作层上方。
实际上,将捕获并处理特定的异常,并显示适当的错误消息。

Delegate exception In this case any exception that is thrown on Facade can be thrown to the above action layer. In action the particular exception will be caught and handled with proper error message.

//This is in Facade Layer
public void save(Student student) throws AppException{
    //exceptions delegated to action layer

    //call to Persist Layer
}

将常规异常转换为应用程序异常
说到持久性,您将获得像SQLException这样的DBException。不应将这种异常发送到Action或Facade层,因此我们可以捕获特定的异常,然后引发新的异常(应用程序的用户定义的异常)

Converting General Exception to App exception Say in persistence you get and DBException like sqlException. This exception should not be send as such to Action or Facade layer, so we catch the particular exception and then throw a new exception (a user defined exception for application)

//This is in Persist Layer
public void save(Student student) throws AppException{
        //converting general exception to AppException and delegating to Facade Layer

        try{
            em.persist(student);//call to DB. This is in Persist Layer
        }catch(Exception e){
            throw new AppException("DB exception", e)
        }
    }

行动层
您将捕获行动中的异常,然后在那里处理异常

In action Layer You will catch your exception in action and then handle exception there

  //This is in Action layer
  public void callSave(Student student){
            try{
                //call Facade layer
            }catch(AppException e){
               //Log error and handle
            }
    }

这篇关于JPA中更好的异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆