db4o的最佳实践来查询从数据库对象 [英] db4o best practice to query objects from db

查看:112
本文介绍了db4o的最佳实践来查询从数据库对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用两种不同的方式来查询db4o的对象,我想这件事进行讨论。

I am using two different ways to query objects in db4o and I would like to discuss about it.

1)在第一个例子,我创建的一个实例ObjectContainer的,我打开连接,然后我将其关闭。

1) In this first example, I create an instance of ObjectContainer, I open the connection, and then I close it.

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



2)在第二个例子中,我创建一个ObjectServer中,让连接打开的整个生命周期应用程序。我也是从ObjectServer中打开的ObjectContainer,让我的查询,然后关闭它:

2) In this second example, I create an ObjectServer and let the connection open for the whole lifecycle of the application. I also open ObjectContainer from the ObjectServer, make my query and then close it:

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



-

--

主要有哪些这两种方法的区别?它是危险的,如果我从来没有关闭ObjectServer中?

What are the main difference between both methods? Is it dangerous if I never close the ObjectServer?

在我认为,第二种方法是更好的,因为如果两个不同的实例调用方法中的第一个实施例表明,第二呼叫者将得到一个异常,导致数据库将被锁定,但在第二个例子中我没有这样的问题。由于我没有使用db4o我喜欢问,如果我在正确的道路太多经验。

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不会意识到该对象已经存储(因为你关闭了连接,并从那里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的)是打开数据库是耗时的操作时间,因此, ,如果保持打开/关闭该数据库中肯定有性能问题(由另一方面,打开客户端连接是不那么昂贵,所以应在所有造成任何问题)。

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的最佳实践来查询从数据库对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆