views:

165

answers:

1

Derby has a series of configuration options that are controlled by system properties. It's quite painful to arrange system property settings in a webapp. Has anyone come up with a solution?

In addition, I have been unable to make them work in a webapp.

Here is the code of a servlet context listener. derby.log is still created in the cwd of the container, rather than calls being made to my logging procedure.

/**
 * Listener to try to get Derby to behave better.
 */
public class ContextListener implements ServletContextListener {

    private static final String TEMP_DIR_ATTRIBUTE = "javax.servlet.context.tempdir";
    private static ServletContext context;
    private static Writer logWriter;

    private class LogWriter extends Writer {

        @Override
        public void close() throws IOException {
        }

        @Override
        public void flush() throws IOException {
        }

        @Override
        public void write(char[] cbuf, int off, int len) throws IOException {
            context.log(new String(cbuf, off, len));
        }

    }

    /** {@inheritDoc}*/
    public void contextDestroyed(ServletContextEvent sce) {
    }

    public static Writer getLogSteam() {
        return logWriter;
    }

    /** {@inheritDoc}*/
    public void contextInitialized(ServletContextEvent sce) {
        logWriter = new LogWriter();
        File tempDirFile = (File)sce.getServletContext().getAttribute(TEMP_DIR_ATTRIBUTE);
        context = sce.getServletContext();
        System.setProperty("derby.system.home", tempDirFile.getAbsolutePath());
        System.setProperty("derby.stream.error.method", "com.basistech.vws.ContextListener.getLogStream");
    }

}
A: 

There are multiple ways to set Derby properties. Here's some docs: http://db.apache.org/derby/docs/10.5/devguide/cdevsetprop24222.html You don't say which part of this you find to be painful, though, so it's hard to know what sort of solution you're looking for.

Which properties are you trying to set, and what aspect of setting them is proving painful?

Bryan Pendleton
I don't like to tell customers / users that they have to add system properties to the launch of tomcat/whatever. What seems to be missing is the ability to just put something in the classpath. If I've missed something, please let me now. I have been through that doc.
bmargulies
Well, depending on exactly what properties you're trying to set, and how your application is set up, here are some other techniques: First, you could put a derby.properties file into the location that you're using as your derby home. Second, you could call System.setProperty() yourself prior to loading the Derby JDBC driver.
Bryan Pendleton
Check the answers to http://stackoverflow.com/questions/1599043/limit-derby-log-file-size for some actual code that demonstrates the System.setProperty technique.
Bryan Pendleton
@Bryan Pendleton, those aren't available in webapps very well.
bmargulies