tags:

views:

16

answers:

1

I have just started writing my first webapp using java/wicket/db4o and all technologies are new to me so please forgive what might be a daft question.

I have created a simple MyDao singleton class that connects to a file-based db4o database and I can write some objects into it and read them back. The problem I have is each time I redeploy my app from Netbeans, I get a com.db4o.ext.DatabaseFileLockedException exception during the tests. This goes away if I delete the DB.

I would think that I need to close() the db4o connection once my MyDao object goes out of scope, but I dont know how to do this. If I was using Perl, I would have a DESTROY() method in my MyDao class that calls the close() on my db4o db. Im not sure what the equivalent mechanism is in java. I have tried to use the finalize() method on my MyDao object but this hasnt worked .

Thanks in advance. Tom

+1  A: 

Hi

What do you do when you redeploy the application? Shutting down the Virtual Machine or just redeploy without restarting the JVM and the Webcontainer?

If you don't restart the webserver you've probably the issue that the singleton still exists and keeps the db4o database running. While the old instance is running you cannot open the database file with another db4o instance. Otherwise it would corrupt the database. This means that you need to shut down the db4o instance when you 'close' the database-application.

I don't know Wicket all to well. Probably it provides a method to take action when you 'shut'-down the application. Otherwise you can always use the raw Java-Web-Facilities. There a ServletContextListener-interface which is called when you start, stop or redeploy the application:

For example:

public class DatabaseSingleton implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        // initialize the database.
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // close database
        System.out.println("close");
    }

}

And then register the it in the web.xml:

    <listener>
    <listener-class>info.gamlor.DatabaseSingleton</listener-class>
</listener>

Maybe also take a look at the db4o servlet-example which also does this.

Gamlor
This looks very useful - thank you very much!
Tom
I am sure this will turn out to be useful however I discovered a bug in my code that was causing the problem. I was using the same db4o file for my tests as for the running version of my app. So, after deploying, when I ran my tests they failed because my app was still running and had the db4o file locked!!
Tom