db4o Tutorial 中文翻译(十一)
9. 客户端/服务器
Now that we have seen how transactions work in db4o conceptually, we are prepared to tackle concurrently executing transactions.
现在我们已经明白db4o中事务的概念了,本章讲处理并行执行的事务。
现在按照以往熟悉的方法准备数据。
// setFirstCar
Pilot pilot = new Pilot("Rubens Barrichello", 99);
Car car = new Car("BMW");
car.Pilot = pilot;
db.Set(car);
// setSecondCar
Pilot pilot = new Pilot("Michael Schumacher", 100);
Car car = new Car("Ferrari");
car.Pilot = pilot;
db.Set(car);9.1. 嵌入式服务器
- 从API角度看,分布式事务和单一VM上的事务没有什么本质区别。在单机上使用事务,我们需要打开db4o服务器,指示它从0端口运行,从而其他的网络程序不可以占用。
// accessLocalServer
IObjectServer server = Db4oFactory.OpenServer(Util.YapFileName, 0);
try

{
IObjectContainer client = server.OpenClient();
// Do something with this client, or open more clients
client.Close();
}
finally

{
server.Close();
}- 再次委托打开和关闭数据库的服务器作为运行环境,从而捕捉客户端交互。
// queryLocalServer
IObjectContainer client = server.OpenClient();
ListResult(client.Get(new Car(null)));
client.Close();- 这个事务的级别在db4o里面被成为“read committed”。但是,每个客户端维护它自己的已知对象缓存的引用。为了让其他客户端的变化快速执行,我们需要在服务器端直接引用已知的对象。我们将这个任务代理为一个专门的ListResult()方法的版本。
