db4o Tutorial 中文翻译(十)

8. 事务


你有没有这样的疑问:db4o如何在一个单独的数据表中处理并发访问?像其他数据库管理系统一样,db4o具有一种事务的机制。在考虑多线程甚至远程,数据库被并行访问之前,首先要了解db4o的事务概念。


    8.1. 执行和会滚

    也许你没有注意到,我们从第一章就开始用事务了。按照定义,你已经用过了db4o的事务了。当一个数据库链接打开的时候,事务已经隐性开始了;当数据库关闭的时候,事务已经执行了。下面的代码从功能上和我们之前写的一样,只是使用了事务。
    //  storeCarCommit
    Pilot pilot  =   new  Pilot( " Rubens Barrichello " 99 );
    Car car 
    =   new  Car( " BMW " );
    car.Pilot 
    =  pilot;
    db.Set(car);
    db.Commit();

    //  listAllCars
    IObjectSet result  =  db.Get( typeof (Car));
    ListResult(result);


    当然,可以会滚事务,以回到以前的状态:
    //  storeCarRollback
    Pilot pilot  =   new  Pilot( " Michael Schumacher " 100 );
    Car car 
    =   new  Car( " Ferrari " );
    car.Pilot 
    =  pilot;
    db.Set(car);
    db.Rollback();

    //  listAllCars
    IObjectSet result  =  db.Get( typeof (Car));
    ListResult(result);

    8.2. 刷新活动的对象


    还有一个问题:当数据库的数据会滚的时候,内存中的对象已经被修改了。如何刷新内存中的对象呢?
    //  carSnapshotRollback
    IObjectSet result  =  db.Get( new  Car( " BMW " ));
    Car car 
    =  (Car)result.Next();
    car.Snapshot();
    db.Set(car);
    db.Rollback();
    Console.WriteLine(car);

    当你意识到要会滚的时候,可以手动更新。
    //  carSnapshotRollbackRefresh
    IObjectSet result = db.Get( new  Car( " BMW " ));
    Car car
    = (Car)result.Next();
    car.Snapshot();
    db.Set(car);
    db.Rollback();
    db.Ext().Refresh(car, 
    int .MaxValue);
    Console.WriteLine(car);


                      What is this IExtObjectContainer construct good for? Well, it provides some functionality that is in itself stable, but the API may still be subject to change. As soon as we are confident that no more changes will occur, ext functionality will be transferred to the common IObjectContainer API.
                      IExtObjectContainer 有什么好处呢?它提供了一些内部固定的功能,但是API还是变了。当我们确定没有更多的变化时,ext功能将变成普通的IObjectContainer API。

                      最后,我们可以再次清除:
                      //  deleteAll
                      IObjectSet result  =  db.Get( typeof (Object));
                      foreach  ( object  item  in  result)
                      {
                          db.Delete(item);
                      }

                      8.3. 总结


                      We have seen how transactions work for a single client. In the  next chapter  we will see how the transaction concept extends to multiple clients, whether they are located within the same VM or on a remote machine.
                      我们已经明白但客户端如何做事务了。在下一章,我们可以将事务扩展到多客户端,这些客户端可以是在不同的VM或者分布式机器上。


                      8.4.完整代码

                       

                      锘縰sing System;
                      using  System.IO;
                      using  Db4objects.Db4o;
                      namespace  Db4objects.Db4o.Tutorial.F1.Chapter5
                      {
                          
                      public class TransactionExample : Util
                          
                      {
                              
                      public static void Main(string[] args)
                              
                      {
                                  File.Delete(Util.YapFileName);
                                  IObjectContainer db
                      =Db4oFactory.OpenFile(Util.YapFileName);
                                  
                      try
                                  
                      {
                                      StoreCarCommit(db);
                                      db.Close();
                                      db 
                      = Db4oFactory.OpenFile(Util.YapFileName);
                                      ListAllCars(db);
                                      StoreCarRollback(db);
                                      db.Close();
                                      db 
                      = Db4oFactory.OpenFile(Util.YapFileName);
                                      ListAllCars(db);
                                      CarSnapshotRollback(db);
                                      CarSnapshotRollbackRefresh(db);
                                  }

                                  
                      finally
                                  
                      {
                                      db.Close();
                                  }

                              }

                              
                              
                      public static void StoreCarCommit(IObjectContainer db)
                              
                      {
                                  Pilot pilot 
                      = new Pilot("Rubens Barrichello"99);
                                  Car car 
                      = new Car("BMW");
                                  car.Pilot 
                      = pilot;
                                  db.Set(car);
                                  db.Commit();
                              }

                          
                              
                      public static void ListAllCars(IObjectContainer db)
                              
                      {
                                  IObjectSet result 
                      = db.Get(typeof(Car));
                                  ListResult(result);
                              }

                              
                              
                      public static void StoreCarRollback(IObjectContainer db)
                              
                      {
                                  Pilot pilot 
                      = new Pilot("Michael Schumacher"100);
                                  Car car 
                      = new Car("Ferrari");
                                  car.Pilot 
                      = pilot;
                                  db.Set(car);
                                  db.Rollback();
                              }

                          
                              
                      public static void CarSnapshotRollback(IObjectContainer db)
                              
                      {
                                  IObjectSet result 
                      = db.Get(new Car("BMW"));
                                  Car car 
                      = (Car)result.Next();
                                  car.Snapshot();
                                  db.Set(car);
                                  db.Rollback();
                                  Console.WriteLine(car);
                              }

                          
                              
                      public static void CarSnapshotRollbackRefresh(IObjectContainer db)
                              
                      {
                                  IObjectSet result
                      =db.Get(new Car("BMW"));
                                  Car car
                      =(Car)result.Next();
                                  car.Snapshot();
                                  db.Set(car);
                                  db.Rollback();
                                  db.Ext().Refresh(car, 
                      int.MaxValue);
                                  Console.WriteLine(car);
                              }

                          }

                      }



                    你可能感兴趣的:(RIA)