日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

db4o Tutorial 中文翻译(十一)

發(fā)布時間:2025/7/25 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 db4o Tutorial 中文翻译(十一) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

9. 客戶端/服務器


Now that we have seen how transactions work in db4o conceptually, we?are prepared to tackle concurrently executing transactions.
現(xiàn)在我們已經(jīng)明白db4o中事務的概念了,本章講處理并行執(zhí)行的事務。

現(xià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. 嵌入式服務器


  • 從API角度看,分布式事務和單一VM上的事務沒有什么本質(zhì)區(qū)別。在單機上使用事務,我們需要打開db4o服務器,指示它從0端口運行,從而其他的網(wǎng)絡程序不可以占用。

//?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();
}

  • 再次委托打開和關閉數(shù)據(jù)庫的服務器作為運行環(huán)境,從而捕捉客戶端交互。
//?queryLocalServer
IObjectContainer?client?=?server.OpenClient();
ListResult(client.Get(
new?Car(null)));
client.Close();


  • 這個事務的級別在db4o里面被成為“read committed”。但是,每個客戶端維護它自己的已知對象緩存的引用。為了讓其他客戶端的變化快速執(zhí)行,我們需要在服務器端直接引用已知的對象。我們將這個任務代理為一個專門的ListResult()方法的版本。
public?static?void?ListRefreshedResult(IObjectContainer?container,?IObjectSet?items,?int?depth)
{
????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();

會滾也可以這樣工作,就像你預料的一樣://?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)絡


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沒有多少步驟了。我們需要專門指定一個大于0的端口號碼,為客戶端設置一個或者更多的帳戶。//?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();
}



.
這個客戶端的連接需要提供一個主機、端口、用戶名和密碼。

//?queryRemoteServer
IObjectContainer?client?=?Db4oFactory.OpenClient("localhost",?port,?user,?password);
ListResult(client.Get(
new?Car(null)));
client.Close();


  • 剛才的所有數(shù)據(jù)都通過上面的例子檢索出來了
//?demonstrateRemoteReadCommitted
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. 消息傳輸


  • 有時候客戶端需要請求服務器做一些事情,就要發(fā)送一個特殊的消息給服務器。服務器需要接受消息、發(fā)送消息到涉及的對象。
  • 可以通過SETMESSAGERECIPIENT()方法,找到發(fā)送消息的對象。

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();
????}

}

消息已經(jīng)接收到了,并且被PROCESSMESSAGE() 方法傳遞。




db4o允許一個客戶端以無格式的類的對象的形式發(fā)送一個任意的消息或者信號給服務器。服務器收到后會發(fā)回一個收到消息,包含從客戶發(fā)來的對象。服務器可以任意定義消息的格式。

9.4. 整合--一個簡單但是完整的db4oserver


  • 讓我們將以上的信息整合,從而寫出一個帶有專門客戶端的簡單服務器,客戶端可以通知服務器關閉。

  • 首先,服務器和客戶端都需要一些共享的設置信息。我們提供一個接口:
namespace?Db4objects.Db4o.Tutorial.F1.Chapter5
{
????
/**////?<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);
????????????}

????????}

????}

}




最后但并不是最不重要的(一點),客戶端停止服務器。
锘縰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. 總結



db4o已經(jīng)是一個家族了。不,當然他不是。有太多db4o沒有覆蓋的內(nèi)容:schema 進展,為你的對象定制持久層,書寫你自己的查詢對象。。等等。更多的文檔需要提供,你也可下載獲得。
我們希望這個教程已經(jīng)幫助你開始使用db4o,你該怎么繼續(xù)呢?

你應該瀏覽剩下的章節(jié)。他們是從我們的網(wǎng)站http://forums.db4o.com/forums/.選擇的話題。



(只是交互版本),實際上,這個教程只是基本的步驟而已,試著在章節(jié)間來回,運行代碼片段,你便可以完全掌握db4o。有時候,你有了困難,甚至發(fā)生異常,但是隨時可以在命令窗口重置數(shù)據(jù)庫。


這些例子都有源代碼。所以你可以自由的實驗。

如果遇到困難,看看FAQ有沒有作用。?在我們的web site瀏覽信息,?看看你的問題是否被提交到 Jira?,或者訪問我們的論壇:http://forums.db4o.com/forums/.

轉(zhuǎn)載于:https://www.cnblogs.com/xxpyeippx/archive/2007/05/06/737302.html

總結

以上是生活随笔為你收集整理的db4o Tutorial 中文翻译(十一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。