db4o best practice to query objects from db
Asked Answered
H

2

9

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

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

--

What are the main difference between both methods? Is it dangerous if I never close the 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.

Heptad answered 13/5, 2011 at 19:36 Comment(0)
L
11

db4o works best when you keep the connection open for the whole application life cycle.

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.

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).

Loblolly answered 13/5, 2011 at 23:32 Comment(5)
Wow, thanks. I did have the problem that my objects were stored twice, and I did not know why. Your answer makes this issue clear for me now. Thanks a lot Vagaus.Heptad
Hey Vagaus, acabei de ver que vc é brasileiro :) Eu estou terminando meu curso de ciencias da computacao na KIT = Karlsruhe Institut of Technology na alemanha. Eu estava há algum tempo querendo conhecer alguem da área que me desse algumas informacoes de como é o mercado de trabalho na área de informatica no Brasil, será que a gente poderia trocar algumas pequenas ideias nesse sentido? Me manda teu email pro meu email se possivel por favor: [email protected]. Desde já agradeco a colaboracao. Abraco!Heptad
I have a question, how do we check if the db4o ObjectContainer is closed?Vinegarroon
basically, this can't be the best way to do it:Vinegarroon
public ObjectContainer getDb() { if (db == null) { System.out.println("db was null in " + dbci + " connection. Had to create new DB object."); db = Db4oEmbedded.openFile(dbci.getConnectionName()); } try{ db.query(); } catch(Exception e){ db = Db4oEmbedded.openFile(dbci.getConnectionName()); } return db; }Vinegarroon
R
4

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.

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). The former involves no networking while the later does (and can work remotely (aka 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).

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

Best!

Rana answered 16/5, 2011 at 12:42 Comment(1)
Danke schön für die ausführliche Erklärung ;)Heptad

© 2022 - 2024 — McMap. All rights reserved.