Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
Dapper
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
Dapper
Commits
3b8b0cdc
Commit
3b8b0cdc
authored
Aug 30, 2019
by
mgravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add APIs for working with the connection pools and client-id
parent
16ed3fd9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
187 additions
and
1 deletion
+187
-1
DbConnectionExtensions.cs
Dapper.ProviderTools/DbConnectionExtensions.cs
+129
-0
ProviderTests.cs
Dapper.Tests/ProviderTests.cs
+58
-1
No files found.
Dapper.ProviderTools/DbConnectionExtensions.cs
0 → 100644
View file @
3b8b0cdc
using
System
;
using
System.Collections.Concurrent
;
using
System.Data.Common
;
using
System.Linq.Expressions
;
using
System.Reflection
;
namespace
Dapper.ProviderTools
{
/// <summary>
/// Helper utilties for working with database connections
/// </summary>
public
static
class
DbConnectionExtensions
{
/// <summary>
/// Attempt to get the client connection id for a given connection
/// </summary>
public
static
bool
TryGetClientConnectionId
(
this
DbConnection
connection
,
out
Guid
clientConnectionId
)
{
clientConnectionId
=
default
;
return
connection
!=
null
&&
ByTypeHelpers
.
Get
(
connection
.
GetType
()).
TryGetClientConnectionId
(
connection
,
out
clientConnectionId
);
}
/// <summary>
/// Clear all pools associated with the provided connection type
/// </summary>
public
static
bool
TryClearAllPools
(
this
DbConnection
connection
)
=>
connection
!=
null
&&
ByTypeHelpers
.
Get
(
connection
.
GetType
()).
TryClearAllPools
();
/// <summary>
/// Clear the pools associated with the provided connection
/// </summary>
public
static
bool
TryClearPool
(
this
DbConnection
connection
)
=>
connection
!=
null
&&
ByTypeHelpers
.
Get
(
connection
.
GetType
()).
TryClearPool
(
connection
);
private
sealed
class
ByTypeHelpers
{
private
static
readonly
ConcurrentDictionary
<
Type
,
ByTypeHelpers
>
s_byType
=
new
ConcurrentDictionary
<
Type
,
ByTypeHelpers
>();
private
readonly
Func
<
DbConnection
,
Guid
>?
_getClientConnectionId
;
private
readonly
Action
<
DbConnection
>?
_clearPool
;
private
readonly
Action
?
_clearAllPools
;
public
bool
TryGetClientConnectionId
(
DbConnection
connection
,
out
Guid
clientConnectionId
)
{
if
(
_getClientConnectionId
==
null
)
{
clientConnectionId
=
default
;
return
false
;
}
clientConnectionId
=
_getClientConnectionId
(
connection
);
return
true
;
}
public
bool
TryClearPool
(
DbConnection
connection
)
{
if
(
_clearPool
==
null
)
return
false
;
_clearPool
(
connection
);
return
true
;
}
public
bool
TryClearAllPools
()
{
if
(
_clearAllPools
==
null
)
return
false
;
_clearAllPools
();
return
true
;
}
public
static
ByTypeHelpers
Get
(
Type
type
)
{
if
(!
s_byType
.
TryGetValue
(
type
,
out
var
value
))
{
s_byType
[
type
]
=
value
=
new
ByTypeHelpers
(
type
);
}
return
value
;
}
private
ByTypeHelpers
(
Type
type
)
{
_getClientConnectionId
=
TryGetInstanceProperty
<
Guid
>(
"ClientConnectionId"
,
type
);
try
{
var
clearAllPools
=
type
.
GetMethod
(
"ClearAllPools"
,
BindingFlags
.
Public
|
BindingFlags
.
Static
,
null
,
Type
.
EmptyTypes
,
null
);
if
(
clearAllPools
!=
null
)
{
_clearAllPools
=
(
Action
)
Delegate
.
CreateDelegate
(
typeof
(
Action
),
clearAllPools
);
}
}
catch
{
}
try
{
var
clearPool
=
type
.
GetMethod
(
"ClearPool"
,
BindingFlags
.
Public
|
BindingFlags
.
Static
,
null
,
new
[]
{
type
},
null
);
if
(
clearPool
!=
null
)
{
var
p
=
Expression
.
Parameter
(
typeof
(
DbConnection
),
"connection"
);
var
body
=
Expression
.
Call
(
clearPool
,
Expression
.
Convert
(
p
,
type
));
var
lambda
=
Expression
.
Lambda
<
Action
<
DbConnection
>>(
body
,
p
);
_clearPool
=
lambda
.
Compile
();
}
}
catch
{
}
}
private
static
Func
<
DbConnection
,
T
>?
TryGetInstanceProperty
<
T
>(
string
name
,
Type
type
)
{
try
{
var
prop
=
type
.
GetProperty
(
name
,
BindingFlags
.
Public
|
BindingFlags
.
Instance
);
if
(
prop
==
null
||
!
prop
.
CanRead
)
return
null
;
if
(
prop
.
PropertyType
!=
typeof
(
T
))
return
null
;
var
p
=
Expression
.
Parameter
(
typeof
(
DbConnection
),
"connection"
);
var
body
=
Expression
.
Property
(
Expression
.
Convert
(
p
,
type
),
prop
);
var
lambda
=
Expression
.
Lambda
<
Func
<
DbConnection
,
T
>>(
body
,
p
);
return
lambda
.
Compile
();
}
catch
{
return
null
;
}
}
}
}
}
Dapper.Tests/ProviderTests.cs
View file @
3b8b0cdc
using
System.Data.Common
;
using
System
;
using
System.Data.Common
;
using
Dapper.ProviderTools
;
using
Xunit
;
...
...
@@ -24,6 +25,62 @@ public void BulkCopy_MicrosoftDataSqlClient()
}
}
[
Fact
]
public
void
ClientId_SystemDataSqlClient
()
=>
TestClientId
<
SystemSqlClientProvider
>();
[
Fact
]
public
void
ClientId_MicrosoftDataSqlClient
()
=>
TestClientId
<
MicrosoftSqlClientProvider
>();
[
Fact
]
public
void
ClearPool_SystemDataSqlClient
()
=>
ClearPool
<
SystemSqlClientProvider
>();
[
Fact
]
public
void
ClearPool_MicrosoftDataSqlClient
()
=>
ClearPool
<
MicrosoftSqlClientProvider
>();
[
Fact
]
public
void
ClearAllPools_SystemDataSqlClient
()
=>
ClearAllPools
<
SystemSqlClientProvider
>();
[
Fact
]
public
void
ClearAllPools_MicrosoftDataSqlClient
()
=>
ClearAllPools
<
MicrosoftSqlClientProvider
>();
private
static
void
TestClientId
<
T
>()
where
T
:
SqlServerDatabaseProvider
,
new
()
{
var
provider
=
new
T
();
using
(
var
conn
=
provider
.
GetOpenConnection
())
{
Assert
.
True
(
conn
.
TryGetClientConnectionId
(
out
var
id
));
Assert
.
NotEqual
(
Guid
.
Empty
,
id
);
}
}
private
static
void
ClearPool
<
T
>()
where
T
:
SqlServerDatabaseProvider
,
new
()
{
var
provider
=
new
T
();
using
(
var
conn
=
provider
.
GetOpenConnection
())
{
Assert
.
True
(
conn
.
TryClearPool
());
}
}
private
static
void
ClearAllPools
<
T
>()
where
T
:
SqlServerDatabaseProvider
,
new
()
{
var
provider
=
new
T
();
using
(
var
conn
=
provider
.
GetOpenConnection
())
{
Assert
.
True
(
conn
.
TryClearAllPools
());
}
}
private
static
void
Test
<
T
>(
DbConnection
connection
)
{
using
(
var
bcp
=
BulkCopy
.
TryCreate
(
connection
))
...
...
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