Calling a query on a table with BLOBs (Files in Tables) using the jTDS-Driver I get a class-loader error:

java.lang.IllegalArgumentException: interface org.hibernate.engine.jdbc.WrappedBlob is not visible from class loader

My Solution was to switch to an other classloader:

  public synchronized void method() {

        Thread thread = Thread.currentThread();
        ClassLoader tempLoader = thread.getContextClassLoader();
        thread.setContextClassLoader(null);

        entityManager.createNativeQuery("select * from TSRPTBLB where TSBID = 1 ")
                       .getSingleResult();

        thread.setContextClassLoader(tempLoader);
  }

Will there be any other, cleaner way than switsching the classloader?

Thank you in advance

asked 09.09.2014 at 14:41

Alexis's gravatar image

Alexis
(suspended)
accept rate: 66%


Yes there is a better way:

  1. You should explicitly set the correct ClassLoader to a class in the JAR where you run into problems (e.g set the class loader from WrappedBlob.class)
  2. Add a finally statement so that in case of an exception you can still reset the original context ClassLoader or the server will be in a corrupt state

So if you consider both points the code could look as follows:

public synchronized void method() {
    Thread thread = Thread.currentThread();
    java.lang.ClassLoader originalClassLoader = thread.getContextClassLoader();
    thread.setContextClassLoader(WrappedBlob.class.getClassLoader());
    try {
        entityManager.createNativeQuery(
                "select * from TSRPTBLB where TSBID = 1 ")
                .getSingleResult();
    } finally {
        thread.setContextClassLoader(originalClassLoader);
    }
}
link

answered 11.09.2014 at 15:44

Reguel%20Wermelinger's gravatar image

Reguel Werme... ♦♦
9.4k31958
accept rate: 70%

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×147
×52
×32

Asked: 09.09.2014 at 14:41

Seen: 3,910 times

Last updated: 11.09.2014 at 15:44