Skip to content

Commit

Permalink
TransactionDbContext bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
279328316 committed Apr 9, 2021
1 parent 04f9e13 commit c4049a1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 57 deletions.
2 changes: 2 additions & 0 deletions Jc.Core/DbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public partial class DbContext
{
internal DbProvider dbProvider; //DbProvider

internal bool isTransaction = false; //是否为事务

/// <summary>
/// Ctor
/// <param name="connectString">数据库连接串或数据库名称</param>
Expand Down
102 changes: 49 additions & 53 deletions Jc.Core/DbContextSetExpand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,11 @@ public partial class DbContext
}
//因为参数有2100的限制
int perOpAmount = 2000/ piMapList.Count; //每次添加Amount
bool isTransactionDbContext = this is TransactionDbContext;
if (useTransaction && !isTransactionDbContext)

if (useTransaction && !isTransaction)
{
#region Use Transaction
DbConnection dbConnection = GetDbConnection();
DbConnection dbConnection = GetDbConnection();
DbTransaction transaction = dbConnection.BeginTransaction();
int i = 0;
T curItem;
Expand Down Expand Up @@ -322,53 +322,52 @@ public partial class DbContext
transaction.Rollback();
CloseDbConnection(dbConnection);
throw ex;
}
}
#endregion
}
else
{
#region Not Use Transaction
DbConnection dbConnection = GetDbConnection();
int i = 0;
T curItem;
try
{
List<T> curOpList = new List<T>();
for (i = 0; i < list.Count; i++)
{
#region 设置Id
curItem = list[i];
object pkValue = dtoDbMapping.PkMap.Pi.GetValue(list[i]);
if (dtoDbMapping.PkMap.PropertyType == typeof(Guid) || dtoDbMapping.PkMap.PropertyType == typeof(Guid?))
{ //Guid Id
if (pkValue == null || (Guid)pkValue == Guid.Empty)
{ //生成Guid
dtoDbMapping.PkMap.Pi.SetValue(list[i], Guid.NewGuid());
}
List<T> curOpList = new List<T>();
for (i = 0; i < list.Count; i++)
{
#region 设置Id
curItem = list[i];
object pkValue = dtoDbMapping.PkMap.Pi.GetValue(list[i]);
if (dtoDbMapping.PkMap.PropertyType == typeof(Guid) || dtoDbMapping.PkMap.PropertyType == typeof(Guid?))
{ //Guid Id
if (pkValue == null || (Guid)pkValue == Guid.Empty)
{ //生成Guid
dtoDbMapping.PkMap.Pi.SetValue(list[i], Guid.NewGuid());
}
#endregion
}
#endregion

curOpList.Add(list[i]);
if ((i + 1) % perOpAmount == 0 || i == list.Count - 1)
curOpList.Add(list[i]);
if ((i + 1) % perOpAmount == 0 || i == list.Count - 1)
{
using (DbCommand dbCommand = dbProvider.GetInsertDbCmd(curOpList, piMapList, this.GetSubTableArg<T>()))
{
using (DbCommand dbCommand = dbProvider.GetInsertDbCmd(curOpList, piMapList, this.GetSubTableArg<T>()))
try
{
dbCommand.Connection = dbConnection;
SetDbConnection(dbCommand);
rowCount += dbCommand.ExecuteNonQuery();
CloseDbConnection(dbCommand);
}
curOpList.Clear();
if (progress != null)
{
progress.Report((i + 1) * 1.0 / list.Count);
catch (Exception ex)
{
CloseDbConnection(dbCommand);
throw ex;
}
}
curOpList.Clear();
if (progress != null)
{
progress.Report((i + 1) * 1.0 / list.Count);
}
}
CloseDbConnection(dbConnection);
}
catch (Exception ex)
{
CloseDbConnection(dbConnection);
throw ex;
}
#endregion
}
Expand Down Expand Up @@ -453,8 +452,7 @@ public partial class DbContext
//因为参数有2100的限制 待更新字段 + 主键
int perOpAmount = 2000 / (piMapList.Count + 1); //每次更新Amount

bool isTransactionDbContext = this is TransactionDbContext;
if (useTransaction && !isTransactionDbContext)
if (useTransaction && !isTransaction)
{
#region Use Transaction
DbConnection dbConnection = GetDbConnection();
Expand Down Expand Up @@ -494,34 +492,32 @@ public partial class DbContext
else
{
#region Not Use Transaction
DbConnection dbConnection = GetDbConnection();
try
List<T> curOpList = new List<T>();
for (int i = 0; i < list.Count; i++)
{
List<T> curOpList = new List<T>();
for (int i = 0; i < list.Count; i++)
curOpList.Add(list[i]);
if ((i + 1) % perOpAmount == 0 || i == list.Count - 1)
{
curOpList.Add(list[i]);
if ((i + 1) % perOpAmount == 0 || i == list.Count - 1)
using (DbCommand dbCommand = dbProvider.GetUpdateDbCmd(curOpList, piMapList, this.GetSubTableArg<T>()))
{
using (DbCommand dbCommand = dbProvider.GetUpdateDbCmd(curOpList, piMapList, this.GetSubTableArg<T>()))
try
{
dbCommand.Connection = dbConnection;
SetDbConnection(dbCommand);
rowCount += dbCommand.ExecuteNonQuery();
}
curOpList.Clear();
if (progress != null)
catch (Exception ex)
{
progress.Report((i + 1) * 1.0 / list.Count);
CloseDbConnection(dbCommand);
throw ex;
}
}
curOpList.Clear();
if (progress != null)
{
progress.Report((i + 1) * 1.0 / list.Count);
}
}
CloseDbConnection(dbConnection);
}
catch (Exception ex)
{
CloseDbConnection(dbConnection);
throw ex;
}
#endregion
}
return rowCount;
Expand Down
11 changes: 7 additions & 4 deletions Jc.Core/TransactionDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ namespace Jc.Core
/// </summary>
public sealed class TransactionDbContext : SubTableDbContext
{
internal bool isTransaction = false; //是否为事务

internal DbConnection transDbConnection; //dbConnection

private DbTransaction dbTransaction;//事务使用
Expand Down Expand Up @@ -70,11 +68,16 @@ internal override void CloseDbConnection(DbConnection connection)
/// <returns></returns>
internal override void SetDbConnection(DbCommand dbCommand)
{
base.SetDbConnection(dbCommand);
if(isTransaction)
if (isTransaction)
{
dbCommand.Connection = transDbConnection;
dbCommand.Transaction = dbTransaction;
}
else
{
//base.SetDbConnection(dbCommand);
throw new Exception("事务连接已关闭");
}
}

/// <summary>
Expand Down

0 comments on commit c4049a1

Please sign in to comment.