Skip to content

Commit

Permalink
🐛 增加SqlSugar 使用SQLite 在Quartz锁库测试
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonNoCry committed Jun 12, 2024
1 parent 35290f5 commit f400a43
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 35 deletions.
46 changes: 23 additions & 23 deletions Blog.Core.Api/Blog.Core.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Blog.Core.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
{
"ConnId": "Main2",
"DBType": 2,
"Enabled": true,
"Enabled": false,
"Connection": "WMBlog3.db", //sqlite只写数据库名就行
"Slaves": [
{
Expand Down Expand Up @@ -265,7 +265,7 @@
"Enabled": false
},
"IpRateLimit": {
"Enabled": true
"Enabled": false
},
"EncryptionResponse": {
"Enabled": true,
Expand Down
2 changes: 1 addition & 1 deletion Blog.Core.Api/wwwroot/BlogCore.Data.json/TasksQz.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"Name": "博客管理",
"JobGroup": "博客测试组",
"TriggerType": 1,
"Cron": "0 */5 * * * ?",
"Cron": "0 */1 * * * ?",
"AssemblyName": "Blog.Core.Tasks",
"ClassName": "Job_Blogs_Quartz",
"Remark": "",
Expand Down
2 changes: 2 additions & 0 deletions Blog.Core.IServices/IGuestbookServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public partial interface IGuestbookServices : IBaseServices<Guestbook>
Task<bool> TestTranPropagationNoTran();

Task<bool> TestTranPropagationTran();
Task TestTranPropagationTran2();
Task TestTranPropagationTran3();
}
}
1 change: 1 addition & 0 deletions Blog.Core.IServices/IPasswordLibServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public partial interface IPasswordLibServices :IBaseServices<PasswordLib>
Task<bool> TestTranPropagation2();
Task<bool> TestTranPropagationNoTranError();
Task<bool> TestTranPropagationTran2();
Task<bool> TestTranPropagationTran3();
}
}
4 changes: 4 additions & 0 deletions Blog.Core.Repository/UnitOfWorks/UnitOfWorkManage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,25 @@ public UnitOfWork CreateUnitOfWork()

public void BeginTran()
{
Console.WriteLine("Begin Transaction Before:" + GetDbClient().ContextID);
lock (this)
{
_tranCount++;
GetDbClient().BeginTran();
}
Console.WriteLine("Begin Transaction After:" + GetDbClient().ContextID);
}

public void BeginTran(MethodInfo method)
{
Console.WriteLine("Begin Transaction Before:" + GetDbClient().ContextID);
lock (this)
{
GetDbClient().BeginTran();
TranStack.Push(method.GetFullName());
_tranCount = TranStack.Count;
}
Console.WriteLine("Begin Transaction After:" + GetDbClient().ContextID);
}

public void CommitTran()
Expand Down
74 changes: 71 additions & 3 deletions Blog.Core.Services/GuestbookServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@
using System.Threading.Tasks;
using Blog.Core.Common.DB;
using Blog.Core.Repository.UnitOfWorks;
using SqlSugar;

namespace Blog.Core.Services
{
public class GuestbookServices : BaseServices<Guestbook>, IGuestbookServices
{
private readonly IUnitOfWorkManage _unitOfWorkManage;
private readonly IBaseRepository<PasswordLib> _passwordLibRepository;

private readonly IPasswordLibServices _passwordLibServices;
private readonly ISqlSugarClient _db;
private SqlSugarScope db => _db as SqlSugarScope;

public GuestbookServices(IUnitOfWorkManage unitOfWorkManage, IBaseRepository<Guestbook> dal, IBaseRepository<PasswordLib> passwordLibRepository, IPasswordLibServices passwordLibServices)
public GuestbookServices(IUnitOfWorkManage unitOfWorkManage, IBaseRepository<Guestbook> dal,
IBaseRepository<PasswordLib> passwordLibRepository, IPasswordLibServices passwordLibServices, ISqlSugarClient db)
{
_unitOfWorkManage = unitOfWorkManage;
_passwordLibRepository = passwordLibRepository;
_passwordLibServices = passwordLibServices;
_db = db;
}

public async Task<MessageModel<string>> TestTranInRepository()
Expand Down Expand Up @@ -193,8 +197,9 @@ public async Task<bool> TestTranPropagationNoTran()
public async Task<bool> TestTranPropagationTran()
{
var guestbooks = await base.Query();
guestbooks = await base.Query();
Console.WriteLine($"first time : the count of guestbooks is :{guestbooks.Count}");

Console.WriteLine(base.Db.ContextID);
var insertGuestbook = await base.Add(new Guestbook()
{
username = "bbb",
Expand All @@ -207,5 +212,68 @@ public async Task<bool> TestTranPropagationTran()

return true;
}

[UseTran(Propagation = Propagation.Required)]
public async Task TestTranPropagationTran2()
{
await Db.Insertable(new Guestbook()
{
username = "bbb",
blogId = 1,
createdate = DateTime.Now,
isshow = true
}).ExecuteReturnSnowflakeIdAsync();


await Db.Insertable(new PasswordLib()
{
PLID = SnowFlakeSingle.Instance.NextId(),
IsDeleted = false,
plAccountName = "aaa",
plCreateTime = DateTime.Now
}).ExecuteReturnSnowflakeIdAsync();

await _passwordLibServices.TestTranPropagationTran2();

Console.WriteLine("完成");
}

public async Task TestTranPropagationTran3()
{
try
{
Console.WriteLine("Begin Transaction Before:" + db.ContextID);
db.BeginTran();
Console.WriteLine("Begin Transaction After:" + db.ContextID);

await db.Insertable(new Guestbook()
{
username = "bbb",
blogId = 1,
createdate = DateTime.Now,
isshow = true
}).ExecuteReturnSnowflakeIdAsync();


await db.Insertable(new PasswordLib()
{
PLID = SnowFlakeSingle.Instance.NextId(),
IsDeleted = false,
plAccountName = "aaa",
plCreateTime = DateTime.Now
}).ExecuteReturnSnowflakeIdAsync();

await _passwordLibServices.TestTranPropagationTran3();

db.CommitTran();
Console.WriteLine("完成");
}
catch (Exception e)
{
db.RollbackTran();
throw;
}

}
}
}
34 changes: 31 additions & 3 deletions Blog.Core.Services/PasswordLibServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@
using Blog.Core.IRepository.Base;
using Blog.Core.IServices;
using Blog.Core.Model.Models;
using Blog.Core.Repository.UnitOfWorks;
using Blog.Core.Services.BASE;
using SqlSugar;

namespace Blog.Core.Services
{
public partial class PasswordLibServices : BaseServices<PasswordLib>, IPasswordLibServices
{
IBaseRepository<PasswordLib> _dal;
private readonly IUnitOfWorkManage _unitOfWorkManage;
private readonly ISqlSugarClient _db;
private SqlSugarScope db => _db as SqlSugarScope;

public PasswordLibServices(IBaseRepository<PasswordLib> dal)
public PasswordLibServices(IBaseRepository<PasswordLib> dal, IUnitOfWorkManage unitOfWorkManage, ISqlSugarClient db)
{
this._dal = dal;
_unitOfWorkManage = unitOfWorkManage;
_db = db;
base.BaseDal = dal;
}

Expand All @@ -24,6 +31,7 @@ public async Task<bool> TestTranPropagation2()
{
await _dal.Add(new PasswordLib()
{
PLID = SnowFlakeSingle.Instance.NextId(),
IsDeleted = false,
plAccountName = "aaa",
plCreateTime = DateTime.Now
Expand All @@ -48,13 +56,33 @@ await _dal.Add(new PasswordLib()
[UseTran(Propagation = Propagation.Nested)]
public async Task<bool> TestTranPropagationTran2()
{
await _dal.Add(new PasswordLib()
await db.Insertable(new PasswordLib()
{
PLID = SnowFlakeSingle.Instance.NextId(),
IsDeleted = false,
plAccountName = "aaa",
plCreateTime = DateTime.Now
});
}).ExecuteReturnSnowflakeIdAsync();

throw new Exception("123");
return true;
}

public async Task<bool> TestTranPropagationTran3()
{
Console.WriteLine("Begin Transaction Before:" + db.ContextID);
db.BeginTran();
Console.WriteLine("Begin Transaction After:" + db.ContextID);
Console.WriteLine("");
await db.Insertable(new PasswordLib()
{
PLID = SnowFlakeSingle.Instance.NextId(),
IsDeleted = false,
plAccountName = "aaa",
plCreateTime = DateTime.Now
}).ExecuteReturnSnowflakeIdAsync();

throw new Exception("123");
return true;
}
}
Expand Down
Loading

0 comments on commit f400a43

Please sign in to comment.