Chapter 2. Exception Handling and Debugging

Table of Contents

Debugging BDB XML Applications

Before continuing, it is helpful to look at exception handling and debugging tools in the BDB XML API.

All BDB XML operations can throw an exception, and so they must be within a try block, or the encompassing method must declare a throwable exception.

BDB XML operations throw XmlException objects. XmlException is inherited from the Berkeley DB DbException class, which is in turn inherited from the normal Java Exception class. You can therefore catch all exceptions thrown by BDB XML classes by catching Exception, although for clarity in reporting purposes you might want to catch each type of exception individually.

The following example illustrates BDB XML exception handling.

package dbxml.gettingStarted;
import com.sleepycat.dbxml.XmlContainer;
import com.sleepycat.dbxml.XmlException;
import com.sleepycat.dbxml.XmlManager;

class doDbXml
{
    public static void
    main(String args[]) throws Throwable
    {
        XmlContainer myContainer = null;
        XmlManager myManager = null;
        String theContainer = "container.dbxml";

        // Open an XmlManager and an XmlContainer.
        try {
            myManager = new XmlManager();
            myContainer = myManager.openContainer(theContainer);

        } catch (XmlException e) {
            // Error handling goes here
        }
    }
} 

Note that, you can obtain more information on the cause of the XmlException by examining the underlying error code. Do this using the XmlException.getErrorCode() method. See the Berkeley DB XML Javadoc for details on the exception codes available through this class.

Debugging BDB XML Applications

In some cases, the exceptions thrown by your BDB XML application may not contain enough information to allow you to debug the source of an error. For this reason, it is always a good idea to create a custom error handler.

Once you have implemented your error handler, you make it known to you BDB XML application using EnvironmentConfig.setErrorHandler()

For example, first you provide an implementation of com.sleepycat.db.ErrorHandler. (Here we provide a trivial implementation of the class, simply for illustration purposes).

package dbxml.gettingStarted;

import com.sleepycat.db.Environment;
import com.sleepycat.db.ErrorHandler;

public class MyErrorHandler implements ErrorHandler
{
    public void error(Environment env,
               String errpfx,
               String msg)
    {
        System.err.println(errpfx + " : " + msg);
    }
}   

Then you provide this class object to EnvironmentConfig.setErrorHandler().

package dbxml.gettingStarted;
import java.io.File;
import java.io.FileNotFoundException;

import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;

import com.sleepycat.dbxml.XmlManager;
import com.sleepycat.dbxml.XmlException;

class doDbXml
{
    public static void
    main(String args[]) throws Throwable
    {
        Environment myEnv = null;
        XmlManager myManager = null;
        File envHome = new File("/export1/testEnv");

        // Open an XmlManager
        try {
           // Set the error handler for this application
           EnvironmentConfig envConf = new EnvironmentConfig();
           meh = new MyErrorHandler();
           envConf.setErrorHandler(meh);
           myEnv = new Environment(envHome, envConf);

           myManager = new XmlManager(myEnv);

        } catch (XmlException e) {
            // Error handling goes here
        }
    }
} 

Once you have set up your error handler, you can control the amount of information that BDB XML reports to that handler using setLogLevel() and setLogCategory().

setLogLevel() allows you to indicate the level of logging that you want to see (debug, info, warning, error, or all of these).

setLogCategory() allows you to indicate the portions of DB XML's subsystems for which you want logging messages issued (indexer, query processor, optimizer, dictionary, container, or all of these).

package dbxml.gettingStarted;
import java.io.File;
import java.io.FileNotFoundException;

import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;

import com.sleepycat.dbxml.XmlManager;
import com.sleepycat.dbxml.XmlException;

class doDbXml
{
    public static void
    main(String args[]) throws Throwable
    {
        Environment myEnv = null;
        XmlManager myManager = null;
        File envHome = new File("/export1/testEnv");

        // Open an XmlManager
        try {
           // Set the error handler for this application
           EnvironmentConfig envConf = new EnvironmentConfig();
           meh = new MyErrorHandler();
           envConf.setErrorHandler(meh);
           myEnv = new Environment(envHome, envConf);

           myManager = new XmlManager(myEnv);
           myManager.setLogLevel(LEVEL_ALL, true);
           myManager.setLogCategory(CATEGORY_ALL, true);

        } catch (XmlException e) {
            // Error handling goes here
        }
    }
}