[Pljava-dev] A fix for Class.forName issues with pl/java

From: hal(dot)hildebrand at me(dot)com (Hal Hildebrand)
To:
Subject: [Pljava-dev] A fix for Class.forName issues with pl/java
Date: 2012-12-23 02:32:15
Message-ID: 63DDD4A5-BBEA-4947-8794-1CCE5D27DC04@me.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pljava-dev

Having run into this myself, I was rather disheartened by the lack of information about resolving this problem available on the intertubes. The best I could find was one entry (http://forums.devshed.com/showpost.php?p=2116328&postcount=3) which resolved the problem by placing the jars on the pl/java classpath - not the SQL/J runtime classpath. While this does, in fact, solve the problem it is a rather poor solution because it requires reconfiguring and rebooting postgres every time you update or change your class path.

Fortunately, I got to the bottom of this and while not solving it completely, at least came up with a easy to implement work around that completely obviates the need to use the pl/java JVM classpath.

The problem comes about because the pl/java JNI does not set the current thread's context class loader. Because this is null, the only thing that Class.forName() has to go on is the JVM classpath. This might be due to the way the classes loaded via org.postgresql.pljava.sqlj.Loader, but my low level class loader lore is a bit rusty so I'm not sure if this is the proximate case.

In any event, if you set the context class loader of the thread to the class loader derived from the class who's static method postgres is calling into the JVM with, then Class.forName will work as expected and will find the classes using both the SQL/J classpath and the JVM classpath defined in the postgres configuration file.

For example:

public static void mySqlEntryPoint(Long id) {
Thread.currentThread().setContextClassLoader(MySqlClass.class.getClassLoader());
}

I believe that this should be done in the JNI entry point logic, but until that happens you can do the above. I believe you'll have to do it for each entry, as this is likely not sticky.

- Hal

Browse pljava-dev by date

  From Date Subject
Next Message Nico Haase 2013-01-08 10:07:25 [Pljava-dev] Downloads not available?
Previous Message Fouapé Bruno 2012-12-05 12:37:12 [Pljava-dev] Pljava-dev Digest, Vol 101, Issue 4