最后将JAX-RS和JPA与Tomcat一起使用,而不需要catch



我们正在使用JAX-RS和JPA。我们使用具有以下结构的方法(细节省略):

@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ResultObject saveById(   @PathParam("id") BigInteger id, 
SomeObject someObject) {
    entityManager = EMF.obtainEntityManager();
    try {
      .. start transaction
      .. write all information to the database
      .. commit transaction
      .. return ResultObject
    } catch ( Exception exception) {
      .. rollback transaction
      .. return ResultObject together with an appropriate error
    } finally {
        entityManager.close();
    }           
}

有没有一种"最佳"方法可以避免在我们创建的每个JAX-RS方法上重复捕获并最终重复捕获?使用过滤器?我们的服务提供商仅支持Tomcat。没有玻璃鱼或其他容器。谢谢你的帮助。

我会将实现转移到REST服务调用的不同服务。REST应该是API的一种类型。逻辑应该在不同的服务中。

@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ResultObject saveById( @PathParam("id") BigInteger id, SomeObject someObject) {
    ResultObject resultObject = new ResultObject();
    ResultType res = someService.saveById(id, someObject)
    // do something with res
    return resultObject ;
}

然后在SomeService中实现一些可以实现事务逻辑的抽象类。

public abstract class AbstractService {
    protected void startTransaction() {
        //...
    }
    protected void endTransaction() {
        //...
    }
}
public class SomeService extends AbstractService {
   public ResultType saveById(BigInteger id, SomeObject someObject) {
       startTransaction();
       // your logic
       endTransaction();
   }
}

还有更好的方法。如果你知道的话,你可以使用Spring Framework。在该解决方案中,您使用@Transactional注释SomeService(或该类中的方法)。

@Transactional
public class SomeService {
   public ResultType saveById(BigInteger id, SomeObject someObject) {
       // your logic
   }
}

在过滤器中创建EntityManger

例如:-

public class EntityManagerFilter implements Filter {

  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    EntityManager em = null;
    try {
      /
      em = EntityManagerFactoryUtil.entityManagerFactory.createEntityManager();
     //
      EntityManagerUtil.ENTITY_MANAGERS.set(em);
      chain.doFilter(request, response);
      EntityManagerUtil.ENTITY_MANAGERS.remove();
      //
    } catch (Exception ex) {
      //
    } finally {
      try {
        if (em != null) {
          em.close();
          //
        };
      } catch (Throwable t) {
       //
      }
    }
  }
  public void init(FilterConfig config) {
    destroy();
    initEntityManagerFactory();
  }
  private void initEntityManagerFactory() {
    EntityManagerFactoryUtil.entityManagerFactory =
        Persistence.createEntityManagerFactory("PersistanceUnitName");
   //
  }
  public void destroy() {
   //
    try {
      if (EntityManagerFactoryUtil.entityManagerFactory != null) {
        EntityManagerFactoryUtil.entityManagerFactory.close();
      };
    } catch (Exception t) {
     /
  }
}
public class EntityManagerUtil {

 public static final ThreadLocal<EntityManager> ENTITY_MANAGERS = new ThreadLocal<EntityManager>();
  /** Returns a fresh EntityManager */
  public static EntityManager getEntityManager() {
    return ENTITY_MANAGERS.get();
  }
}
public class EntityManagerFactoryUtil {
  public static EntityManagerFactory entityManagerFactory;
}

相关内容

最新更新