Web application using db4o

It is perfectly possible to use db4o in web environment because its database always works in thread-safe mode. Servlets can be accessed by multiple threads, and the transactions against to the db4o core are made in a thread-safe manner.

Step by step

It is necessary to copy the db4o library to the directory WEB-INF/lib. This way your application will access the class responsible for the creation of the server and of the database client.

The same way that it works in desktop version, we don’t need to worry about how to map the process, neither with containers and other applications.

On the other hand, it is necessary to do some configuration. First of all, we have to configure the access to the database file and after this we must think in a way of creating only the ObjectServer for each application.

The great way for this to work is to create the ObjectServer in the startup of the application context and close the connection when the context is ended. To get it we need to elaborate a Listener class, which is not given by the db4o API.

Here you have an example code of a Listener that comes together with db4o tutorial:


package myproject.dao;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.db4o.Db4o;
import com.db4o.ObjectServer;

public class Db4oServletContextListener implements ServletContextListener {
public static final String KEY_DB4O_FILE_NAME = "db4oFileName";

public static final String KEY_DB4O_SERVER = "db4oServer";

private ObjectServer server = null;

public void contextInitialized(ServletContextEvent event) {
close();
ServletContext context = event.getServletContext();
String filePath = context.getRealPath("WEB-INF/db/"
+ context.getInitParameter(KEY_DB4O_FILE_NAME));
server = Db4o.openServer(filePath, 0);
context.setAttribute(KEY_DB4O_SERVER, server);
context.log("db4o startup on " + filePath);
}

public void contextDestroyed(ServletContextEvent event) {
ServletContext context = event.getServletContext();
context.removeAttribute(KEY_DB4O_SERVER);
close();
context.log("db4o shutdown");
}

private void close() {
if (server != null) {
server.close();
}
server = null;
}

}

It is very simple to visualize what this code does. When the context is started the db4oServer is instantiated. And when the context is stopped the connection with the database is ended. After that you have to configure the Listener to the application context of its application, inside the web.xml file that can be found in WEB-INF directory:


<listener>
<listener-class>meuprojeto.dao.Db4oServletContextListener</listener-class>
</listener>

Observe that it is necessary to describe the complete name of the class (to list the packet where the Listener is). Now we need to configure the db4o file path that will be used by Listener:


<context-param>
<param-name>db4oFileName</param-name>
<param-value>web.yap</param-value>
</context-param>

Observe that the constant KEY_DB4O_FILE_NAME of the Listener has the same value of the context parameter.I really appreciate this system.

Anúncios

~ por Glaucio Guerra em dezembro 8, 2007.

3 Respostas to “Web application using db4o”

  1. I recently starting using db4O and I have to say thank you for providing your experience in using db4O in the context of a web application. Many of the issues you discussed helped in my re-education from RDBMS to true OODBMS. Keep up the good work.

  2. good job! well done! try to write a longer version with pages and others elements a web’s app have, many of us will apreciate it!
    Thanks againt.

  3. I’m curious how you access the ObjectContainer in your design. In the reference docs it says “only one transaction is allowed per object container instance.” Does this imply you should have a pool of containers similiar to a traditional relational model and that a container should be assigned or openned upon every request and closed when the reply returns.

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair /  Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair /  Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )

Conectando a %s

 
%d blogueiros gostam disto: