【问题标题】:javax.persistence.TransactionRequiredException: There is no currently active transactionjavax.persistence.TransactionRequiredException:当前没有活动事务
【发布时间】:2017-09-25 01:29:09
【问题描述】:

我有 EntityManager 服务并在 init 方法中创建 DAO 类并将 EntityManager 传递给 DAO 构造函数。:

@Slf4j
public class OPhoneService {
@Setter
    private EntityManager entityManager;

public void init() {
        log.info("init");
        log.info(Thread.currentThread().getName());
        oPhoneDao = new OPhoneDaoImpl(entityManager);
        List<OPhone> oPhones = oPhoneDao.getAllOPhones(0);
        OPhone oPhone = oPhones.get(0);
        oPhone.setState(1);
        oPhoneDao.merge(oPhone);
}

}

在这一行oPhoneDao.merge(oPhone); 得到错误:

javax.persistence.TransactionRequiredException: There is no currently active transaction.

我的合并方法:

@Override
    public E merge(E e) {
        E merge = entityManager.merge(e);
        entityManager.flush();
        return merge;
    }

还有我的 bean 配置

<bean id="oPhoneBean" class="....services.OPhoneService" init-method="init"
          scope="singleton">
        <jpa:context unitname="ophone" property="entityManager"/>
        <tx:transaction method="*" value="Required"/>
    </bean>

【问题讨论】:

    标签: java hibernate jpa osgi entity


    【解决方案1】:

    这是 Aries 蓝图中的一个已知问题。事务拦截器未添加到 init 方法中。

    ARIES-1715

    【讨论】:

    • 好的。是为了测试。我在 init 方法中写了这个,因为当我使用石英方法时,我得到了一些错误。我在 init 方法中创建 dao 并尝试在 schedulet quartz 方法中使用这个 dao,当我调用合并我的应用程序时冻结
    • 我很高兴我退出了,不再使用 karaf。
    • 那你为什么问?
    • 因为这个曲线系统应该再过 5 天
    • 嗯,我经常使用 karaf,对它非常满意。
    【解决方案2】:

    您需要在合并方法中启动并提交事务。

    @Override
    public E merge(E e) {
        EntityTransaction tx = entityManager.getTransaction();
        tx.begin();
        E merge = entityManager.merge(e);
        tx.commit();
        entityManager.flush();
        return merge;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-09
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-25
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多