Here's an answer from Binod, one of our app server engineers...

Kathey Marsden wrote:
My goal is:
I want to use a specific version of Derby which I ship with my app and I
don't  want to interfere with any other derby versions loaded  in the
same JVM or have them interfere with me.   I am creating a new
datasource in a separate URLClassLoader and using that for creating all
my connections. Are there other things I need to do to meet my goal? I have a feeling it all must be more complex than it looks to me right now.

Thanks

Kathey
Below is some code showing what I have done in playing with this so far.

Method to load derby in separate loader and create datasource:
private static DataSource newDataSource(ClassLoader loader, String
databaseName) throws Exception
    {
        DataSource ds = (DataSource)
loader.loadClass("org.apache.derby.jdbc.EmbeddedDataSource").newInstance();
        // setDatabaseName with reflection
        Class[] argType = {String.class};
        String[] args = new String[] {databaseName};
        Method sh = ds.getClass().getMethod("setDatabaseName", argType);
        sh.invoke(ds, args);
        return ds;
}

    // Calling program ....
    .....
        URL[] urls =
                  new URL[]{new URL(derbyJarURLString)};
            System.out.println(urls[0].getFile());
            ClassLoader loader1 =new URLClassLoader(urls);
            DataSource ds = newDataSource(loader1,"mydb;create=true");
            ds.getConnection();
   ...



--- Begin Message ---

David W. Van Couvering wrote On 11/09/05 10:51 PM,:
> Hi, guys.  Is there any helpful information you can provide Kathey?  You 
> can respond to me or directly to [EMAIL PROTECTED]  If you load 
> one class with a classloader, will all other classes for objects created 
> by that class be loaded from the same classloader?  If not, how do you 
> achieve what she is trying to accomplish?

Make sure that there is only one URL classloader instance that has
access to the derby jar files. And make sure that whenever a code
accesses the datasource, it always go thru newDataSource method, then
yes, it should work.

- Binod.

p.s: This is all assuming that derby code doent do any other interesting
classloader mechanism inside :-). For example, it can use Thread's
context classLoader to load some other part of derby that might break.


> 
> Thanks!
> 
> David
> 
> 
> ------------------------------------------------------------------------
> 
> Subject:
> Question about using URLClassLoader and Derby
> From:
> Kathey Marsden <[EMAIL PROTECTED]>
> Date:
> Wed, 09 Nov 2005 08:55:58 -0800
> To:
> Derby Discussion <[email protected]>
> 
> 
> My goal is:
> I want to use a specific version of Derby which I ship with my app and I
> don't  want to interfere with any other derby versions loaded  in the
> same JVM or have them interfere with me.   I am creating a new
> datasource in a separate URLClassLoader and using that for creating all
> my connections.  Are there other things I need to do to meet my goal?  
> I have a feeling it all must be more complex than it looks to me right now.
> 
> Thanks
> 
> Kathey
>    
> Below is some code showing what I have done in playing with this  so far.
> 
> Method to load derby in separate loader and create datasource:
>       
>       private static DataSource newDataSource(ClassLoader loader, String
> databaseName) throws Exception
>     {
>         DataSource ds = (DataSource)
> loader.loadClass("org.apache.derby.jdbc.EmbeddedDataSource").newInstance();
>         // setDatabaseName with reflection
>         Class[] argType = {String.class};
>         String[] args = new String[] {databaseName};
>         Method sh = ds.getClass().getMethod("setDatabaseName", argType);
>         sh.invoke(ds, args);
>         return ds;
>        
>     }
> 
>     // Calling program ....
>     .....
>         URL[] urls =
>                   new URL[]{new URL(derbyJarURLString)};
>             System.out.println(urls[0].getFile());
>             ClassLoader loader1 =new URLClassLoader(urls);
>             DataSource ds = newDataSource(loader1,"mydb;create=true");
>             ds.getConnection();
>    ...
> 
> 
> 

-- 





--- End Message ---
begin:vcard
fn:David W Van Couvering
n:Van Couvering;David W
org:Sun Microsystems, Inc.;Database Technology Group
email;internet:[EMAIL PROTECTED]
title:Senior Staff Software Engineer
tel;work:510-550-6819
tel;cell:510-684-7281
x-mozilla-html:TRUE
version:2.1
end:vcard

Reply via email to