【发布时间】:2021-12-25 11:34:58
【问题描述】:
我有这个代码:
public class MyService : IMyService
{
public EarningService(IDbConnection db)
{
Db = db;
}
public async Task SomeMethodWithTransacion()
{
Db.Open();
var tran = Db.BeginTransaction();
try
{
string sqlInsert1 = "insert into someTable1";
await Db.ExecuteAsync(sqlInsert1, new {param1});
string sqlInsert2 = "insert into someTable2";
await Db.ExecuteAsync(sqlInsert2, new {param2})
var field1 = GetSomeField1(param);
string updateQuery1 = @"UPDATE SOMETABLE1 SET someField = 'someValue' WHERE Id = @Id";
Db.Execute(updateQuery1, new { Id });
string sqlInsert3 = "insert into someTable3";
await Db.ExecuteAsync(sqlInsert3, new {param3});
var field2 = GetField2(param);
var field3 = GetField3(param);
var field4 = GetField4(param);
string sqlInsert4 = "insert into someTable4";
await Db.ExecuteAsync(sqlInsert4, new {param4});
var sqlSP = "[AddAssets]";
await Db.ExecuteAsync(sqlSP, new { @owner= @owner, @rvalue = value }, tran,
commandType: CommandType.StoredProcedure);
tran.Commit();
return;
}
catch (Exception e)
{
tran.Rollback();
return 500;
}
}
}
//startup
public void ConfigureServices(IServiceCollection services)
{
string dbConnectionString = this.Configuration.GetConnectionString("Default");
// Inject IDbConnection, with implementation from SqlConnection class.
services.AddTransient<IDbConnection>((sp) => new SqlConnection(dbConnectionString));
}
但是我在执行下面的代码时遇到了问题:
var sqlSP = "[AddAssets]";
await Db.ExecuteAsync(sqlSP, new { @owner= @owner, @rvalue = value }, tran,
commandType: CommandType.StoredProcedure);
存储过程:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AddAssets]
@owner uniqueidentifier,
@rvalue nvarchar(MAX)
AS
BEGIN
DECLARE @currentBal decimal = (SELECT TOP (1) [Balance]
FROM [dbo].[Assets]
WHERE [Owner] = @owner AND [Status]=1)
IF @currentBal >= 0
BEGIN
DECLARE @assetTable TABLE
(
[Id] [int] NOT NULL,
[Owner] [uniqueidentifier] NULL,
[Balance] [decimal](18, 2) NULL,
[Status] [smallint] NULL
);
INSERT INTO @assetTable
SELECT TOP 1
[Id], [Owner], [Balance], [Status]
FROM [dbo].[Assets] WITH (UPDLOCK)
WHERE [Owner] = @owner AND [Status] = 1;
INSERT INTO [dbo].[Assets] ([Owner], [Balance], [Status])
VALUES (@owner, @rvalue, 1)
UPDATE [dbo].[Assets]
SET [Status] = 2
WHERE [Id] = (SELECT TOP 1 [Id] FROM @assetTable
WHERE [Owner] = @owner);
END
我确实使用 jmeter 对该方法进行了负载测试,但得到了以下预期结果:
| Id | Owner | Balance | Status | RV |
|---|---|---|---|---|
| 2 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 102.00 | 1 | 0x000000000007186D |
| 12 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 103.00 | 1 | 0x000000000007186F |
| 13 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 103.00 | 1 | 0x0000000000071871 |
| 14 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 103.00 | 1 | 0x0000000000071873 |
| 15 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 103.00 | 1 | 0x0000000000071875 |
| 16 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 61.00 | 1 | 0x0000000000071877 |
| 17 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 86.00 | 1 | 0x0000000000071879 |
| 18 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 88.00 | 1 | 0x000000000007187B |
| 19 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 77.00 | 1 | 0x000000000007187D |
| 20 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 51.00 | 1 | 0x000000000007187F |
当我预期结果是只有 1 行的状态 = 1 对于 1 个所有者:
| Id | Owner | Balance | Status | RV |
|---|---|---|---|---|
| 2 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 102.00 | 1 | 0x000000000007186D |
| 12 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 103.00 | 2 | 0x000000000007186F |
| 13 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 103.00 | 2 | 0x0000000000071871 |
| 14 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 103.00 | 2 | 0x0000000000071873 |
| 15 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 103.00 | 2 | 0x0000000000071875 |
| 16 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 61.00 | 2 | 0x0000000000071877 |
| 17 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 86.00 | 2 | 0x0000000000071879 |
| 18 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 88.00 | 2 | 0x000000000007187B |
| 19 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 77.00 | 2 | 0x000000000007187D |
| 20 | 3B09C822-E613-454B-A6AA-53FA68363F74 | 51.00 | 2 | 0x000000000007187F |
请有人帮我解决这个问题。
【问题讨论】:
标签: c# sql-server asp.net-core