Db4oUtil (aka. The Easiest Way to Get Started With Db4o)
Want the quickest and easiest way to get started with db4o? Use the following Db4oUtil class. This class is very similar to the HibernateUtil class that you may be familiar with if you're a Hibernate user. It uses a thread local object so it's thread safe, makes multi-tier usage a no brainer and it's just plain simple to use.
The simple step-by-step for this tutorial:
- Copy the Db4oUtil.java code below into a new class
- Use the Db4oUtil methods in your code, samples below
That is it... seriously. You can optionally tweak the fields in Db4oUtil to your liking, like you'll want to change the YAPFILENAME field for each application you will use this with.
Db4oUtil.java
public class Db4oUtil {
// EDIT THESE SETTINGS
private static final String YAPFILENAME = "test.db4o.yap";
private static final int PORT = 0;
/*
If you want the server to be networked, change the port number above and uncomment the USER, PASSWORD lines below/
Then in getObjectServer, uncomment the objectServer.grantAccess line
*/
//private static final String USER = "username";
//private static final String PASSWORD = "password";
private static ObjectServer objectServer;
private static final ThreadLocal dbThreadLocal = new ThreadLocal();
public static ObjectContainer getObjectContainer() {
ObjectContainer oc = (ObjectContainer) dbThreadLocal.get();
if (oc == null || oc.ext().isClosed()) {
oc = getObjectServer().openClient();
dbThreadLocal.set(oc);
}
return oc;
}
public static void closeObjectContainer() {
ObjectContainer oc = (ObjectContainer) dbThreadLocal.get();
dbThreadLocal.set(null);
if (oc != null) oc.close();
}
public synchronized static ObjectServer getObjectServer() {
if (objectServer == null) {
objectServer = getObjectServerForFilename(YAPFILENAME, PORT);
// and give access
//objectServer.grantAccess(USER, PASSWORD);
}
return objectServer;
}
public static void shutdown() {
if (objectServer != null) {
objectServer.close();
}
}
public static ObjectServer getObjectServerForFilename(String yapfilename, int port) {
File parentDir = getDbDirectory();
File dbfile = new File(parentDir, yapfilename);
// for replication //////////////////////////
Db4o.configure().generateUUIDs(Integer.MAX_VALUE);
Db4o.configure().generateVersionNumbers(Integer.MAX_VALUE);
// other options
Db4o.configure().exceptionsOnNotStorable(true);
Db4o.configure().objectClass("java.math.BigDecimal").translate(new com.db4o.config.TSerializable());
// now open server
ObjectServer objectServer = Db4o.openServer(dbfile.getPath(), port);
return objectServer;
}
private static File getDbDirectory() {
// will store data files in {user.home}/db4o/data directory
String dbfile = System.getProperty("user.home") + "/db4o/data";
File f = new File(dbfile);
if (!f.exists()) {
f.mkdirs();
}
return f;
}
}
You can edit the fields at the top to modify your database filename, etc.Using Db4oUtil
This is a simple code sample of usage:// Get ObjectContainer - you can repetitively call this function in different methods without the need to pass the ObjectContainer aroundThat's all there is to it!
ObjectContainer oc = Db4oUtil.getObjectContainer();
// YOUR CODE HERE - use the ObjectContainer and do all your stuff here
// Close ObjectContainer when done
Db4oUtil.closeDb();
// Close object server when completely exiting application
Db4oUtil.shutdown();


4 Comments:
Is the db4oUtil.java class all I need to begin using db4o with Tomcat? Will the helper class be able to handle multiple connections to the db4o server like a typical web applications? I can't believe its could be that easy :)
Although this can be used in any applicaion, it can definitely handle multiple connections from multiple tomcat requests with no problem. For each request, you can use Db4oUtil.getObjectContainer() throughout the entire request cycle and you'll be sure to get the exact same ObjectContainer for each call. Be sure to close the connections at the end of each request though - Db4oUtil.closeObjectContainer().
Also, if you think you'll be getting heavy traffic, you may want to plug a Connection Pooling mechanism in there.
Can someone please read this topic: http://developer.db4o.com/forums/thread/29406.aspx on the db4o forum, i'm having too much problems with duplicating objects when storing object that has another object in it. I made my program just like travis reeder said, I should be getting the same objectcontainer over and over, but this seems not to be the case...
Marko, you will get the same ObjectContainer in the same thread until you call Db4oUtil.closeObjectContainer(). In a web application if you're using a ServletFilter to close it, then it will be the same ObjectContainer for the life of a single request.
Generally, you'll have to re-get your object when submitting, then copy the fields. This is a big pain, I realize, but hopefully db4o will have better ID support soon. Keep an eye on COR-177.
I make good use of BeanUtils from Apache for copying object fields.
Post a Comment
Links to this post:
Create a Link
<< Home