繁体   English   中英

当事务类型是 JTA 时,显式启动和提交事务有什么影响?

[英]What is the impact of explicitly start and commit a transaction while transaction-type is JTA?

我正在开发一个将部署在Weblogic上的J2EE应用程序,它包含两层:

  1. 业务:DAO 和逻辑方法
  2. EJB:将使用业务层

我将两层分开,以便能够在 Java SE 项目中重用业务层(作为 jar 库)。

我使用transaction-type = JTA让服务器管理事务,但在 SE 项目中我使用transaction-type = RESOURCE_LOCAL所以我需要明确开始并提交事务。

所以问题是:如果我在使用JTA显式启动和提交事务有什么问题吗?

换句话说,以下两个代码之间是否存在巨大差异:

public void create(T entity) {

    entityManager.persist(entity);

}

public void create(T entity) {

    entityManager.getTransaction().begin(); 
    entityManager.persist(entity);
    entityManager.getTransaction().commit();

}

在手动处理事务时,您应该更加谨慎。 始终有一个安全网,以防出现异常,以便回滚您的操作:

      try {
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();
        try {
            entityManager.persist(entity);
            transaction.commit();
        } catch (Exception e) {                
            transaction.rollback(); 
            throw e; // optional if you want to manage the exception higher up                         
        } finally {
          entityManager.close(); // optional if you also manage you EM creation manually.
         } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM