mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
update demo
This commit is contained in:
parent
3339d96117
commit
4d91d87660
@ -70,9 +70,9 @@ namespace aspnetcore_transaction.Controllers
|
|||||||
_repoSong2.Insert(new Song());
|
_repoSong2.Insert(new Song());
|
||||||
}
|
}
|
||||||
[Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的
|
[Transactional(Propagation = Propagation.Nested)] //sqlite 不能嵌套事务,会锁库的
|
||||||
async public Task Test11()
|
public Task Test11()
|
||||||
{
|
{
|
||||||
await Task.Delay(TimeSpan.FromSeconds(10)).ContinueWith(t =>
|
return Task.Delay(TimeSpan.FromSeconds(1)).ContinueWith(t =>
|
||||||
_repoSong.InsertAsync(new Song()));
|
_repoSong.InsertAsync(new Song()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ using Rougamo.Context;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -18,8 +19,7 @@ namespace FreeSql
|
|||||||
IsolationLevel? m_IsolationLevel;
|
IsolationLevel? m_IsolationLevel;
|
||||||
|
|
||||||
static AsyncLocal<IServiceProvider> m_ServiceProvider = new AsyncLocal<IServiceProvider>();
|
static AsyncLocal<IServiceProvider> m_ServiceProvider = new AsyncLocal<IServiceProvider>();
|
||||||
public static void SetServiceProvider(IServiceProvider serviceProvider) =>
|
public static void SetServiceProvider(IServiceProvider serviceProvider) => m_ServiceProvider.Value = serviceProvider;
|
||||||
m_ServiceProvider.Value = serviceProvider;
|
|
||||||
|
|
||||||
IUnitOfWork _uow;
|
IUnitOfWork _uow;
|
||||||
public override void OnEntry(MethodContext context)
|
public override void OnEntry(MethodContext context)
|
||||||
@ -29,14 +29,24 @@ namespace FreeSql
|
|||||||
}
|
}
|
||||||
public override void OnExit(MethodContext context)
|
public override void OnExit(MethodContext context)
|
||||||
{
|
{
|
||||||
try
|
if (typeof(Task).IsAssignableFrom(context.RealReturnType))
|
||||||
{
|
{
|
||||||
if (context.Exception == null) _uow.Commit();
|
((Task)context.ReturnValue).ContinueWith(t => _OnExit());
|
||||||
else _uow.Rollback();
|
return;
|
||||||
}
|
}
|
||||||
finally
|
_OnExit();
|
||||||
|
|
||||||
|
void _OnExit()
|
||||||
{
|
{
|
||||||
_uow.Dispose();
|
try
|
||||||
|
{
|
||||||
|
if (context.Exception == null) _uow.Commit();
|
||||||
|
else _uow.Rollback();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_uow.Dispose();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user