Replies: 7 comments
-
As noted in the chat discussion, normal IRB (non-Rails console) seems to load the driver fine, so it seems that something about the Rails console is interfering with the loading of this JDBC jar. |
Beta Was this translation helpful? Give feedback.
-
I have been playing with JDBC a bit locally and I suspect this is a classloading issue. When I use a script similar to yours to load the SQLite3 JDBC driver, it can never see the proper driver or make a connection. It only works when I specify the jar file at the command line classpath:
I suspect that when running IRB or a script at your command line, something is causing the jar file to be loaded earlier and therefore be usable by the JDBC subsystem. When running in the Rails console, that loading does not happen. |
Beta Was this translation helpful? Give feedback.
-
@gaurish A couple questions for you and things you can try:
This has always been a frustrating process, given how JDBC loads drivers. An alternative for you would be to access the classes of the driver directly (check the docs) and request a connection directly without going through the DriverManager class. |
Beta Was this translation helpful? Give feedback.
-
@gaurish i was to get this working in rails by adding the driver jar file into the $CLASSPATH and using sequel gem. This is not with active-record btw in In order to connect |
Beta Was this translation helpful? Give feedback.
-
A second look at this makes me think that perhaps Rails is initializing the DriverManager early, so subsequent classpath updates are not seen. You could confirm this by accessing DriverManager earlier to trigger its driver discovery logic, and then run the rest of your script after that (including the A workaround might be to have the Rails app you are running within require the jar file much earlier in the boot process, so it is available by the time it accesses DriverManager to set up the database subsystem. |
Beta Was this translation helpful? Give feedback.
-
Not really a JRuby issue, I believe. DriverManager is very quirky about how and when it loads drivers, and I believe this is simply a problem with it loading before the jar file is available. We could use better guidance here, and I'd like to get a better explanation for what is happening, but there's no bug here. I'm moving this to a discussion. |
Beta Was this translation helpful? Give feedback.
-
FWIW, I came across a similar issue and had a pretty ridiculous resolution. The jar was in the
we have a It has at the top java_import java.sql.Driver
java_import java.sql.DriverManager and I 🆕 added at the bottom in 'main' # @see https://github.com/jruby/jruby/wiki/JDBC
# https://github.com/jruby/jruby/issues/7169
# https://github.com/jruby/jruby/discussions/7290
# https://github.com/jruby/jruby/issues/5742
# https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby#referencing-java-classes-using-full-qualified-class-name
def reliable_java_import(fully_qualified_class_name)
java_import fully_qualified_class_name
instance_eval "Java::#{fully_qualified_class_name}"
end then changed in a subclass cwd = File.expand_path('..', __FILE__)
require File.join(cwd, 'jdbc_adapter')
require File.join(cwd, 'jdbc_ads', 'adsjdbc-10.10.0.28.jar')
- java_import 'com.extendedsystems.jdbc.advantage.ADSDriver'
+ reliable_java_import 'com.extendedsystems.jdbc.advantage.ADSDriver' and it just works ✨ Weird. Thanks for the ideas. I was working through the non-magical version (driver = Java::com.extendedsystems.jdbc.advantage.ADSDriver.new |
Beta Was this translation helpful? Give feedback.
-
I am trying load snowflake JDBC driver which works with jruby as standalone script but not within Rails.
working as a script or jruby IRB
https://gist.github.com/gaurish/8336972c32b8645a918b4f51b4bf1c08
But the within rails, driver is not being loaded:
https://gist.github.com/gaurish/8336972c32b8645a918b4f51b4bf1c08#file-rails-console-rb
Environment Information
Darwin tarantula 20.6.0 Darwin Kernel Version 20.6.0: Tue Feb 22 21:10:41 PST 2022; root:xnu-7195.141.26~1/RELEASE_X86_64 x86_64
Other relevant info you may wish to add:
rails new
available at https://github.com/gaurish/debug-rails where this issue can be reproduced.Expected Behavior
Actual Behavior
Errors out. Java::JavaSql::SQLException (No suitable driver found
Beta Was this translation helpful? Give feedback.
All reactions