Oracle still treats empty string as null

It is really a joke that Oracle still treats empty (zero length) strings as null, even in the year 2012! Recently I came (again) across this fact when we were adding support for Oracle 11g in our application.

I was shocked that this is still an issue. Although they may had their reasons in the past, this is really annoying nowadays. I can’t understand why they didn’t make a cut a some point, made the default SQL92 compliant and added an option for the legacy systems. If you don’t know what I’m talking about:

When you have an Entity that has a String attribute, put an empty String (“”) into it and persist it. When you read it back this String attribute is now null instead of “”! This leads to unfunny side-effects especially when you’re already supporting other databases like MySQL, MSSQL or PostgreSQL where this isn’t an issue. Be prepared to re-check queries, required attributes and JSF pages.

How are you handling the situation? Is there a new option I’m probably not aware of?

CDI BeforeShutdown Event not fired

Ever wondered why the JEE6 CDI Event BeforeShutdown isn’t fired when the container shuts down as described in its javadoc?

Well, this is because it’s one of the container lifecycle events that are not accessible for normal CDI managed beans. To get access to these special events, your implementation has to be an CDI Extension.

First, implement the Extension interface:

package org.nightprogrammer.examples.cdi;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.BeforeShutdown;
import javax.enterprise.inject.spi.Extension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BeforeShutdownTest implements Extension {
	private static final Logger log = LoggerFactory.getLogger(BeforeShutdownTest.class);
	public void onShutdown(@Observes BeforeShutdown beforeShutdownEvent) {
		log.debug("shutdown event received!");

Second, create a META-INF/services/javax.enterprise.inject.spi.Extension file containing the full qualified classname of your Extension:


That’s it! Now you receive the Event as soon as the container goes down. Would be great if the javadoc would mention that directly in the BeforeShutdown Event though. That would have saved me an hour of research :(.

What do you use for cleaning up resources before a container shuts down?

(Besides contextDestroyed() for webapps and addShutdownHook() for JavaSE applications.)

base64 encode and decode using JDK

Ever asked yourself why the JDK isn’t shipped with a builtin base64 encoder/decoder? Well, since JDK 6 there is one – although it’s quite buried: javax.xml.bind.DatatypeConverter.

Usage is quite simple:

// encode byte[] to base64 String
String myBase64String = DatatypeConverter.printBase64Binary(myByteData);
// decode base64 String to byte[]
byte[] myByteData = DatatypeConverter.parseBase64Binary(myBase64String);

So no need to add a thirdparty library just for decoding/encoding base64 anymore.

IllegalAccessError, packages and classloaders

Ever wondered why you get an IllegalAccessError when invoking protected/package-private methods on class B from class A although both classes are in the same package?

Well it mostly happens only at runtime when two different classloaders loading the classes. classloader A loads class A, classloader B loads class B. Since the JVM spec, paragraph 5.3 mandates

At runtime, a class or interface is determined not by its name alone, but by a pair: its binary name (§4.2.1) and its defining class loader. Each such class or interface belongs to a single runtime package. The runtime package of a class or interface is determined by the package name and defining class loader of the class or interface.

the package from class A is the same as class B at compile time, but NOT at runtime since it was loaded by different classloaders! To get to the root cause, you have to check why your classes are loaded by different classloaders. For example I came across that problem when using JBoss 7 the first time.