Issue Type: Bug Bug
Affects Versions: JRuby 1.7.4, JRuby 1.7.2
Assignee: Thomas E Enebo
Attachments: jruby_complete_drivermanager.zip
Created: 10/Jun/13 4:06 AM
Description:

I'm getting very strange (or should we say "interesting") problems when doing some very straight-forward things with DriverManager.getConnection() from JRuby.

The error message I get is this (full details further down):

DriverManager.java:604:in `getConnection': java.sql.SQLException: No suitable driver found for jdbc:hsqldb:dummy

This is even though the driver has been registered like this:

$LOAD_PATH << '.'   # Needed for jruby-complete.jar

require 'java'
require 'hsqldb.jar'

# Just to get debugging output.
java::sql::DriverManager.log_writer = java::io::PrintWriter.new(java::lang::System.out, true)

org::hsqldb::jdbc::JDBCDriver.new
conn = java::sql::DriverManager.get_connection('jdbc:hsqldb:dummy')
puts conn

The funny/weird thing is this: The script above works perfectly with JRuby installed as an .exe/binary (Windows/OSX). It's only when we are using jruby-complete-1.7.x.jar that this problem occurs (tested with 1.7.2 and 1.7.4, same problem with both). The full output of the above program (which is attached as a test case btw) is this:

DriverManager.getConnection("jdbc:hsqldb:dummy")
    skipping: java.sql.DriverInfo
getConnection: no suitable driver found for jdbc:hsqldb:dummy
java.sql.SQLException: No suitable driver found for jdbc:hsqldb:dummy
	at java.sql.DriverManager.getConnection(DriverManager.java:604)
	at java.sql.DriverManager.getConnection(DriverManager.java:243)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at org.jruby.javasupport.JavaMethod.invokeDirectWithExceptionHandling(JavaMethod.java:455)
	at org.jruby.javasupport.JavaMethod.invokeStaticDirect(JavaMethod.java:367)
	at org.jruby.java.invokers.StaticMethodInvoker.call(StaticMethodInvoker.java:60)
	at org.jruby.internal.runtime.methods.AliasMethod.call(AliasMethod.java:61)
	at org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:326)
	at org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:170)
	at jdbc_test.__file__(jdbc_test.rb:10)
	at jdbc_test.load(jdbc_test.rb)
	at org.jruby.Ruby.runScript(Ruby.java:810)
	at org.jruby.Ruby.runScript(Ruby.java:803)
	at org.jruby.Ruby.runNormally(Ruby.java:674)
	at org.jruby.Ruby.runFromMain(Ruby.java:523)
	at org.jruby.Main.doRunFromMain(Main.java:390)
	at org.jruby.Main.internalRun(Main.java:279)
	at org.jruby.Main.run(Main.java:221)
	at org.jruby.Main.main(Main.java:201)
SQLException: SQLState(08001)
DriverManager.deregisterDriver: org.hsqldb.jdbc.JDBCDriver@3e36ae6d
DriverManager.java:604:in `getConnection': java.sql.SQLException: No suitable driver found for jdbc:hsqldb:dummy
	from DriverManager.java:243:in `getConnection'
	from NativeMethodAccessorImpl.java:-2:in `invoke0'
	from NativeMethodAccessorImpl.java:57:in `invoke'
	from DelegatingMethodAccessorImpl.java:43:in `invoke'
	from Method.java:601:in `invoke'
	from JavaMethod.java:455:in `invokeDirectWithExceptionHandling'
	from JavaMethod.java:367:in `invokeStaticDirect'
	from StaticMethodInvoker.java:60:in `call'
	from AliasMethod.java:61:in `call'
	from CachingCallSite.java:326:in `cacheAndCall'
	from CachingCallSite.java:170:in `call'
	from jdbc_test.rb:10:in `__file__'
	from jdbc_test.rb:-1:in `load'
	from Ruby.java:810:in `runScript'
	from Ruby.java:803:in `runScript'
	from Ruby.java:674:in `runNormally'
	from Ruby.java:523:in `runFromMain'
	from Main.java:390:in `doRunFromMain'
	from Main.java:279:in `internalRun'
	from Main.java:221:in `run'
	from Main.java:201:in `main'

When running with jruby.exe/jruby it gives this instead:

DriverManager.getConnection("jdbc:hsqldb:dummy")
    trying org.hsqldb.jdbc.JDBCDriver
getConnection returning org.hsqldb.jdbc.JDBCDriver
org.hsqldb.jdbc.JDBCConnection@6b0f6d29
DriverManager.deregisterDriver: org.hsqldb.jdbc.JDBCDriver@16744cae

...so, the line "skipping: java.sql.DriverInfo" is obviously the problem. We did some research on this and found this in the DriverManager source code (http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/sql/DriverManager.java#DriverManager.getConnection%28java.lang.String%2Cjava.util.Properties%2Cjava.lang.ClassLoader%29), line 612:

            // If the caller does not have permission to load the driver then
            // skip it.
            if ( getCallerClass(callerCL, di.driverClassName ) !=
                 di.driverClass ) {
                println("    skipping: " + di);
                continue;
            }

It somehow seems to have to do with the ClassLoader(s) being involved. I guess the classloader is different for jruby-complete.jar vs "standard" JRuby? Are there any ways to work around this problem?

(What we did for a temporary workaround was to have a little .java file which wraps the call to DriverManager.getConnection() for us. This works fine; obviously, .jar files being loaded in JRuby are not in the same "context" or something like normal .rb code being executed. I must admit Java classloading is not my greatest strength, so maybe someone with more experience in this regard can shed further light on the matter...)

Oh, and one more word: This has nothing to do with HSQLDB, it's purely a problem with the JDBC DriverManager. This is just a simplified test case to make it reproducible/debuggable. In real life, the jt400.jar driver is being used, but it really doesn't make any difference here: they work equally well/poor in this regard.

Environment: OSX, Windows
Project: JRuby
Labels: java jdbc
Priority: Major Major
Reporter: Per Lundberg
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira
--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email

Reply via email to