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

时间:2019-07-01 07:26:18

标签: 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应用程序时,我收到以下错误stacktrace:

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

1 个答案:

答案 0 :(得分:0)

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

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

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

进一步的调查导致获得关于特权https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html的MySQL 8文档页面,并在以下部分中突出显示了这一点。

  

在MySQL 8.0之前,任何用户都可以执行XA RECOVER语句来   发现未完成的准备好的XA交易的XID值,   可能导致用户提交或回滚XA事务   除了启动它的人。 在MySQL 8.0中,XA RECOVER是   仅允许具有XA_RECOVER_ADMIN特权的用户使用,   预期仅授予有需要的管理用户   。例如,对于   XA应用程序(如果已崩溃且有必要查找)   由应用程序启动的未完成交易,因此它们可以   回滚。此特权要求可防止用户   发现未完成的XA事务的XID值   除了自己的。它不会影响正常的提交或回滚   XA交易,因为启动它的用户知道其XID。

因此,我通过My​​SQL中的以下命令为数据源用户添加了所需的特权(相应地替换用户名和主机部分)。

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

进行此更改后,spring boot应用程序将启动,没有任何问题。