当前位置: 代码迷 >> .NET Framework >> Castle ActiveRecord 事务到底如何处理啊 多谢
  详细解决方案

Castle ActiveRecord 事务到底如何处理啊 多谢

热度:235   发布时间:2016-05-02 00:55:53.0
Castle ActiveRecord 事务到底怎么处理啊 谢谢
TransactionScope tran = new TransactionScope();  
  try
  {

  UserInfo ui = null;


  ui = new UserInfo();
  ui.ID = "1";
  ui.Name = "11111";
  ui.Create(); //这里OK  


  ui = new UserInfo();
   
  ui.ID = "2";
  ui.Name = "2222";
  ui.Create();//这里由于库中已经存在主键为2的记录 应该引发异常 结果这里根本没有引发异常

   

  tran.VoteCommit();//结果提交了 但是提交后实际上并没有插入成功主键为1的记录
  }
  catch(Exception)
  {
  tran.VoteRollBack();//理论上应该在插入主键2的时候发生异常,进入此代码来回滚事务
  }

------解决方案--------------------
呵呵 帮顶吧
------解决方案--------------------
TransactionScope transaction = new TransactionScope();

try
{
Blog blog = new Blog();
blog.Author = "hammett";
blog.Name = "some name";
blog.Save();

Post post = new Post(blog, "title", "post contents", "Castle");
post.Save();
}
catch(Exception)
{
transaction.VoteRollBack();

throw;
}
finally
{
transaction.Dispose();
}