2 How to program servlets with X-Hive/DB

Programming a servlet is not really different from programming other applications. We do provide a sample servlet, which shows how to combine them programmatically. In this article two common issues that often come up with servlets are dealt with; using a session pool and classpath issues.

The information in this section only applies to X-Hive/DB 6. When using earlier versions of X-Hive/DB (4 and earlier), the properties for classloading are slightly different because of native libraries, and the session pool optimizations are also different. See previous Advanced topics release 4.0 for information on earlier versions of X-Hive/DB.

Summary of this article:

2.1 Classpath Issues

The information in this section applies mostly to the Tomcat servlet engine. It is about how to how to set up a classpath, specifically how Tomcat deals with this. It does not explain fully how the Tomcat classloader system works, please see the Tomcat documentation for this.

In Tomcat, like other servlet engines, the proper way to deal with the classpath is to create a WEB-INF/lib directory for your servlet and copy all the jars to this directory. This is what is advised in the servlet specification. It is also where jars are placed if an application gets unpacked from a .war file. However, there some serious downsides to placing the X-Hive/DB related jars in WEB-INF/lib:

One way to prevent some of these problems is to place all X-Hive/DB jars in what tomcat calls the 'common lib', which is the directory tomcatDir/common/lib. Jars in this directory are shared between all servlets. The classloader which loads the classes from this directory does not unload classes, which means the JNI library will not be loaded multiple times. Here follows a list of jar placement tips:

2.1.1 Common exceptions from Tomcat

Because the classloading issues described can sometimes lead to confusing exceptions and error messages, we will list some common ones and their causes here. These errors may be shown in your webbrowser accessing Tomcat, on the console of the running Tomcat process, or in the file tomcatDir/logs/localhost_log.<date>.txt:

2.2 Session Pooling

It should be noted that although the creation of sessions in X-Hive/DB still takes some time, especially when connecting to an external (dedicated) server (which involves setting up a TCP/IP connection), creating a session for every request does not automatically mean bad performance, as it did in previous versions of X-Hive/DB. Therefor the information in this section could be considered as deprecated, except for the remarks on read-only sessions.

In the sample servlet we create a stack of sessions, from which sessions are taken and returned when needed. Each time a session is used a connection is made on it and a transaction is opened. In this way the session pool can be used in a generic way. You can use this mechanism too in your applications. As the goal of the pool is to increase the responsiveness, we describe three possible ways to increase the reaction speed and explain the merits of each one. Only the first tip on read-only sessions can lead to great improvements of the performance of your system with X-Hive/DB, because the two other items describe bottlenecks that no longer exist in X-Hive/DB 5: