pg.ddx.io  pljava-dev@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Subject: [Pljava-dev] A fix for Class.forName issues with pl/java
Date: Sat, 22 Dec 2012 18:32:15 -0800
Message-ID: <63DDD4A5-BBEA-4947-8794-1CCE5D27DC04@me.com> (raw)

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




Message-ID: <63DDD4A5-BBEA-4947-8794-1CCE5D27DC04@me.com>
Permalink:  ../63DDD4A5-BBEA-4947-8794-1CCE5D27DC04@me.com/
Also on:    postgresql.org/message-id/63DDD4A5-BBEA-4947-8794-1CCE5D27DC04@me.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pljava-dev@postgresql.org
  Subject: Re: [Pljava-dev] A fix for Class.forName issues with pl/java
  In-Reply-To: <63DDD4A5-BBEA-4947-8794-1CCE5D27DC04@me.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox