db4o Tutorial 中文翻译(十)

8. 事务


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


8.1. 执行和会滚

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

None.gif //  listAllCars
None.gif
IObjectSet result  =  db.Get( typeof (Car));
None.gifListResult(result);


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

None.gif //  listAllCars
None.gif
IObjectSet result  =  db.Get( typeof (Car));
None.gifListResult(result);

8.2. 刷新活动的对象


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

当你意识到要会滚的时候,可以手动更新。
None.gif //  carSnapshotRollbackRefresh
None.gif
IObjectSet result = db.Get( new  Car( " BMW " ));
None.gifCar car
= (Car)result.Next();
None.gifcar.Snapshot();
None.gifdb.Set(car);
None.gifdb.Rollback();
None.gifdb.Ext().Refresh(car, 
int .MaxValue);
None.gifConsole.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。

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

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.完整代码

 

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

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                db.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

InBlock.gif    
InBlock.gif        
public static void ListAllCars(IObjectContainer db)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IObjectSet result 
= db.Get(typeof(Car));
InBlock.gif            ListResult(result);
ExpandedSubBlockEnd.gif        }

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

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

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

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


转载于:https://www.cnblogs.com/xxpyeippx/archive/2007/05/06/737261.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值