db4o Tutorial 中文翻译(十一)
生活随笔
收集整理的這篇文章主要介紹了
db4o Tutorial 中文翻译(十一)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
9. 客戶端/服務(wù)器
Now that we have seen how transactions work in db4o conceptually, we?are prepared to tackle concurrently executing transactions.
現(xiàn)在我們已經(jīng)明白db4o中事務(wù)的概念了,本章講處理并行執(zhí)行的事務(wù)。
現(xiàn)在按照以往熟悉的方法準(zhǔn)備數(shù)據(jù)。
//?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. 嵌入式服務(wù)器
- 從API角度看,分布式事務(wù)和單一VM上的事務(wù)沒有什么本質(zhì)區(qū)別。在單機(jī)上使用事務(wù),我們需要打開db4o服務(wù)器,指示它從0端口運(yùn)行,從而其他的網(wǎng)絡(luò)程序不可以占用。
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();
}
- 再次委托打開和關(guān)閉數(shù)據(jù)庫的服務(wù)器作為運(yùn)行環(huán)境,從而捕捉客戶端交互。
IObjectContainer?client?=?server.OpenClient();
ListResult(client.Get(new?Car(null)));
client.Close();
- 這個(gè)事務(wù)的級別在db4o里面被成為“read committed”。但是,每個(gè)客戶端維護(hù)它自己的已知對象緩存的引用。為了讓其他客戶端的變化快速執(zhí)行,我們需要在服務(wù)器端直接引用已知的對象。我們將這個(gè)任務(wù)代理為一個(gè)專門的ListResult()方法的版本。
{
????Console.WriteLine(items.Count);
????foreach?(object?item?in?items)
????{????
????????container.Ext().Refresh(item,?depth);
????????Console.WriteLine(item);
????}
}
//?demonstrateLocalReadCommitted
IObjectContainer?client1?=server.OpenClient();
IObjectContainer?client2?=server.OpenClient();
Pilot?pilot?=?new?Pilot("David?Coulthard",?98);
IObjectSet?result?=?client1.Get(new?Car("BMW"));
Car?car?=?(Car)result.Next();
car.Pilot?=?pilot;
client1.Set(car);
ListResult(client1.Get(new?Car(null)));
ListResult(client2.Get(new?Car(null)));
client1.Commit();
ListResult(client1.Get(typeof(Car)));????????????
ListRefreshedResult(client2,?client2.Get(typeof(Car)),?2);
client1.Close();
client2.Close();
會(huì)滾也可以這樣工作,就像你預(yù)料的一樣://?demonstrateLocalRollback
IObjectContainer?client1?=?server.OpenClient();
IObjectContainer?client2?=?server.OpenClient();
IObjectSet?result?=?client1.Get(new?Car("BMW"));
Car?car?=?(Car)result.Next();
car.Pilot?=?new?Pilot("Someone?else",?0);
client1.Set(car);
ListResult(client1.Get(new?Car(null)));
ListResult(client2.Get(new?Car(null)));
client1.Rollback();
client1.Ext().Refresh(car,?2);
ListResult(client1.Get(new?Car(null)));
ListResult(client2.Get(new?Car(null)));
client1.Close();
client2.Close();
9.2. 網(wǎng)絡(luò)
From here it's only a small step towards operating db4o over a TCP/IP network.?We just specify a port number greater than zero?and set up one or more accounts for our client(s).
到了這里,距離通過TCP/IP協(xié)議操作db4o沒有多少步驟了。我們需要專門指定一個(gè)大于0的端口號碼,為客戶端設(shè)置一個(gè)或者更多的帳戶。//?accessRemoteServer
IObjectServer?server?=?Db4oFactory.OpenServer(Util.YapFileName,?ServerPort);
server.GrantAccess(ServerUser,?ServerPassword);
try
{
????IObjectContainer?client?=?Db4oFactory.OpenClient("localhost",?ServerPort,?ServerUser,?ServerPassword);
????//?Do?something?with?this?client,?or?open?more?clients
????client.Close();
}
finally
{
????server.Close();
}
.
這個(gè)客戶端的連接需要提供一個(gè)主機(jī)、端口、用戶名和密碼。
//?queryRemoteServer
IObjectContainer?client?=?Db4oFactory.OpenClient("localhost",?port,?user,?password);
ListResult(client.Get(new?Car(null)));
client.Close();
- 剛才的所有數(shù)據(jù)都通過上面的例子檢索出來了
IObjectContainer?client1?=?Db4oFactory.OpenClient("localhost",?port,?user,?password);
IObjectContainer?client2?=?Db4oFactory.OpenClient("localhost",?port,?user,?password);
Pilot?pilot?=?new?Pilot("Jenson?Button",?97);
IObjectSet?result?=?client1.Get(new?Car(null));
Car?car?=?(Car)result.Next();
car.Pilot?=?pilot;
client1.Set(car);
ListResult(client1.Get(new?Car(null)));
ListResult(client2.Get(new?Car(null)));
client1.Commit();
ListResult(client1.Get(new?Car(null)));
ListResult(client2.Get(new?Car(null)));
client1.Close();
client2.Close();
//?demonstrateRemoteRollback
IObjectContainer?client1?=?Db4oFactory.OpenClient("localhost",?port,?user,?password);
IObjectContainer?client2?=?Db4oFactory.OpenClient("localhost",?port,?user,?password);
IObjectSet?result?=?client1.Get(new?Car(null));
Car?car?=?(Car)result.Next();
car.Pilot?=?new?Pilot("Someone?else",?0);
client1.Set(car);
ListResult(client1.Get(new?Car(null)));
ListResult(client2.Get(new?Car(null)));
client1.Rollback();
client1.Ext().Refresh(car,2);
ListResult(client1.Get(new?Car(null)));
ListResult(client2.Get(new?Car(null)));
client1.Close();
client2.Close();
9.3. 消息傳輸
- 有時(shí)候客戶端需要請求服務(wù)器做一些事情,就要發(fā)送一個(gè)特殊的消息給服務(wù)器。服務(wù)器需要接受消息、發(fā)送消息到涉及的對象。
- 可以通過SETMESSAGERECIPIENT()方法,找到發(fā)送消息的對象。
{
????lock(this)
????{
????????IObjectServer?db4oServer?=?Db4oFactory.OpenServer(FILE,?PORT);
????????db4oServer.GrantAccess(USER,?PASS);
????????
????????//?Using?the?messaging?functionality?to?redirect?all
????????//?messages?to?this.processMessage
????????db4oServer.Ext().Configure().ClientServer().SetMessageRecipient(this);
????????try
????????{
????????????if?(!?stop)
????????????{
????????????????//?wait?forever?for?Notify()?from?Close()
????????????????Monitor.Wait(this);
????????????}
????????}
????????catch?(Exception?e)
????????{
????????????Console.WriteLine(e.ToString());
????????}
????????db4oServer.Close();
????}
}
消息已經(jīng)接收到了,并且被PROCESSMESSAGE() 方法傳遞。
db4o允許一個(gè)客戶端以無格式的類的對象的形式發(fā)送一個(gè)任意的消息或者信號給服務(wù)器。服務(wù)器收到后會(huì)發(fā)回一個(gè)收到消息,包含從客戶發(fā)來的對象。服務(wù)器可以任意定義消息的格式。
9.4. 整合--一個(gè)簡單但是完整的db4oserver
- 讓我們將以上的信息整合,從而寫出一個(gè)帶有專門客戶端的簡單服務(wù)器,客戶端可以通知服務(wù)器關(guān)閉。
- 首先,服務(wù)器和客戶端都需要一些共享的設(shè)置信息。我們提供一個(gè)接口:
{
????/**////?<summary>
????///?Configuration?used?for?StartServer?and?StopServer.
????///?</summary>
????public?class?ServerConfiguration
????{
????????/**////?<summary>
????????///?the?host?to?be?used.
????????///?If?you?want?to?run?the?client?server?examples?on?two?computers,
????????///?enter?the?computer?name?of?the?one?that?you?want?to?use?as?server.?
????????///?</summary>
????????public?const?string?HOST?=?"localhost";??
????????/**////?<summary>
????????///?the?database?file?to?be?used?by?the?server.
????????///?</summary>
????????public?const?string?FILE?=?"formula1.yap";
????????/**////?<summary>
????????///?the?port?to?be?used?by?the?server.
????????///?</summary>
????????public?const?int?PORT?=?4488;
????????/**////?<summary>
????????///?the?user?name?for?access?control.
????????///?</summary>
????????public?const?string?USER?=?"db4o";
????
????????/**////?<summary>
????????///?the?pasword?for?access?control.
????????///?</summary>
????????public?const?string?PASS?=?"db4o";
????}
}
創(chuàng)建server:锘縰sing?System;
using?System.Threading;
using?Db4objects.Db4o;
using?Db4objects.Db4o.Messaging;
namespace?Db4objects.Db4o.Tutorial.F1.Chapter5
{
????/**////?<summary>
????///?starts?a?db4o?server?with?the?settings?from?ServerConfiguration.
????///?This?is?a?typical?setup?for?a?long?running?server.
????///?The?Server?may?be?stopped?from?a?remote?location?by?running
????///?StopServer.?The?StartServer?instance?is?used?as?a?MessageRecipient
????///?and?reacts?to?receiving?an?instance?of?a?StopServer?object.
????///?Note?that?all?user?classes?need?to?be?present?on?the?server?side
????///?and?that?all?possible?Db4oFactory.Configure()?calls?to?alter?the?db4o
????///?configuration?need?to?be?executed?on?the?client?and?on?the?server.
????///?</summary>
????public?class?StartServer?:?ServerConfiguration,?IMessageRecipient
????{
????????/**////?<summary>
????????///?setting?the?value?to?true?denotes?that?the?server?should?be?closed
????????///?</summary>
????????private?bool?stop?=?false;
????????/**////?<summary>
????????///?starts?a?db4o?server?using?the?configuration?from
????????///?ServerConfiguration.
????????///?</summary>
????????public?static?void?Main(string[]?arguments)
????????{
????????????new?StartServer().RunServer();
????????}
????????/**////?<summary>
????????///?opens?the?IObjectServer,?and?waits?forever?until?Close()?is?called
????????///?or?a?StopServer?message?is?being?received.
????????///?</summary>
????????public?void?RunServer()
????????{
????????????lock(this)
????????????{
????????????????IObjectServer?db4oServer?=?Db4oFactory.OpenServer(FILE,?PORT);
????????????????db4oServer.GrantAccess(USER,?PASS);
????????????????
????????????????//?Using?the?messaging?functionality?to?redirect?all
????????????????//?messages?to?this.processMessage
????????????????db4oServer.Ext().Configure().ClientServer().SetMessageRecipient(this);
????????????????try
????????????????{
????????????????????if?(!?stop)
????????????????????{
????????????????????????//?wait?forever?for?Notify()?from?Close()
????????????????????????Monitor.Wait(this);
????????????????????}
????????????????}
????????????????catch?(Exception?e)
????????????????{
????????????????????Console.WriteLine(e.ToString());
????????????????}
????????????????db4oServer.Close();
????????????}
????????}
????????/**////?<summary>
????????///?messaging?callback
????????///?see?com.db4o.messaging.MessageRecipient#ProcessMessage()
????????///?</summary>
????????public?void?ProcessMessage(IObjectContainer?con,?object?message)
????????{
????????????if?(message?is?StopServer)
????????????{
????????????????Close();
????????????}
????????}
????????/**////?<summary>
????????///?closes?this?server.
????????///?</summary>
????????public?void?Close()
????????{
????????????lock(this)
????????????{
????????????????stop?=?true;
????????????????Monitor.PulseAll(this);
????????????}
????????}
????}
}
最后但并不是最不重要的(一點(diǎn)),客戶端停止服務(wù)器。
锘縰sing?System;
using?Db4objects.Db4o;
using?Db4objects.Db4o.Messaging;
namespace?Db4objects.Db4o.Tutorial.F1.Chapter5
{
????/**////?<summary>
????///?stops?the?db4o?Server?started?with?StartServer.
????///?This?is?done?by?opening?a?client?connection
????///?to?the?server?and?by?sending?a?StopServer?object?as
????///?a?message.?StartServer?will?react?in?it's
????///?processMessage?method.
????///?</summary>
????public?class?StopServer?:?ServerConfiguration
????{
????????/**////?<summary>
????????///?stops?a?db4o?Server?started?with?StartServer.
????????///?</summary>
????????///?<exception?cref="Exception"?/>
????????public?static?void?Main(string[]?args)
????????{
????????????IObjectContainer?IObjectContainer?=?null;
????????????try
????????????{
????????????????//?connect?to?the?server
????????????????IObjectContainer?=?Db4oFactory.OpenClient(HOST,?PORT,?USER,?PASS);
????????????}
????????????catch?(Exception?e)
????????????{
????????????????Console.WriteLine(e.ToString());
????????????}
????????????
????????????if?(IObjectContainer?!=?null)
????????????{
????????????????//?get?the?messageSender?for?the?IObjectContainer
????????????????IMessageSender?messageSender?=?IObjectContainer.Ext()
????????????????????.Configure().ClientServer().GetMessageSender();
????????????????//?send?an?instance?of?a?StopServer?object
????????????????messageSender.Send(new?StopServer());
????????????????
????????????????//?close?the?IObjectContainer
????????????????IObjectContainer.Close();
????????????}
????????}
????}
}
9.5. 總結(jié)
db4o已經(jīng)是一個(gè)家族了。不,當(dāng)然他不是。有太多db4o沒有覆蓋的內(nèi)容:schema 進(jìn)展,為你的對象定制持久層,書寫你自己的查詢對象。。等等。更多的文檔需要提供,你也可下載獲得。
我們希望這個(gè)教程已經(jīng)幫助你開始使用db4o,你該怎么繼續(xù)呢?
你應(yīng)該瀏覽剩下的章節(jié)。他們是從我們的網(wǎng)站http://forums.db4o.com/forums/.選擇的話題。
(只是交互版本),實(shí)際上,這個(gè)教程只是基本的步驟而已,試著在章節(jié)間來回,運(yùn)行代碼片段,你便可以完全掌握db4o。有時(shí)候,你有了困難,甚至發(fā)生異常,但是隨時(shí)可以在命令窗口重置數(shù)據(jù)庫。
這些例子都有源代碼。所以你可以自由的實(shí)驗(yàn)。
如果遇到困難,看看FAQ有沒有作用。?在我們的web site瀏覽信息,?看看你的問題是否被提交到 Jira?,或者訪問我們的論壇:http://forums.db4o.com/forums/.
轉(zhuǎn)載于:https://www.cnblogs.com/xxpyeippx/archive/2007/05/06/737302.html
總結(jié)
以上是生活随笔為你收集整理的db4o Tutorial 中文翻译(十一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 异常:java.util.Concurr
- 下一篇: 与客家土楼的约会(增城-河源)