MariaDB连接器与MySQLdb连接器 – 事务问题

huangapple go评论77阅读模式
英文:

MariaDB connector vs MySQLdb connector - transaction issue

问题

我们正在从MySQL连接器迁移到MariaDB连接器。我们正在使用Spring和Bitronix事务管理器。

在我们更改连接器时,我们在事务方面遇到了几个问题。可能我们已经找到了根本原因:处理MySQL和Maria中的联合事务的差异,请参阅下面的详细信息:

在项目的几个地方,我们正在使用此注释:
@Transactional(propagation = Propagation.REQUIRES_NEW)

以下是实现中的差异:

MySQL连接器,MysqlXAConnection#isSameRM实现:

if (xares instanceof MysqlXAConnection) {
    return this.underlyingConnection.isSameResource(((MysqlXAConnection) xares).underlyingConnection);
}

MariaDB连接器,org.mariadb.jdbc.MariaXaResource#isSameRM实现:

//通常由事务管理器用于“加入”事务。我们不支持加入,因此始终返回false;
return false;

是否有人遇到了相同的问题?如何处理?

我们的实现:

/**
 * 审计日志的实现,使用JPA在新事务中保存数据。
 */
public class JpaAudit implements AuditSPI {

    @Autowired
    private AuditlogRepository auditLogRepository;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void log(AuditLogEntity aAuditObj) {
        auditLogRepository.save(aAuditObj);
    }
}

Propagation.REQUIRES_NEW更改为Propagation.REQUIRED会有所帮助,但我认为这对我们来说不是方法。

英文:

We are migrating from the MySQL connector to the MariaDB connector. We are using Spring and the Bitronix transaction manager.

When we changed connector, we are facing several issues with transactions. Probably we have found the root cause: differences between handling joined transactions with MySQL and Maria, please see details below:

We are using this annotation on several places in the project:
@Transactional(propagation = Propagation.REQUIRES_NEW)

Here is the differences in implementation:

MySQL connector, MysqlXAConnection#isSameRM implementation:

if(xaresinstanceofMysqlXAConnection){
 return this.underlyingConnection.isSameResource(((MysqlXAConnection)xares).underlyingConnection);
}

MariaDB connector, org.mariadb.jdbc.MariaXaResource#isSameRM implementation:

//Typicallyusedbytransactionmanagerto"join"transactions.Wedonotsupportjoins,
//soalwaysreturnfalse;
return false;

Is there someone who faced the same issue? How to handle it?

Our implementation:

/**
 * Implementation of Audit Log which saves data using JPA in new transaction.
 */
public class JpaAudit implements AuditSPI {

    @Autowired
    private AuditlogRepository auditLogRepository;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void log(AuditLogEntity aAuditObj) {
        auditLogRepository.save(aAuditObj);
    }
}

Changing Propagation.REQUIRES_NEW to Propagation.REQUIRED helps but I think it is not way for us.

答案1

得分: 1

以下是翻译好的部分:

你问题中的问题已被报告(CONJ-825),并由Diego Dupin进行了修正(感谢他)。

提交的主要部分在src/main/java/org/mariadb/jdbc/MariaXaResource.java 中:

     @Override
      public boolean isSameRM(XAResource xaResource) {
    -    // 通常由事务管理器用于“加入”事务。我们不支持加入,
    -    // 所以始终返回false;
    +    if (xaResource instanceof MariaXaResource) {
    +      MariaXaResource other = (MariaXaResource) xaResource;
    +      return connection
    +          .getProtocol()
    +          .getUrlParser()
    +          .equals(other.connection.getProtocol().getUrlParser());
    +    }
    +    return false;
      }

这个修复包含在MariaDB Connector/J 2.7.0中。

英文:

The issue in your question was reported (CONJ-825) and corrected (commit 70c406b2) by Diego Dupin (thanks to him).

The main part of the commit is in src/main/java/org/mariadb/jdbc/MariaXaResource.java :

 @Override
  public boolean isSameRM(XAResource xaResource) {
-    // Typically used by transaction manager to "join" transactions. We do not support joins,
-    // so always return false;
+    if (xaResource instanceof MariaXaResource) {
+      MariaXaResource other = (MariaXaResource) xaResource;
+      return connection
+          .getProtocol()
+          .getUrlParser()
+          .equals(other.connection.getProtocol().getUrlParser());
+    }
+    return false;
  }

This fix is included from MariaDB Connector/J 2.7.0.

huangapple
  • 本文由 发表于 2020年8月31日 15:33:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63666604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定