开发者

How to close a db4o connection when application is shutdown/restarted?

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,开发者_如何学Go 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜