OSGi中的事务回滚 [英] Transaction rollback in OSGi

查看:167
本文介绍了OSGi中的事务回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个OSGi包,我在其中声明了一个服务并用蓝图注入了一个事务:

I have an OSGi bundle in which I declare a service and inject into it a transaction with blueprint:

<bean id="MyServiceImpl"
          class="com.test.impl.MyServiceImpl">
    <jpa:context property="em" unitname="mypu" />
    <tx:transaction method="*" value="Required" />
</bean>
<service id="MyService" ref="MyServiceImpl" interface="com.test.api.MyService" />   

在这个服务中我有两个方法,每个方法都在数据库中写入数据,类似于以下:

In this service I have two methods each one of which is writing data in the database, something like the following:

public void createParent() throws MyException {
    Parent parent = new Parent();
    ... // Set parent fields
    em.persist(parent);
    createChild();
    // Checks that could throw MyException
}

public void createChild() throws MyException {
    Child child = new Child();
    ... // Set child fields
    em.persist(child);
    // Checks that could throw MyException
}

我的问题如下:


  1. 如果我在 em.persist(parent)之间的createParent方法中抛出运行时异常; createChild(); 事务回滚(正如我所料)并且父级不会保留在数据库中。但是,如果在同一点上我抛出MyException(这是一个已检查的异常),则事务提交并且父持久化。我在 Aries邮件列表蓝图声明性事务中声明的(已检查)异常不会触发回滚。有没有办法配置此行为并指定我希望我的异常在抛出时回滚事务?

  2. 如果我在createChild方法中抛出运行时异常(在 em.persist(child); )子数据库中没有持久化,但父项是持久存在的,就好像这两个方法在两个不同的事务中运行一样。这是为什么?不应该在createParent启动的事务中创建连接吗?

  3. 如果在调用createChild之后在createParent方法中抛出运行时异常,我会得到与第2点相同的行为(即。 parent是持久化的,而child不是持久化的,这让我更加困惑,因为即使我假设createChild启动一个新的事务,然后在createParent中抛出异常时也不会回滚。

  4. 如果在上面的第2点和第3点中,两种方法都在不同的服务中,那么一切都按预期工作,即。在任何方法中抛出的运行时异常都会回滚整个事务。

  1. If I throw a runtime exception in the createParent method between em.persist(parent); and createChild(); the transaction rolls back (as I would expect) and parent is not persisted in the DB. However if at the same point I throw MyException (which is a checked exception) the transaction commits and parent is persisted. I saw in the Aries mailing list that a declared (checked) exception in a blueprint declarative transaction does not trigger a rollback. Is there a way to configure this behavior and specify that I want my exception to rollback the transaction when thrown?
  2. If I throw a runtime exception in the createChild method (after em.persist(child);) child is not persisted in the database, however parent is persisted, as if the two methods are running in two different transactions. Why is that? Shouldn't createChild join in the transaction started by createParent?
  3. If I throw a runtime exception in the createParent method after the call to createChild I get the same behavior as in point 2 (ie. parent is persisted and child is not persisted) which confuses me even more since even if I assume that createChild starts a new transaction then this should not get rolled back when an exception is thrown in createParent.
  4. If in points 2 and 3 above the two methods are in different services then everything works as expected, ie. a runtime exception thrown in any of the methods rolls back the whole transaction.

有人可以解释一下上述行为吗?

Can someone please explain the above behavior?

推荐答案

从Aries邮件列表中获得一些帮助后发现问题出在数据源配置中,而不是蓝图配置中。虽然我使用 MysqlXADataSource 作为驱动程序类,但数据源服务已注册为 javax.sql.DataSource 而不是 javax.sql.XADataSource 这就是弄乱我的交易。

After getting some help form the Aries mailing list it turns out the problem was in the datasource configuration and not in the blueprint configuration. Although I was using MysqlXADataSource as a driver class the datasource service was registered as a javax.sql.DataSource instead of javax.sql.XADataSource which is what was messing up my transactions.

这篇关于OSGi中的事务回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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