今天把分布式事务弄好以后,后面一直报连接错误
先来说这个错误:XA resource 'XXXXdatasource': end for XID '' raised -7: the XA resource has become unavailable
这个算是mysql对于Atomikos的一个小bug
官方的解释:
A possible workaround might be to try and start TransactionsEssentials with the parametercom.atomikos.icatch.serial_jta_transactions set to false.
com.atomikos.icatch.serial_jta_transactions=false
Also, make sure to set the following property on the MySQL datasource:
pinGlobalTxToPhysicalConnection="true"
我在试了com.atomikos.icatch.serial_jta_transactions这个以后,还是一样的,没有任何反应。照常报错,数据库还是一直连接不,第一次登陆访问数据库都不行
于是换一种方式pinGlobalTxToPhysicalConnection="true";
但是我是用的druid,也挺无奈,于是在url上面添加了一段,惊奇的发现。卧槽,居然可以了
配置文件:
spring:
datasource:
druid:
type: com.alibaba.druid.pool.xa.DruidXADataSource
driver-class-name: com.mysql.jdbc.Driver
platform: mysql
default:
# 资源标识
uniqueResourceName: default
# 数据源类名
xaDataSourceClassName: com.alibaba.druid.pool.xa.DruidXADataSource
xaProperties:
# 数据源配置
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://xxxxxxxx:3306/mypinyu?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&pinGlobalTxToPhysicalConnection=true
username: root
password: Goodlan@123
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
second:
# 资源标识
uniqueResourceName: second
# 数据源类名
xaDataSourceClassName: com.alibaba.druid.pool.xa.DruidXADataSource
xaProperties:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mypinyu?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&pinGlobalTxToPhysicalConnection=true
username: root
password: admin
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
#验证连接是否可用,使用的SQL语句
validationQuery: SELECT 1
#指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除.
testWhileIdle: true
#借出连接时不要测试,否则很影响性能
testOnBorrow: false
testOnReturn: false
#jta相关参数配置
jta:
log-dir: classpath:tx-logs
transaction-manager-id: txManager
logging.config:
classpath: log4j2.xml
#返回视图的前缀 目录对应src/main/webapp下
spring.mvc.view.prefix: /WEB-INF/jsp/
#返回的后缀
spring.mvc.view.suffix: .jsp