13,347
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// Represents the NHibernate version of the transaction context, which is
/// the default transaction context implementation used by Apworks Server Application.
/// </summary>
public sealed class DbTransactionContext : TransactionContextBase
{
#region Private Fields
/// <summary>
/// The <see cref="NHibernate.ITransaction"/> object that handles transactions.
/// </summary>
private ITransaction transaction;
#endregion
#region Constructors
/// <summary>
/// Constructs the transaction context by using the default isolation level (Unspecified level).
/// </summary>
public DbTransactionContext() : base() { }
/// <summary>
/// Constructs the transaction context by using the given isolation level.
/// </summary>
/// <param name="isolationLevel">The isolation level which is used for initializing the
/// transaction context.</param>
public DbTransactionContext(IsolationLevel isolationLevel) : base(isolationLevel) { }
#endregion
public ISession Session { get { return DatabaseSessionFactory.Instance.Session; } }
#region ITransactionContext Members
/// <summary>
/// Begins a transaction.
/// </summary>
public override void BeginTransaction()
{
transaction = DatabaseSessionFactory.Instance.Session.BeginTransaction(this.isolationLevel);
}
/// <summary>
/// Commits the transaction.
/// </summary>
public override void Commit()
{
transaction.Commit();
}
/// <summary>
/// Rolls back the transaction.
/// </summary>
public override void Rollback()
{
transaction.Rollback();
}
#endregion
#region IDisposable Members
/// <summary>
/// Disposes the transaction context.
/// </summary>
public override void Dispose()
{
// Disposes the transaction object.
transaction.Dispose();
// If the session used by the transaction context exists and is still
// open, close the session. This ensures that when next time the client
// access the database, new session will be opened.
ISession databaseSession = DatabaseSessionFactory.Instance.Session;
if (databaseSession != null && databaseSession.IsOpen)
{
//databaseSession.Flush();
databaseSession.Close();
}
}
#endregion
// 此处将获得一个DbTransactionContext的实例
using (ITransactionContext transContext = TransactionContextManager.GetContext())
{
try
{
transContext.BeginTransaction();
// ... 你的逻辑
transContext.Commit()
}
catch
{
transContext.Rollback();
throw;
}
}
/// <summary>
/// 保存明细和单据
/// </summary>
/// <param name="dt">要保存的DataTable</param>
/// <param name="hash">包含insert,update,delete的Hashtable</param>
/// <returns>功能T,失败F</returns>
public bool UpdateDetailAndBill(DataTable dt, Hashtable hash)
{
if (trans == null)
{
trans = conn.BeginTransaction();
_billCommand.Transaction = trans;
}
SqlCommand _deleteCommand = new SqlCommand(hash["delete"].ToString(), conn, trans);
_deleteCommand.CommandType = CommandType.StoredProcedure;
SqlCommand insertCommand = new SqlCommand(hash["insert"].ToString(), conn, trans);
insertCommand.CommandType = CommandType.StoredProcedure;
DataMapping dmaping = new DataMapping(conn);
_detailCommand = new SqlCommand(hash["update"].ToString(), conn, trans);
_detailCommand.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter();
_detailCommand = dmaping.getStroeProcedureParamsByName(_detailCommand);
_deleteCommand = dmaping.getStroeProcedureParamsByName(_deleteCommand);
insertCommand = dmaping.getStroeProcedureParamsByName(insertCommand);
da.InsertCommand = insertCommand;
da.UpdateCommand = _detailCommand;
da.DeleteCommand = _deleteCommand;
da.InsertCommand.UpdatedRowSource = UpdateRowSource.OutputParameters;
da.UpdateCommand.UpdatedRowSource = UpdateRowSource.OutputParameters;
da.DeleteCommand.UpdatedRowSource = UpdateRowSource.OutputParameters;
da.UpdateBatchSize = 1;
try
{
//_deleteCommand.ExecuteNonQuery();
int j = _billCommand.ExecuteNonQuery();
int i = da.Update(dt);
dt.AcceptChanges();
trans.Commit();();//如果成功,提交事务
return true;
}
catch (Exception ex)
{
trans.Rollback();//如果失败,回滚事务
throw ex;
}
finally
{
conn.Close();
}
}