mysql - 使用 spring-boot 和 bitronix JTA 管理器将 XA 数据源配置到 mysql 8 DB 失败并出现 SqlException XAERR_RMERR

标签 mysql spring-data-jpa jta xa bitronix

我创建了一个 Spring Boot 2 (2.1.6.RELEASE) 项目,它依赖于 spring-boot-starter-data-jpa 和 spring spring-boot-starter-jta-bitronix,并为 Mysql DB 配置了 XA 数据源 ( 8.0.16).

应用程序属性文件(针对与 <> 之间的占位符值的相关性进行了修剪)包含以下配置:

spring:
  datasource:
    url: jdbc:mysql://<host>:<port>/<dbName>
    username: <username>
    password: <password>
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    database-platform: org.hibernate.dialect.MySQL8Dialect
    hibernate:
      ddl-auto: none
  jta:
    bitronix:
      properties:
        server-id: <serverid>

在启动 spring boot 应用程序时,我收到以下错误堆栈跟踪:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactory' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactoryBuilder' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactoryBuilder' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaVendorAdapter' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception; nested exception is bitronix.tm.resource.ResourceConfigurationException: cannot create JDBC datasource named dataSource
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactoryBuilder' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactoryBuilder' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaVendorAdapter' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception; nested exception is bitronix.tm.resource.ResourceConfigurationException: cannot create JDBC datasource named dataSource
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaVendorAdapter' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception; nested exception is bitronix.tm.resource.ResourceConfigurationException: cannot create JDBC datasource named dataSource
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception; nested exception is bitronix.tm.resource.ResourceConfigurationException: cannot create JDBC datasource named dataSource
Caused by: bitronix.tm.resource.ResourceConfigurationException: cannot create JDBC datasource named dataSource
Caused by: bitronix.tm.recovery.RecoveryException: failed recovering resource dataSource
Caused by: com.mysql.cj.jdbc.MysqlXAException: XAER_RMERR: Fatal error occurred in the transaction branch - check your data for consistency
Caused by: java.sql.SQLException: XAER_RMERR: Fatal error occurred in the transaction branch - check your data for consistency

最佳答案

免责声明:我正在 self 记录我自己的问题,希望能帮助其他人,因为令人惊讶的是,这个问题/问题很难解决。

来自验证 github 上的 bitronix 问题跟踪器 https://github.com/bitronix/btm/issues/100 , 即使问题已经关闭而没有直接回答,解决方案也不是很明显。

https://github.com/bitronix/btm/wiki/FAQ 阅读 bitronix 常见问题解答尽管与 Oracle 相关,但暗示了该问题,它指的是缺少用户权限。

进一步调查导致了关于权限的 MySQL 8 文档页面 https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html以下部分特别强调。

Prior to MySQL 8.0, any user could execute the XA RECOVER statement to discover the XID values for outstanding prepared XA transactions, possibly leading to commit or rollback of an XA transaction by a user other than the one who started it. In MySQL 8.0, XA RECOVER is permitted only to users who have the XA_RECOVER_ADMIN privilege, which is expected to be granted only to administrative users who have need for it. This might be the case, for example, for administrators of an XA application if it has crashed and it is necessary to find outstanding transactions started by the application so they can be rolled back. This privilege requirement prevents users from discovering the XID values for outstanding prepared XA transactions other than their own. It does not affect normal commit or rollback of an XA transaction because the user who started it knows its XID.

因此,我在 MySQL 中通过以下命令为数据源用户添加了所需的权限(根据需要替换用户名和主机部分)。

GRANT XA_RECOVER_ADMIN ON *.* TO 'username'@'%';
FLUSH PRIVILEGES;

通过此更改,spring boot 应用程序可以毫无问题地启动。

关于mysql - 使用 spring-boot 和 bitronix JTA 管理器将 XA 数据源配置到 mysql 8 DB 失败并出现 SqlException XAERR_RMERR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56831529/

相关文章:

php - 如何处理 Android 和 PHP/MySQL 之间的时区?

spring - 在使用 QueryDSL 编写动态查询时,如何使用 Spring 的分页(使用 Pageable)?

java - 关闭连接后,数据连接池中的连接也不可用

Spring JPA : The column name ** was not found in this ResultSet

java - 为什么不提交 Requires_New 事务?

jpa - 处理 JPA 合并的最佳方法?

mysql - 如何从 mysql 获取 utf-8 字符到 Visual Fox Pro

mysql - 如果删除了两个外键,则删除表条目

java - persistence.xml 事务类型 ="JTA"

Python MySQLdb唯一记录,忽略错误