处理服务层中的异常

时间:2015-04-01 14:50:13

标签: java spring hibernate exception-handling

请您建议如何克服下面描述的问题:

有一个应用程序(它涉及使用Hibernate和Spring框架)有3层:DAO,服务和控制器。我想避免在DB中保存重复的实体。我是通过使用DB级别的约束和bean中的注释(@Table(name = "artist", uniqueConstraints = {@UniqueConstraint(columnNames = "artist_name")}))来实现的。 逻辑假设如果我尝试添加重复实体,则抛出异常。我尝试在服务层处理异常但它未能成功。我的意思是,即使我明确注意到抛出的所有异常,由于异常,应用程序无法继续工作。为了更清楚,我引用下面的代码:

DAO-层

@Override
public void saveEntity(Client client) {
    Session session = sessionFactory.getCurrentSession();
    session.save(client);
}

服务层

@Transactional
@Override
public boolean saveEntity(Artist entity) {
    boolean completedState = false;
    try {
        //method from DAO-layer
        artistDAO.saveEntity(entity);
    } catch (ConstraintViolationException | UnexpectedRollbackException e) {
        return completedState;
    }
    return completedState = true;
}

控制器

@RequestMapping(value="addArtist", method=RequestMethod.POST)
public String processNewArtistForm(@ModelAttribute Artist artist, Model model) {
//If duplicated artistName prompts to be saved an saveEntity(entity) returns "false". Entity is failed to save.
    boolean completedState = false;
    completedState = artistService.saveEntity(artist);
    if (completedState == true) {
        List<Artist> listOfArtists = artistService.getListOfEntities();
        model.addAttribute("listOfArtists", listOfArtists);
    } else {
        model.addAttribute("errorMessage", "An artist under present name already exists");
        return "newArtist";
    }
    return "listOfArtists";
}

我尝试保存重复实体时的Tomcat通知

Type Exception report

message Request processing failed; nested exception is org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only
    org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:720)
    org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:521)
    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    com.sun.proxy.$Proxy29.saveEntity(Unknown Source)
    web.ArtistsListController.processNewArtistForm(ArtistsListController.java:66)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.9 logs.

如果这些架构解决方案是否正确,请您提供建议吗?提前谢谢你。

3 个答案:

答案 0 :(得分:1)

为什么不在保存之前检查重复项?:

@Transactional
@Override
public void saveEntity(Client client) {
    Session session = sessionFactory.getCurrentSession();
    // session.get() will always query the database and will return null
    // if no rows found
    if (session.get(Client.class,client.getId()==null) {
    session.save(client);
    //flush session cache
    session.flush();
     }
}

答案 1 :(得分:0)

默认情况下,@ Transaction注释会导致在任何RuntimeException上回滚的事务(请参阅doc)。

使用@Transactional(noRollbackFor = {ConstraintViolationException.class})更改行为。

答案 2 :(得分:0)

解决方案之一 - 是在控制器中处理异常。另外(坦率地说,我仍然无法理解它为什么会起作用)我可以冲洗会话。