Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CAP
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
CAP
Commits
7971d672
Commit
7971d672
authored
Jul 25, 2019
by
Savorboard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix thread safe issue of ICapPublisher bug. (#371)
parent
075c8275
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
64 additions
and
48 deletions
+64
-48
ICapPublisher.MongoDB.cs
src/DotNetCore.CAP.MongoDB/ICapPublisher.MongoDB.cs
+4
-3
ICapTransaction.MongoDB.cs
src/DotNetCore.CAP.MongoDB/ICapTransaction.MongoDB.cs
+3
-1
ICapPublisher.MySql.cs
src/DotNetCore.CAP.MySql/ICapPublisher.MySql.cs
+3
-2
ICapTransaction.MySql.cs
src/DotNetCore.CAP.MySql/ICapTransaction.MySql.cs
+8
-4
ICapPublisher.PostgreSql.cs
src/DotNetCore.CAP.PostgreSql/ICapPublisher.PostgreSql.cs
+2
-2
ICapTransaction.PostgreSql.cs
src/DotNetCore.CAP.PostgreSql/ICapTransaction.PostgreSql.cs
+6
-3
ICapPublisher.SqlServer.cs
src/DotNetCore.CAP.SqlServer/ICapPublisher.SqlServer.cs
+2
-2
ICapTransaction.SqlServer.cs
src/DotNetCore.CAP.SqlServer/ICapTransaction.SqlServer.cs
+4
-2
CapPublisherBase.cs
src/DotNetCore.CAP/Abstractions/CapPublisherBase.cs
+25
-27
ICapPublisher.cs
src/DotNetCore.CAP/ICapPublisher.cs
+4
-1
CAP.BuilderTest.cs
test/DotNetCore.CAP.Test/CAP.BuilderTest.cs
+3
-1
No files found.
src/DotNetCore.CAP.MongoDB/ICapPublisher.MongoDB.cs
View file @
7971d672
...
...
@@ -28,8 +28,9 @@ namespace DotNetCore.CAP.MongoDB
await
PublishAsyncInternal
(
message
);
}
protected
override
Task
ExecuteAsync
(
CapPublishedMessage
message
,
ICapTransaction
transaction
,
CancellationToken
cancel
=
default
(
CancellationToken
))
protected
override
Task
ExecuteAsync
(
CapPublishedMessage
message
,
ICapTransaction
transaction
=
null
,
CancellationToken
cancel
=
default
)
{
var
insertOptions
=
new
InsertOneOptions
{
BypassDocumentValidation
=
false
};
...
...
@@ -49,7 +50,7 @@ namespace DotNetCore.CAP.MongoDB
Version
=
_options
.
Version
,
};
if
(
NotUseTransaction
)
if
(
transaction
==
null
)
{
return
collection
.
InsertOneAsync
(
store
,
insertOptions
,
cancel
);
}
...
...
src/DotNetCore.CAP.MongoDB/ICapTransaction.MongoDB.cs
View file @
7971d672
...
...
@@ -2,6 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for license information.
using
System.Diagnostics
;
using
Microsoft.Extensions.DependencyInjection
;
using
MongoDB.Driver
;
// ReSharper disable once CheckNamespace
...
...
@@ -70,7 +71,8 @@ namespace DotNetCore.CAP
ICapPublisher
publisher
,
bool
autoCommit
=
false
)
{
var
clientSessionHandle
=
client
.
StartSession
();
var
capTrans
=
publisher
.
Transaction
.
Begin
(
clientSessionHandle
,
autoCommit
);
publisher
.
Transaction
.
Value
=
publisher
.
ServiceProvider
.
GetService
<
CapTransactionBase
>();
var
capTrans
=
publisher
.
Transaction
.
Value
.
Begin
(
clientSessionHandle
,
autoCommit
);
return
new
CapMongoDbClientSessionHandle
(
capTrans
);
}
}
...
...
src/DotNetCore.CAP.MySql/ICapPublisher.MySql.cs
View file @
7971d672
...
...
@@ -29,10 +29,11 @@ namespace DotNetCore.CAP.MySql
await
PublishAsyncInternal
(
message
);
}
protected
override
async
Task
ExecuteAsync
(
CapPublishedMessage
message
,
ICapTransaction
transaction
,
protected
override
async
Task
ExecuteAsync
(
CapPublishedMessage
message
,
ICapTransaction
transaction
=
null
,
CancellationToken
cancel
=
default
(
CancellationToken
))
{
if
(
NotUseTransaction
)
if
(
transaction
==
null
)
{
using
(
var
connection
=
new
MySqlConnection
(
_options
.
ConnectionString
))
{
...
...
src/DotNetCore.CAP.MySql/ICapTransaction.MySql.cs
View file @
7971d672
...
...
@@ -3,15 +3,18 @@
using
System.Data
;
using
System.Diagnostics
;
using
System.Threading
;
using
Microsoft.EntityFrameworkCore.Infrastructure
;
using
Microsoft.EntityFrameworkCore.Storage
;
using
Microsoft.Extensions.DependencyInjection
;
// ReSharper disable once CheckNamespace
namespace
DotNetCore.CAP
{
public
class
MySqlCapTransaction
:
CapTransactionBase
{
public
MySqlCapTransaction
(
IDispatcher
dispatcher
)
:
base
(
dispatcher
)
public
MySqlCapTransaction
(
IDispatcher
dispatcher
)
:
base
(
dispatcher
)
{
}
...
...
@@ -28,7 +31,6 @@ namespace DotNetCore.CAP
dbContextTransaction
.
Commit
();
break
;
}
Flush
();
}
...
...
@@ -85,7 +87,8 @@ namespace DotNetCore.CAP
ICapPublisher
publisher
,
bool
autoCommit
=
false
)
{
var
trans
=
database
.
BeginTransaction
();
var
capTrans
=
publisher
.
Transaction
.
Begin
(
trans
,
autoCommit
);
publisher
.
Transaction
.
Value
=
publisher
.
ServiceProvider
.
GetService
<
CapTransactionBase
>();
var
capTrans
=
publisher
.
Transaction
.
Value
.
Begin
(
trans
,
autoCommit
);
return
new
CapEFDbTransaction
(
capTrans
);
}
...
...
@@ -105,7 +108,8 @@ namespace DotNetCore.CAP
}
var
dbTransaction
=
dbConnection
.
BeginTransaction
();
return
publisher
.
Transaction
.
Begin
(
dbTransaction
,
autoCommit
);
publisher
.
Transaction
.
Value
=
publisher
.
ServiceProvider
.
GetService
<
CapTransactionBase
>();
return
publisher
.
Transaction
.
Value
.
Begin
(
dbTransaction
,
autoCommit
);
}
}
}
\ No newline at end of file
src/DotNetCore.CAP.PostgreSql/ICapPublisher.PostgreSql.cs
View file @
7971d672
...
...
@@ -29,10 +29,10 @@ namespace DotNetCore.CAP.PostgreSql
await
PublishAsyncInternal
(
message
);
}
protected
override
async
Task
ExecuteAsync
(
CapPublishedMessage
message
,
ICapTransaction
transaction
,
protected
override
async
Task
ExecuteAsync
(
CapPublishedMessage
message
,
ICapTransaction
transaction
=
null
,
CancellationToken
cancel
=
default
(
CancellationToken
))
{
if
(
NotUseTransaction
)
if
(
transaction
==
null
)
{
using
(
var
connection
=
InitDbConnection
())
{
...
...
src/DotNetCore.CAP.PostgreSql/ICapTransaction.PostgreSql.cs
View file @
7971d672
...
...
@@ -5,6 +5,7 @@ using System.Data;
using
System.Diagnostics
;
using
Microsoft.EntityFrameworkCore.Infrastructure
;
using
Microsoft.EntityFrameworkCore.Storage
;
using
Microsoft.Extensions.DependencyInjection
;
// ReSharper disable once CheckNamespace
namespace
DotNetCore.CAP
...
...
@@ -90,7 +91,8 @@ namespace DotNetCore.CAP
}
var
dbTransaction
=
dbConnection
.
BeginTransaction
();
return
publisher
.
Transaction
.
Begin
(
dbTransaction
,
autoCommit
);
publisher
.
Transaction
.
Value
=
publisher
.
ServiceProvider
.
GetService
<
CapTransactionBase
>();
return
publisher
.
Transaction
.
Value
.
Begin
(
dbTransaction
,
autoCommit
);
}
/// <summary>
...
...
@@ -99,12 +101,13 @@ namespace DotNetCore.CAP
/// <param name="database">The <see cref="DatabaseFacade" />.</param>
/// <param name="publisher">The <see cref="ICapPublisher" />.</param>
/// <param name="autoCommit">Whether the transaction is automatically committed when the message is published</param>
/// <returns>The <see cref="IDbContextTransaction" /> of EF
dbc
ontext transaction object.</returns>
/// <returns>The <see cref="IDbContextTransaction" /> of EF
DbC
ontext transaction object.</returns>
public
static
IDbContextTransaction
BeginTransaction
(
this
DatabaseFacade
database
,
ICapPublisher
publisher
,
bool
autoCommit
=
false
)
{
var
trans
=
database
.
BeginTransaction
();
var
capTrans
=
publisher
.
Transaction
.
Begin
(
trans
,
autoCommit
);
publisher
.
Transaction
.
Value
=
publisher
.
ServiceProvider
.
GetService
<
CapTransactionBase
>();
var
capTrans
=
publisher
.
Transaction
.
Value
.
Begin
(
trans
,
autoCommit
);
return
new
CapEFDbTransaction
(
capTrans
);
}
}
...
...
src/DotNetCore.CAP.SqlServer/ICapPublisher.SqlServer.cs
View file @
7971d672
...
...
@@ -29,10 +29,10 @@ namespace DotNetCore.CAP.SqlServer
await
PublishAsyncInternal
(
message
);
}
protected
override
async
Task
ExecuteAsync
(
CapPublishedMessage
message
,
ICapTransaction
transaction
,
protected
override
async
Task
ExecuteAsync
(
CapPublishedMessage
message
,
ICapTransaction
transaction
=
null
,
CancellationToken
cancel
=
default
(
CancellationToken
))
{
if
(
NotUseTransaction
)
if
(
transaction
==
null
)
{
using
(
var
connection
=
new
SqlConnection
(
_options
.
ConnectionString
))
{
...
...
src/DotNetCore.CAP.SqlServer/ICapTransaction.SqlServer.cs
View file @
7971d672
...
...
@@ -150,7 +150,8 @@ namespace DotNetCore.CAP
}
var
dbTransaction
=
dbConnection
.
BeginTransaction
();
var
capTransaction
=
publisher
.
Transaction
.
Begin
(
dbTransaction
,
autoCommit
);
publisher
.
Transaction
.
Value
=
publisher
.
ServiceProvider
.
GetService
<
CapTransactionBase
>();
var
capTransaction
=
publisher
.
Transaction
.
Value
.
Begin
(
dbTransaction
,
autoCommit
);
return
(
IDbTransaction
)
capTransaction
.
DbTransaction
;
}
...
...
@@ -165,7 +166,8 @@ namespace DotNetCore.CAP
ICapPublisher
publisher
,
bool
autoCommit
=
false
)
{
var
trans
=
database
.
BeginTransaction
();
var
capTrans
=
publisher
.
Transaction
.
Begin
(
trans
,
autoCommit
);
publisher
.
Transaction
.
Value
=
publisher
.
ServiceProvider
.
GetService
<
CapTransactionBase
>();
var
capTrans
=
publisher
.
Transaction
.
Value
.
Begin
(
trans
,
autoCommit
);
return
new
CapEFDbTransaction
(
capTrans
);
}
}
...
...
src/DotNetCore.CAP/Abstractions/CapPublisherBase.cs
View file @
7971d672
...
...
@@ -7,7 +7,6 @@ using System.Threading;
using
System.Threading.Tasks
;
using
DotNetCore.CAP.Diagnostics
;
using
DotNetCore.CAP.Infrastructure
;
using
DotNetCore.CAP.Internal
;
using
DotNetCore.CAP.Models
;
using
Microsoft.Extensions.DependencyInjection
;
...
...
@@ -17,9 +16,7 @@ namespace DotNetCore.CAP.Abstractions
{
private
readonly
IMessagePacker
_msgPacker
;
private
readonly
IContentSerializer
_serializer
;
private
CapTransactionBase
_transaction
;
protected
bool
NotUseTransaction
;
private
readonly
IDispatcher
_dispatcher
;
// ReSharper disable once InconsistentNaming
protected
static
readonly
DiagnosticListener
s_diagnosticListener
=
...
...
@@ -28,13 +25,15 @@ namespace DotNetCore.CAP.Abstractions
protected
CapPublisherBase
(
IServiceProvider
service
)
{
ServiceProvider
=
service
;
_dispatcher
=
service
.
GetRequiredService
<
IDispatcher
>();
_msgPacker
=
service
.
GetRequiredService
<
IMessagePacker
>();
_serializer
=
service
.
GetRequiredService
<
IContentSerializer
>();
Transaction
=
new
AsyncLocal
<
ICapTransaction
>();
}
p
rotected
IServiceProvider
ServiceProvider
{
get
;
}
p
ublic
IServiceProvider
ServiceProvider
{
get
;
}
public
ICapTransaction
Transaction
=>
_transaction
??
(
_transaction
=
ServiceProvider
.
GetRequiredService
<
CapTransactionBase
>());
public
AsyncLocal
<
ICapTransaction
>
Transaction
{
get
;
}
public
void
Publish
<
T
>(
string
name
,
T
contentObj
,
string
callbackName
=
null
)
{
...
...
@@ -65,27 +64,33 @@ namespace DotNetCore.CAP.Abstractions
protected
async
Task
PublishAsyncInternal
(
CapPublishedMessage
message
)
{
if
(
Transaction
.
DbTransaction
==
null
)
{
NotUseTransaction
=
true
;
Transaction
.
DbTransaction
=
new
NoopTransaction
();
}
Guid
operationId
=
default
(
Guid
);
var
operationId
=
default
(
Guid
);
try
{
operationId
=
s_diagnosticListener
.
WritePublishMessageStoreBefore
(
message
);
await
ExecuteAsync
(
message
,
Transaction
);
_transaction
.
AddToSent
(
message
);
if
(
Transaction
.
Value
?.
DbTransaction
==
null
)
{
await
ExecuteAsync
(
message
);
s_diagnosticListener
.
WritePublishMessageStoreAfter
(
operationId
,
message
);
s_diagnosticListener
.
WritePublishMessageStoreAfter
(
operationId
,
message
);
if
(
NotUseTransaction
||
Transaction
.
AutoCommit
)
_dispatcher
.
EnqueueToPublish
(
message
);
}
else
{
_transaction
.
Commit
();
var
transaction
=
(
CapTransactionBase
)
Transaction
.
Value
;
await
ExecuteAsync
(
message
,
transaction
);
s_diagnosticListener
.
WritePublishMessageStoreAfter
(
operationId
,
message
);
transaction
.
AddToSent
(
message
);
if
(
transaction
.
AutoCommit
)
{
transaction
.
Commit
();
}
}
}
catch
(
Exception
e
)
...
...
@@ -94,17 +99,10 @@ namespace DotNetCore.CAP.Abstractions
throw
;
}
finally
{
if
(
NotUseTransaction
||
Transaction
.
AutoCommit
)
{
_transaction
?.
Dispose
();
}
}
}
protected
abstract
Task
ExecuteAsync
(
CapPublishedMessage
message
,
ICapTransaction
transaction
,
ICapTransaction
transaction
=
null
,
CancellationToken
cancel
=
default
(
CancellationToken
));
protected
virtual
string
Serialize
<
T
>(
T
obj
,
string
callbackName
=
null
)
...
...
src/DotNetCore.CAP/ICapPublisher.cs
View file @
7971d672
// Copyright (c) .NET Core Community. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using
System
;
using
System.Threading
;
using
System.Threading.Tasks
;
...
...
@@ -11,10 +12,12 @@ namespace DotNetCore.CAP
/// </summary>
public
interface
ICapPublisher
{
IServiceProvider
ServiceProvider
{
get
;
}
/// <summary>
/// CAP transaction context object
/// </summary>
ICapTransaction
Transaction
{
get
;
}
AsyncLocal
<
ICapTransaction
>
Transaction
{
get
;
}
/// <summary>
/// Asynchronous publish an object message.
...
...
test/DotNetCore.CAP.Test/CAP.BuilderTest.cs
View file @
7971d672
...
...
@@ -120,7 +120,9 @@ namespace DotNetCore.CAP.Test
private
class
MyProducerService
:
ICapPublisher
{
public
ICapTransaction
Transaction
{
get
;
}
public
IServiceProvider
ServiceProvider
{
get
;
}
public
AsyncLocal
<
ICapTransaction
>
Transaction
{
get
;
}
public
Task
PublishAsync
<
T
>(
string
name
,
T
contentObj
,
string
callbackName
=
null
,
CancellationToken
cancellationToken
=
default
(
CancellationToken
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment