mysql - 事务回滚在 karaf 中不起作用

标签 mysql hibernate jpa osgi rollback

我使用 apache karaf 作为 OSGI 容器,使用 Aries Blueprint 来声明 beans,使用 Hibernate 4.3.6 作为 JPA 提供程序。数据库是MySQL。我需要在抛出异常时回滚事务。我将用以下代码片段来说明它:

  @Transactional(Transactional.TxType.REQUIRED)
  public void testTransactionMethod() {
    Role role = createRole();
    roleDao.save(role);

    User user = new User();
    userDao.save(user);
  }

两个 DAO 的 dao.save() 方法的实现是:

public T save(T entity) {
    return entityManager.merge(entity);
}

save() DAO 的方法没有用 @Transactional 注释。 我的persistence.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             version="2.0">
  <persistence-unit name="examplePersistenceUnit" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/sqlds)</jta-data-source>
    <properties>
      <property name="hibernate.connection.url" value="jdbcURL"/>
      <property name="hibernate.connection.username" value="username"/>
      <property name="hibernate.connection.password" value="password"/>
      <property name="hibernate.connection.driver_class" value="DriverClass"/>

      <property name="hibernate.dialect" value="DB_Dialect"/>
      <property name="hibernate.show_sql" value="false"/>
      <property name="hibernate.hbm2ddl.auto" value="none"/>
    </properties>
  </persistence-unit>
</persistence>

我期望的是当userDao.save(user)时引发异常,整个事务将被回滚,并且我的 ROLE 表中没有保存任何内容,但实际上我的数据库中有一个新的角色条目。 我也尝试过使用 @Transactional(rollbackOn = Exception.class) ,但仍然得到相同的结果。

编辑

根据要求,我添加了我的数据源配置。我使用 pax-jdbc。我有两个 cfg 文件。第一个是org.apache.aries.jpa.examplePersistenceUnit.cfg

hibernate.connection.url = jdbc:mysql://url/schema
hibernate.connection.username = username
hibernate.connection.password = password
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.hbm2ddl.auto = none

第二个是org.ops4j.datasource-example.cfg

osgi.jdbc.driver.name = mysql
    databaseName = ${user.name}
    dataSourceName = jdbc/sqlds
    url=jdbc:mysql://url/schema
    user=${user.name}
    password=${user.name}

还在我的蓝图中我有以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://aries.apache.org/xmlns/jpa/v2.0.0"
    xmlns:tx="http://aries.apache.org/xmlns/transactions/v2.0.0"
    xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
    <jpa:enable />
    <tx:enable />

最佳答案

您必须使用 XA 数据源。

您可以通过修改 cfg 文件中 osgi.jdbc.driver.name 的值来做到这一点。

请参阅下面的链接了解更多信息:

https://ops4j1.jira.com/wiki/spaces/PAXJDBC/pages/61767716/Pooling+and+XA+support+for+DataSourceFactory

关于mysql - 事务回滚在 karaf 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43472004/

相关文章:

mysql - 有没有办法在多对一关系中共享类的复合主键?

java - 使用 Sniffy 指定的 Oracle URL 无效

java - 是否可以在没有实体的情况下使用 JpaRepository?

android - Sqlite 数据库查询在获取时出现一些问题

php - 如何获取包含搜索项的数据

java - 来自多个数据库的 Hibernate 实体

Java时间在Mysql表中插入

sql - 在一个词中搜索文字

mysql - 在 MySQL 中向具有特定前缀的表授予权限

java - 如何动态编写条件查询?