.net mysql 分布式事务处理,为什么打开连接MySQL中抛出一个分布式事务错误? (.NET连接器)...

I'm opening a connection to a local MySQL server and on the connection.Open() method it throws this error:

System.NotSupportedException: MySQL Connector/Net does not currently support distributed transactions.

at MySql.Data.MySqlClient.MySqlConnection.EnlistTransaction(Transaction> transaction)

at MySql.Data.MySqlClient.MySqlConnection.Open()

All I'm doing is this:

var connection = new MySql.Data.MySqlClient.MySqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);

connection.Open();

The connection string in the app.config is

I don't know why it's trying to enlist the transaction, I haven't specified any transactions & I only have one MySQL server I'm connecting to

解决方案

Try adding Enlist=false to your connection string:

EDIT: from the MySQL Connector/.NET documentation, if you set AutoEnlist=false in the connection string it should work.

It appears that certain versions of ADO.NET can default to automatically enlisting a connection into an existing transaction. See http://msdn.microsoft.com/en-us/library/ms254973.aspx for more detail, but I expect somehow somewhere ADO is confused into thinking that there's an existing transaction going on to some other db.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 同时开启 Neo4j 和 MySQL事务可以使用 Spring 的编程式事务管理来实现。具体操作步骤如下: 1. 在 Spring 配置文件配置数据源和事务管理器。 ``` <bean id="neo4jDataSource" class="org.neo4j.jdbc.bolt.BoltDataSource"> <constructor-arg index="0" value="bolt://localhost:7687"/> <constructor-arg index="1" value="neo4j"/> <constructor-arg index="2" value="password"/> </bean> <bean id="mysqlDataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/dbname"/> <property name="username" value="username"/> <property name="password" value="password"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="mysqlDataSource"/> </bean> ``` 2. 在 Java 代码使用 `TransactionTemplate` 来同时开启 Neo4j 和 MySQL事务。 ``` @Autowired private DataSource neo4jDataSource; @Autowired private DataSource mysqlDataSource; @Autowired private TransactionTemplate transactionTemplate; public void doTransaction() { transactionTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus status) { try (Connection neo4jConnection = neo4jDataSource.getConnection(); Connection mysqlConnection = mysqlDataSource.getConnection()) { neo4jConnection.setAutoCommit(false); mysqlConnection.setAutoCommit(false); // 在 Neo4j 执行事务操作 // ... // 在 MySQL 执行事务操作 // ... neo4jConnection.commit(); mysqlConnection.commit(); } catch (Exception e) { status.setRollbackOnly(); throw new RuntimeException("Transaction rolled back.", e); } } }); } ``` 在以上代码,我们使用了 `TransactionTemplate` 来开启事务,并且将 Neo4j 和 MySQL 的数据源分别注入到了代码。在事务,我们可以通过获取对应的数据库连接对象,然后手动开启、提交和回滚事务。在现异常时,我们需要将事务状态标记为回滚状态,并异常让 Spring 框架自动回滚事务

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值