ADO事务处理方式运行正常
/// <summary>
/// 执行数据库事务
/// </summary>
/// <param name="dbConnStr">数据库连接串,有多少个连接串就创建多少个事务</param>
/// <param name="routineList">事务代理数组,注意,可能会在一个事务中完成所有工作</param>
public static void DoTransaction(string[] dbConnStr,ITransactionRoutine[] routineList)
{
object[] conn = new object[dbConnStr.Length];
object[] trans = new object[dbConnStr.Length];
try
{
for(int i = 0; i < dbConnStr.Length;i++)
{
SqlConnection connItem = new SqlConnection(dbConnStr[i]);
conn[i] = connItem;
connItem.Open();
trans[i] = connItem.BeginTransaction();
}
for(int i = 0;i < routineList.Length;i++)
{
ITransactionRoutine routine = routineList[i];
routine.Execute((SqlTransaction) trans[routine.TransIndex]);
}
foreach(object obj in trans)
((SqlTransaction)obj).Commit(); //提交所有事务
}
catch(Exception ex)
{
foreach(object obj in trans)
{
SqlTransaction transItem = (SqlTransaction)obj;
try
{
transItem.Rollback(); //回滚所有事务
}
catch (SqlException e)
{
if (transItem.Connection != null)
{
Console.WriteLine("An exception of type " + e.GetType() +
" was encountered while attempting to roll back the transaction.");
}
throw e;
}
}
throw ex; //最后不要忘记抛出来
}
finally
{
for(int i = 0;i < conn.Length;i++) //释放所有数据库连接
{
SqlConnection connItem = (SqlConnection)conn[i];
if(connItem != null)
connItem.Close();
connItem = null;
}
}
}
/// 执行数据库事务
/// </summary>
/// <param name="dbConnStr">数据库连接串,有多少个连接串就创建多少个事务</param>
/// <param name="routineList">事务代理数组,注意,可能会在一个事务中完成所有工作</param>
public static void DoTransaction(string[] dbConnStr,ITransactionRoutine[] routineList)
{
object[] conn = new object[dbConnStr.Length];
object[] trans = new object[dbConnStr.Length];
try
{
for(int i = 0; i < dbConnStr.Length;i++)
{
SqlConnection connItem = new SqlConnection(dbConnStr[i]);
conn[i] = connItem;
connItem.Open();
trans[i] = connItem.BeginTransaction();
}
for(int i = 0;i < routineList.Length;i++)
{
ITransactionRoutine routine = routineList[i];
routine.Execute((SqlTransaction) trans[routine.TransIndex]);
}
foreach(object obj in trans)
((SqlTransaction)obj).Commit(); //提交所有事务
}
catch(Exception ex)
{
foreach(object obj in trans)
{
SqlTransaction transItem = (SqlTransaction)obj;
try
{
transItem.Rollback(); //回滚所有事务
}
catch (SqlException e)
{
if (transItem.Connection != null)
{
Console.WriteLine("An exception of type " + e.GetType() +
" was encountered while attempting to roll back the transaction.");
}
throw e;
}
}
throw ex; //最后不要忘记抛出来
}
finally
{
for(int i = 0;i < conn.Length;i++) //释放所有数据库连接
{
SqlConnection connItem = (SqlConnection)conn[i];
if(connItem != null)
connItem.Close();
connItem = null;
}
}
}
/// <summary>
/// 数据库事务操作接口
/// </summary>
public interface ITransactionRoutine
{
/// <summary>
/// 所属的事务编号
/// </summary>
int TransIndex
{
get;
}
/// <summary>
/// 执行 有异常就会弹出
/// </summary>
/// <param name="trans"></param>
void Execute(SqlTransaction trans);
}
/// 数据库事务操作接口
/// </summary>
public interface ITransactionRoutine
{
/// <summary>
/// 所属的事务编号
/// </summary>
int TransIndex
{
get;
}
/// <summary>
/// 执行 有异常就会弹出
/// </summary>
/// <param name="trans"></param>
void Execute(SqlTransaction trans);
}
调用方式如下:
//找到初始化站点的配置数据
IConfigurationElement[] pluginsConfigElems = null;
IConfigurationElement[] catalogsConfigElems = null;
IConfigurationElement[] rolesConfigElems = null;
IConfigurationElement[] usersConfigElems = null;
SetupConfigElems(ref pluginsConfigElems,
ref catalogsConfigElems,
ref rolesConfigElems,
ref usersConfigElems);
//第一步 创建站点数据
DBTrans.ITransactionRoutine routineSaveSiteInDB = new RoutineSaveSiteInDB(m_siteMgr, m_site);
//第二步 创建默认的分配模块
DBTrans.ITransactionRoutine routineAssignPlugins =
new RoutineAssignPlugins(m_site, pluginsConfigElems);
//第三步 创建固定的应用目录
DBTrans.ITransactionRoutine routineCreateCatalogs =
new RoutineCreateCatalogs(m_site, catalogsConfigElems);
//第四步 创建固定的角色,创建固定的用户,并分配到角色中,并且给角色分配目录权限
DBTrans.ITransactionRoutine routineCreateRoles =
new RoutineCreateRoles(m_site, rolesConfigElems, usersConfigElems);
DBTrans.ITransactionRoutine[] routineList = {
routineSaveSiteInDB,
routineAssignPlugins,
routineCreateCatalogs,
routineCreateRoles
};
try
{
DBTrans.DoTransaction(new string[] {dbConn},routineList);
}
catch (ApplicationException ex)
{
result.Success = false;
result.KernelMessage = ex.InnerException.Message;
}
return result;
IConfigurationElement[] pluginsConfigElems = null;
IConfigurationElement[] catalogsConfigElems = null;
IConfigurationElement[] rolesConfigElems = null;
IConfigurationElement[] usersConfigElems = null;
SetupConfigElems(ref pluginsConfigElems,
ref catalogsConfigElems,
ref rolesConfigElems,
ref usersConfigElems);
//第一步 创建站点数据
DBTrans.ITransactionRoutine routineSaveSiteInDB = new RoutineSaveSiteInDB(m_siteMgr, m_site);
//第二步 创建默认的分配模块
DBTrans.ITransactionRoutine routineAssignPlugins =
new RoutineAssignPlugins(m_site, pluginsConfigElems);
//第三步 创建固定的应用目录
DBTrans.ITransactionRoutine routineCreateCatalogs =
new RoutineCreateCatalogs(m_site, catalogsConfigElems);
//第四步 创建固定的角色,创建固定的用户,并分配到角色中,并且给角色分配目录权限
DBTrans.ITransactionRoutine routineCreateRoles =
new RoutineCreateRoles(m_site, rolesConfigElems, usersConfigElems);
DBTrans.ITransactionRoutine[] routineList = {
routineSaveSiteInDB,
routineAssignPlugins,
routineCreateCatalogs,
routineCreateRoles
};
try
{
DBTrans.DoTransaction(new string[] {dbConn},routineList);
}
catch (ApplicationException ex)
{
result.Success = false;
result.KernelMessage = ex.InnerException.Message;
}
return result;