提交 7766b293 编写于 作者: s0611163's avatar s0611163

ISession接口增加 从连接池池获取连接方法 供直接调用Dapper方法使用。

上级 f47e6938
...@@ -210,5 +210,23 @@ namespace Dapper.LiteSql ...@@ -210,5 +210,23 @@ namespace Dapper.LiteSql
} }
#endregion #endregion
#region 从连接池池获取连接
/// <summary>
/// 从连接池池获取连接
/// </summary>
public DbConnectionExt GetConnection(DbTransactionExt _tran = null)
{
return _connFactory.GetConnection(_tran);
}
/// <summary>
/// 从连接池池获取连接
/// </summary>
public Task<DbConnectionExt> GetConnectionAsync(DbTransactionExt _tran = null)
{
return _connFactory.GetConnectionAsync(_tran);
}
#endregion
} }
} }
...@@ -12,10 +12,11 @@ namespace Dapper.LiteSql ...@@ -12,10 +12,11 @@ namespace Dapper.LiteSql
/// <summary> /// <summary>
/// 开始事务 /// 开始事务
/// </summary> /// </summary>
public void BeginTransaction() public DbTransactionExt BeginTransaction()
{ {
_conn = _connFactory.GetConnection(null); _conn = _connFactory.GetConnection(null);
_tran = new DbTransactionExt(_conn.Conn.BeginTransaction(), _conn); _tran = new DbTransactionExt(_conn.Conn.BeginTransaction(), _conn);
return _tran;
} }
#endregion #endregion
......
...@@ -60,5 +60,17 @@ namespace Dapper.LiteSql ...@@ -60,5 +60,17 @@ namespace Dapper.LiteSql
SqlValue ForList(IList list); SqlValue ForList(IList list);
#endregion #endregion
#region 从连接池池获取连接
/// <summary>
/// 从连接池池获取连接
/// </summary>
DbConnectionExt GetConnection(DbTransactionExt _tran = null);
/// <summary>
/// 从连接池池获取连接
/// </summary>
Task<DbConnectionExt> GetConnectionAsync(DbTransactionExt _tran = null);
#endregion
} }
} }
...@@ -12,7 +12,7 @@ namespace Dapper.LiteSql ...@@ -12,7 +12,7 @@ namespace Dapper.LiteSql
/// <summary> /// <summary>
/// 开始事务 /// 开始事务
/// </summary> /// </summary>
void BeginTransaction(); DbTransactionExt BeginTransaction();
#endregion #endregion
#region 提交事务 #region 提交事务
......
...@@ -36,6 +36,9 @@ ...@@ -36,6 +36,9 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Dapper, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapper.2.0.123\lib\net461\Dapper.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration" /> <Reference Include="System.Configuration" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
...@@ -57,6 +60,7 @@ ...@@ -57,6 +60,7 @@
<Compile Include="AsyncTest.cs" /> <Compile Include="AsyncTest.cs" />
<Compile Include="BatchInsertTest.cs" /> <Compile Include="BatchInsertTest.cs" />
<Compile Include="BatchUpdateTest.cs" /> <Compile Include="BatchUpdateTest.cs" />
<Compile Include="DapperTest.cs" />
<Compile Include="DeleteTest.cs" /> <Compile Include="DeleteTest.cs" />
<Compile Include="InsertTest.cs" /> <Compile Include="InsertTest.cs" />
<Compile Include="LambdaTest.cs" /> <Compile Include="LambdaTest.cs" />
...@@ -88,6 +92,7 @@ ...@@ -88,6 +92,7 @@
<None Include="App.config"> <None Include="App.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup /> <ItemGroup />
<Choose> <Choose>
......
using DAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using Models;
using Utils;
using Dapper;
using static Dapper.SqlMapper;
using System.Data.Common;
using System.Threading;
namespace Dapper.LiteSqlTest
{
[TestClass]
public class DapperTest
{
#region 构造函数
public DapperTest()
{
LiteSqlFactory.GetSession();
}
#endregion
#region 测试直接使用Dapper
[TestMethod]
public void TestUseDapper()
{
var session = LiteSqlFactory.GetSession();
using (var conn = session.GetConnection())
{
DynamicParameters dynamicParameters = new DynamicParameters();
dynamicParameters.Add("id", 20);
List<SysUser> list = conn.Conn.Query<SysUser>(@"
select id, user_name as UserName, real_name as RealName,
password, remark,
create_userid as CreateUserid, create_time as CreateTime,
update_userid as UpdateUserid, update_time as UpdateTime
from sys_user
where id < @id", dynamicParameters).ToList();
foreach (SysUser item in list)
{
Console.WriteLine(ModelToStringUtil.ToString(item));
}
}
}
#endregion
#region 测试混合并发使用DapperLiteSql
[TestMethod]
public void TestUseDapper2()
{
ThreadPool.SetMinThreads(50, 50);
Console.WriteLine("开始");
List<Task> tasks = new List<Task>();
for (int i = 0; i < 200; i++)
{
var task = Task.Run(() =>
{
try
{
var session = LiteSqlFactory.GetSession();
var list = session.Queryable<SysUser>().Where(t => t.Id < 20).ToList();
Console.WriteLine("Dapper.LiteSql查询成功, count=" + list.Count);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
});
tasks.Add(task);
var task2 = Task.Run(() =>
{
try
{
var session = LiteSqlFactory.GetSession();
using (var conn = session.GetConnection())
{
DynamicParameters dynamicParameters = new DynamicParameters();
dynamicParameters.Add("id", 20);
List<SysUser> list = conn.Conn.Query<SysUser>(@"
select id, user_name as UserName, real_name as RealName,
password, remark,
create_userid as CreateUserid, create_time as CreateTime,
update_userid as UpdateUserid, update_time as UpdateTime
from sys_user
where id < @id", dynamicParameters).ToList();
Console.WriteLine("Dapper查询成功, count=" + list.Count);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
});
tasks.Add(task2);
}
Task.WaitAll(tasks.ToArray());
Console.WriteLine("完成");
}
#endregion
}
}
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Dapper" version="2.0.123" targetFramework="net461" />
</packages>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册