db4o Developer Community

db4o open source object database, native to Java and .NET
Welcome to db4o Developer Community Sign in | Join
in Search
More Search Options

Community News

Use db4o in you Guice applications through Warp

For those of you trying to catch up, Guice is a lightweight dependency injection framework for Java 5 and above, by Google. The guys from Wideplay Interactive (Dhanji and Josh) provide an eco-system for Guice called "Warp" comprised of independent, lightweight modules to enhance Guice applications with persistence, transactions, servlets, and so on.
 
Quite recently Jeff Chung contributed Wideplay's integration for db4objects for the persistence module (warp-persist). Now in version 1.0, warp-persist provides drop-in support for persistence and transactions with Hibernate, JPA and db4objects.

To enable db4o persistence support, you need to configure the module when creating your injector:

Injector injector = Guice.createInjector(..., 
PersistenceService.usingDb4o().buildModule());
You must bind in the name of a file db4o can use as its data store in one of 
your own modules, for instance:
bindConstant().annotatedWith(Db4Objects.class).to("mystore.dat");

This creates a (or uses an existing) db file named mystore.dat. Alternatively,
you can connect to a db4o object server at a remote location:

 

bindConstant().annotatedWith(Db4Objects.class).to("mystore.dat");

bindConstant().annotatedWith(Names.named(Db4Objects.HOST)).to("localhost");
bindConstant().annotatedWith(Names.named(Db4Objects.PORT)).to(4321);
bindConstant().annotatedWith(Names.named(Db4Objects.USER)).to("autobot");
bindConstant().annotatedWith(Names.named(Db4Objects.PASSWORD)).to("morethanmeetstheeye");
 

Once you have the injector created, you can freely inject and use an
ObjectContainer in your transactional services:

 

import com.wideplay.warp.persist.Transactional;
import com.db4o.ObjectContainer;
... 
 
public class MyService {
	@Inject Provider<ObjectContainer> oc; 
 
	@Transactional 
	public void createNewPerson() {
		oc.get().set(new Person(...)); 
	} 
} 
 
If you want more infomation about warp-persist and db4o (incluidng how to use 
both in Web Environments (session-per-http-request)) see:
 
http://www.wideplay.com/webextensions%3A%3Ajpaintegration22
 
Thanks Jeff, Dhanji and Josh!!
Published Monday, March 03, 2008 6:50 AM by German Viscuso

Comments

No Comments
Anonymous comments are disabled