为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。
【DM版本】:--03134284194-20240621-232765-20108
【操作系统】:Docker
【CPU】: 虚拟化机器
【问题描述】:
mysql 迁移到 达梦,之前mysql设置的是 REPEATABLE-READ (默认),
并看到 dm.ini 文件中
ISOLATION_LEVEL = 1 #Default Transaction Isolation Level, 1: Read Commited; 3: Serializable
也没有看到 2 的配置。尝试过改为2,重启服务,但是服务启动成功,但是连不上服务(暂未研究具体原因)
现在 ef core 的实现代码:
using var transcation = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions() { IsolationLevel = IsolationLevel.RepeatableRead },TransactionScopeAsyncFlowOption.Enabled);
......
transcation.Complete();
默认配置的话,提交报错:
Microsoft.EntityFrameworkCore.Update: Error: An exception occurred in the database while saving changes for context type 'Byzan.Spas.Manufacture.Dal.SpasManufactureDbContext'.
Dm.DmException (0x80004005): 6038,非法的事务隔离级
at Dm.DmError.ThrowDmException(Int32 code)
at Dm.DmConnInstance.SetTransactionIsolation(DmTransaction m_Tran, IsolationLevel level)
at Dm.DmConnInstance.BeginTrx(IsolationLevel il)
at Dm.DmConnection.do_BeginDbTransaction(IsolationLevel isolationLevel)
at Dm.DmPromotableTransaction.System.Transactions.IPromotableSinglePhaseNotification.Initialize()
at System.Transactions.TransactionStatePSPEOperation.PSPEInitialize(InternalTransaction tx, IPromotableSinglePhaseNotification promotableSinglePhaseNotification, Guid promoterType)
at System.Transactions.TransactionStateActive.EnlistPromotableSinglePhase(InternalTransaction tx, IPromotableSinglePhaseNotification promotableSinglePhaseNotification, Transaction atomicTransaction, Guid promoterType)
at System.Transactions.Transaction.EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification promotableSinglePhaseNotification, Guid promoterType)
at System.Transactions.Transaction.EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification promotableSinglePhaseNotification)
at Dm.DmConnection.do_EnlistTransaction(Transaction transaction)
at Dm.DmConnection.EnlistTransaction(Transaction transaction)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.HandleAmbientTransactions()
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList`1 entriesToSave, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(DbContext _, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Dm.Storage.Internal.DmExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
尝试过使用默认配置 或 ReadCommitted :
using var transcation = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
或者
using var transcation = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted },TransactionScopeAsyncFlowOption.Enabled);
但是无法批量插入数据:
c9 ccc9;
BEGIN
c9 = NEW rrr9[7];
INSERT INTO "Manufacture.OrderDemandSegmentation" ("Id", "BomId", "CreateByUserId", "CreateDateTime", "DeletedBySplitOrder", "LastUpdateByUserId", "LastUpdateDateTime", "MaterialId", "No", "OrderPlanId", "ParentId", "ParentIdPaths", "ParentNo", "Remark", "RoutingSequenceSpecificationId", "RoutingSpecificationId", "RowVersion", "SegmentLength", "SegmentOriginLength", "SegmentationSequenceId", "SequenceId", "Status", "TenantId", "ThirdPartyExtensions", "ThirdPartyId", "UnitId", "UnitName")
VALUES (:p245, :p246, :p247, :p248, :p249, :p250, :p251, :p252, :p253, :p254, :p255, :p256, :p257, :p258, :p259, :p260, :p261, :p262, :p263, :p264, :p265, :p266, :p267, :p268, :p269, :p270, :p271),
(:p272, :p273, :p274, :p275, :p276, :p277, :p278, :p279, :p280, :p281, :p282, :p283, :p284, :p285, :p286, :p287, :p288, :p289, :p290, :p291, :p292, :p293, :p294, :p295, :p296, :p297, :p298),
(:p299, :p300, :p301, :p302, :p303, :p304, :p305, :p306, :p307, :p308, :p309, :p310, :p311, :p312, :p313, :p314, :p315, :p316, :p317, :p318, :p319, :p320, :p321, :p322, :p323, :p324, :p325),
(:p326, :p327, :p328, :p329, :p330, :p331, :p332, :p333, :p334, :p335, :p336, :p337, :p338, :p339, :p340, :p341, :p342, :p343, :p344, :p345, :p346, :p347, :p348, :p349, :p350, :p351, :p352),
(:p407, :p408, :p409, :p410, :p411, :p412, :p413, :p414, :p415, :p416, :p417, :p418, :p419, :p420, :p421, :p422, :p423, :p424, :p425, :p426, :p427, :p428, :p429, :p430, :p431, :p432, :p433) RETURN "CopySegmentationId","CoreColorId","IsDeleted","Labels","MToKgProportion","MergeOrderId","OrderDemandSegmentationIsReScheduled","OrderPlanSegmentationId","RedundantQuantity","SplitOrderId" INTO c9;
SELECT * FROM ARRAY c9;
END;
END;
Microsoft.EntityFrameworkCore.Database.Command: Debug: A data reader was disposed.
Microsoft.EntityFrameworkCore.Database.Connection: Debug: Closing connection to database 'DM8_TEST' on server '192.168.69.147'.
Microsoft.EntityFrameworkCore.Database.Connection: Debug: Closed connection to database '' on server '(null)'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: The 'OrderDemandSegmentationEntity' entity with key '{Id: 1008042100626857984}' tracked by 'SpasManufactureDbContext' changed from 'Added' to 'Unchanged'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: The 'OrderDemandSegmentationEntity' entity with key '{Id: 1008042097455964160}' tracked by 'SpasManufactureDbContext' changed from 'Added' to 'Unchanged'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: The 'OrderDemandSegmentationEntity' entity with key '{Id: 1008042094389927936}' tracked by 'SpasManufactureDbContext' changed from 'Added' to 'Unchanged'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: The 'OrderDemandSegmentationEntity' entity with key '{Id: 1008042091164508160}' tracked by 'SpasManufactureDbContext' changed from 'Added' to 'Unchanged'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: The 'OrderDemandSegmentationEntity' entity with key '{Id: 1008042087842619392}' tracked by 'SpasManufactureDbContext' changed from 'Added' to 'Unchanged'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: The 'OrderDemandSegmentationEntity' entity with key '{Id: 1008042084550090752}' tracked by 'SpasManufactureDbContext' changed from 'Added' to 'Unchanged'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: The 'OrderDemandSegmentationEntity' entity with key '{Id: 1008042081244979200}' tracked by 'SpasManufactureDbContext' changed from 'Added' to 'Unchanged'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: The 'OrderDemandSegmentationEntity' entity with key '{Id: 1008042075721080832}' tracked by 'SpasManufactureDbContext' changed from 'Added' to 'Unchanged'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: The 'OrderDemandSegmentationEntity' entity with key '{Id: 1008042070947962880}' tracked by 'SpasManufactureDbContext' changed from 'Added' to 'Unchanged'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: The 'OrderDemandSegmentationEntity' entity with key '{Id: 1008042100626857985}' tracked by 'SpasManufactureDbContext' changed from 'Added' to 'Unchanged'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: The 'OrderDemandSegmentationEntity' entity with key '{Id: 1008042100626857986}' tracked by 'SpasManufactureDbContext' changed from 'Added' to 'Unchanged'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: The 'OrderDemandSegmentationEntity' entity with key '{Id: 1008042103994884096}' tracked by 'SpasManufactureDbContext' changed from 'Added' to 'Unchanged'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: The 'OrderDemandSegmentationEntity' entity with key '{Id: 1008042103994884097}' tracked by 'SpasManufactureDbContext' changed from 'Added' to 'Unchanged'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: The 'OrderDemandSegmentationEntity' entity with key '{Id: 1008042103994884098}' tracked by 'SpasManufactureDbContext' changed from 'Added' to 'Unchanged'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: The 'OrderDemandSegmentationEntity' entity with key '{Id: 1008042107157389312}' tracked by 'SpasManufactureDbContext' changed from 'Added' to 'Unchanged'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: The 'OrderDemandSegmentationEntity' entity with key '{Id: 1008042107157389313}' tracked by 'SpasManufactureDbContext' changed from 'Added' to 'Unchanged'.
Microsoft.EntityFrameworkCore.Update: Debug: SaveChanges completed for 'SpasManufactureDbContext' with 16 entities written to the database.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: DetectChanges starting for 'SpasManufactureDbContext'.
Microsoft.EntityFrameworkCore.ChangeTracking: Debug: DetectChanges completed for 'SpasManufactureDbContext'.
Microsoft.EntityFrameworkCore.Update: Debug: SaveChanges starting for 'SpasManufactureDbContext'.
Microsoft.EntityFrameworkCore.Update: Debug: SaveChanges completed for 'SpasManufactureDbContext' with 0 entities written to the database.
关键:Microsoft.EntityFrameworkCore.Update: Debug: SaveChanges completed for 'SpasManufactureDbContext' with 0 entities written to the database.
没有成功插入数据
怎么让我用 TransactionScope 正常插入数据?
再次补充,设置dm.ini
ISOLATION_LEVEL=3
之后,还是一样的:
Operation is not valid due to the current state of the object
Microsoft.EntityFrameworkCore.Update: Debug: SaveChanges completed for 'SpasManufactureDbContext' with 0 entities written to the database.
把事务代码去掉就能正常插入数据。
参数按达梦的说明设置,绝大多数情况建议设置=1提交读隔离级别!并不支持=2呢~否则全库的并发性丧失,特需的事务在事务级别设置即可
字数限制,我再补充下。
代码:
using var transcation = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
我尝试连接字符串加上 enlist=true;
报错: