简体   繁体   English

db4o从db查询对象的最佳实践

[英]db4o best practice to query objects from db

I am using two different ways to query objects in db4o and I would like to discuss about it. 我使用两种不同的方式来查询db4o中的对象,我想讨论它。

1) In this first example, I create an instance of ObjectContainer, I open the connection, and then I close it. 1)在第一个例子中,我创建了一个ObjectContainer实例,我打开连接,然后关闭它。

ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), "User");
ObjectSet result = db.queryByExample(user);
db.close();

2) In this second example, I create an ObjectServer and let the connection open for the whole lifecycle of the application. 2)在第二个例子中,我创建了一个ObjectServer,让连接在应用程序的整个生命周期内打开。 I also open ObjectContainer from the ObjectServer, make my query and then close it: 我还从ObjectServer打开ObjectContainer,进行查询然后关闭它:

ObjectServer userDb = Db4oClientServer.openServer(Db4oClientServer.newServerConfiguration(), "User", 0);
ObjectContainer client = client = userDb.openClient();
ObjectSet result = client.queryByExample(user);
client.close();

-- -

What are the main difference between both methods? 两种方法有什么主要区别? Is it dangerous if I never close the ObjectServer? 如果我从不关闭ObjectServer,这是危险的吗?

In my opinion, the second method is better, because if two different instances call the method showed in the first example, the second caller will get an exception, cause the database would be locked, but in the second example I do not have such a problem. 在我看来,第二种方法更好,因为如果两个不同的实例调用第一个例子中显示的方法,第二个调用者将获得异常,导致数据库被锁定,但在第二个例子中我没有这样的问题。 As I do not have much experience with db4o I prefer to ask if I am on the right way. 由于我对db4o没有太多经验,我更愿意问我是否正确。

db4o works best when you keep the connection open for the whole application life cycle. 在整个应用程序生命周期中保持连接打开时,db4o的效果最佳。

If you retrieve an object, close the database, reopen it and store the object again db4o will not realize that the object is already stored (since you closed the connection and whence db4o's reference system also) and will store a second instance. 如果你检索一个对象,关闭数据库,重新打开它并再次存储该对象db4o将不会意识到该对象已经存储(因为你关闭了连接以及db4o的引用系统)并且将存储第二个实例。

Another issue (if you run db4o in embedded mode) is that opening the database is a time consuming operation, so, if you keep opening/close the database the certainly will have performance issues (by the other hand, opening client connections is not so costly and so should pose no problem at all). 另一个问题(如果你在嵌入式模式下运行db4o)是打开数据库是一个耗时的操作,所以,如果你继续打开/关闭数据库肯定会有性能问题(另一方面,打开客户端连接不是这样的昂贵的,所以应该没有任何问题)。

In the first code sample you're opening the db4o database using embedded mode, basically you get a local object container and you work on one transaction until you close the db. 在第一个代码示例中,您使用嵌入模式打开db4o数据库,基本上您获得了一个本地对象容器,并且您在关闭数据库之前处理一个事务。

In the second sample you're instantiating an object server. 在第二个示例中,您将实例化对象服务器。 There are two modes when working with object servers: local mode (when you choose port number 0, that's what you did) and remote mode (choose a host and a port higher than 0). 使用对象服务器时有两种模式:本地模式(当您选择端口号0时,这就是您所做的)和远程模式(选择主机和高于0的端口)。 The former involves no networking while the later does (and can work remotely (aka C/S)). 前者不涉及网络而后者(并且可以远程工作(又称C / S))。

Anyway, the advantage of working with an object server is that you get multiple transactions via opening client object containers (openClient()), when you open a new client you get a new object container with it's own reference system and independent commit/rollback (like a new transaction). 无论如何,使用对象服务器的优点是通过打开客户端对象容器(openClient())获得多个事务,当您打开一个新客户端时,您将获得一个具有自己的引用系统和独立提交/回滚的新对象容器(像一个新的交易)。

So you usually will go with the second sample if you'll be working with multiple clients operating on the same object server and you need transaction separation. 因此,如果您将使用在同一对象服务器上运行的多个客户端并且需要事务分离,那么通常会使用第二个示例。

More info: http://developer.db4o.com/Documentation/Reference/db4o-8.0/java/reference/index_CSH.html#client-server.htm 更多信息: http//developer.db4o.com/Documentation/Reference/db4o-8.0/java/reference/index_CSH.html#client-server.htm

Best! 最好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM