Re: Exception in SAXParserFactory.newInstance with 3.0M4

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Tue Jul 01 2008 - 15:07:37 EDT

  • Next message: Jerald Dawson: "Re: Exception in SAXParserFactory.newInstance with 3.0M4"

    I don't have SWT experience, but clearly thread ClassLoader is not
    present there by default. I hope otherwise the structure is flat and
    there is no nested CL. Anyways, I committed the fix. The link to the
    patched jar is the same:

        http://people.apache.org/~aadamchik/patched/cayenne-server-3.0-07012008.jar

    Don't have time now to check what other places reference the thread CL.

    (but of course there's always an option to do this:
    Thread
    .currentThread
    ().setContextClassLoader(myclass.getClass().getClassLoader()))

    Andrus

    On Jul 1, 2008, at 9:51 PM, Scott Anderson wrote:

    > Jerald,
    >
    > If this is a similar ClassLoader issue, you can try explicitly
    > sending a
    > (String)null to Cayenne for the driver name, forcing it to go through
    > the DriverManger, with which you will register your JDBC driver.
    >
    > I have a similar situation in one of my applications, except that I am
    > forcing Cayenne to go through a different ClassLoader than the JVM's
    > default. You have the exact opposite case from mine, but my solution
    > should work for you as well, as long as you're able to instantiate the
    > JDBC driver.
    >
    > http://bnubot.googlecode.com/svn/trunk/BNUBot/src/net/bnubot/db/conf/Cay
    > enneConfiguration.java
    >
    >
    > HTH,
    > Scott
    >
    > -----Original Message-----
    > From: Jerald Dawson [mailto:jdawso..rk.com]
    > Sent: Tuesday, July 01, 2008 2:44 PM
    > To: use..ayenne.apache.org
    > Subject: Re: Exception in SAXParserFactory.newInstance with 3.0M4
    >
    > Thanks Andrus
    >
    > I got through that exception but now am getting another one where the
    > org.apache.cayenne.conn.DriverDataSource is trying to load the jdbc
    > driver class. I looked in the source and see that it is similar to the
    > other code we were having issues with so I'm wondering if its the same
    > thing. Here is the code in question where the exception is happening:
    >
    > private static Driver loadDriver(String driverClassName) throws
    > SQLException {
    >
    > Class<?> driverClass;
    > try {
    > driverClass = Class.forName(driverClassName, true, Thread
    > .currentThread()
    > .getContextClassLoader());
    > }
    > catch (Exception ex) {
    > throw new SQLException("Can not load JDBC driver named '"
    > + driverClassName
    > + "': "
    > + ex.getMessage());
    > }
    > ...
    >
    > And here is the stack from the exception:
    > INFO: *** Connecting: FAILURE.
    > java.sql.SQLException: Can not load JDBC driver named
    > 'com.mysql.jdbc.Driver': com/mysql/jdbc/Driver
    > at
    > org
    > .apache
    > .cayenne.conn.DriverDataSource.loadDriver(DriverDataSource.java:
    > 62)
    > at
    > org
    > .apache
    > .cayenne
    > .conn.DriverDataSource.setDriverClassName(DriverDataSource.java:256)
    >
    > As you can see, driverClass is again using getContextClassLoad from
    > Thread,currentThread(). Not 100% sure that its the issue but it seems
    > reasonable. Also, I have verified that the mysql jars are in my
    > classpath. Also, modeler seems to see it fine and is able to access my
    > database.
    >
    > jerald dawson
    >
    > On Jul 1, 2008, at 1:18 PM, Andrus Adamchik wrote:
    >
    >>> BTW, I'm using SWT if that is pertinent.
    >>
    >> It probably is. The ClassLoader settings are likely different.
    >>
    >> Anyways the code in question does look suspect. I implemented a bit
    >> more paranoid version of this method (without actually knowing
    >> whether null values are valid here or not). The code is checked in
    >> to trunk, and you may try this cayenne-server.jar that contains the
    >> fix:
    >>
    >>
    > http://people.apache.org/~aadamchik/patched/cayenne-server-3.0-07012008
    > .
    > jar
    >>
    >> Andrus
    >>
    >>
    >>
    >>
    >> On Jul 1, 2008, at 9:02 PM, Jerald Dawson wrote:
    >>
    >>> Ok, more info. I have been stepping through the code to see what is
    >>> actually throwing the NPE and I've come to this code in
    >>> DataSourceInfo class:
    >>>
    >>> public PasswordEncoding getPasswordEncoder() {
    >>> PasswordEncoding encoder = null;
    >>>
    >>> try {
    >>> encoder = (PasswordEncoding) Thread
    >>> .currentThread()
    >>> .getContextClassLoader()
    >>> .loadClass(getPasswordEncoderClass())
    >>> .newInstance();
    >>> // encoder = (PasswordEncoding)
    >>> // Class.forName(getPasswordEncoderClass()).newInstance();
    >>> }
    >>> ...
    >>>
    >>> Now following the code, I notice that the getContextClassLoader
    >>> call returns null which seems to be the source of the NPE. Is there
    >>> a reason getContextClassLoader would be returning null?
    >>>
    >>> BTW, I'm using SWT if that is pertinent.
    >>
    >>
    >
    >



    This archive was generated by hypermail 2.0.0 : Tue Jul 01 2008 - 15:08:15 EDT