[jruby-user] pre-newbie help needed

8 views
Skip to first unread message

Perry Smith

unread,
May 16, 2013, 9:49:01 PM5/16/13
to us...@jruby.codehaus.org
Warning: I know zero about Java :-)

I am trying to connect (read only) to an IBM Cloudscape database. This is what eventually became Derby. I have a list of jar files:

db2j.jar
db2jcc.jar
db2jcview.jar
db2jnet.jar
db2jtools.jar

that a real Java application uses to access the same database. The database is in a local directory like:

find db -type f -print | head -20
db/OpcClientDb/db.lck
db/OpcClientDb/dbex.lck
db/OpcClientDb/log/log.ctrl
db/OpcClientDb/log/log48978.dat
db/OpcClientDb/log/logmirror.ctrl
db/OpcClientDb/seg0/A1
db/OpcClientDb/seg0/c10.dat
db/OpcClientDb/seg0/c101.dat
db/OpcClientDb/seg0/c111.dat
db/OpcClientDb/seg0/c121.dat
db/OpcClientDb/seg0/c130.dat
db/OpcClientDb/seg0/c141.dat
db/OpcClientDb/seg0/c150.dat
...

Poking at the internet so far I've some up with this:
====
#! /usr/bin/env jruby

include Java

require 'lib/db2j.jar'
require 'lib/db2jcc.jar'
require 'lib/db2jcview.jar'
require 'lib/db2jnet.jar'
require 'lib/db2jtools.jar'

url = 'jdbc:db2j:db/OpcClientDb'
conn = java.sql.DriverManager.get_connection(url)
====

which gets me:

DriverManager.java:602:in `getConnection': java.sql.SQLException: No suitable driver found for jdbc:db2j:db/OpcClientDb
(plus a stack trace)

So I am guessing that I need to load the classes in the jar files but I can't figure out how to do that. I've tried things like:

import "com.ibm.db2j"
or
include_class java::db2j
or
java_import java.lang.com.ibm.db2j

all of which don't seem to be even close to what I need.

If I do:

jar tf lib/db2j.jar | head -20

I get things like:

com/ibm/db2j/aggregates/AggregateDefinition.class
com/ibm/db2j/aggregates/Aggregator.class
com/ibm/db2j/authentication/UserAuthenticator.class
com/ibm/db2j/catalog/AliasInfo.class
com/ibm/db2j/catalog/DefaultInfo.class
com/ibm/db2j/catalog/DependableFinder.class
com/ibm/db2j/catalog/IndexDescriptor.class
com/ibm/db2j/catalog/ReferencedColumns.class
com/ibm/db2j/catalog/Statistics.class
com/ibm/db2j/catalog/TypeDescriptor.class
com/ibm/db2j/core/BootStrap.class

How do I figure out what to pass to import or java_import or include?

Thank you for your time,
Perry


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Manuel Ramos

unread,
May 16, 2013, 10:19:35 PM5/16/13
to us...@jruby.codehaus.org

Are these java classes view by the jre thats runs Jruby? Sounds like a classpath problem...

Karol Bucek

unread,
May 17, 2013, 1:06:02 AM5/17/13
to us...@jruby.codehaus.org

 Hi Perry, you need to load the driver class (right) before connecting using DriverManager.

Assuming the class name is right do smt like: Java::JavaClass.for_name 'com.ibm.db2j.jdbc.DB2jDriver'
Make sure you do this after the .jar files are required, yet before connecting ...

Hope it helps, K.

Perry Smith

unread,
May 17, 2013, 9:13:05 AM5/17/13
to us...@jruby.codehaus.org
Weee!!!

Thank you so much.  I now get:

WARNING: Cloudscape (instance c013800d-013e-b298-9d99-00c0a801c300) is attempting to boot the database /Users/pedzan/Source/OPCstuff/ruby-stuff/db/OpcClientDb even though Cloudscape (instance c013800d-013d-b6e5-23bc-0009318f4f00) may still be active.  Only one instance of Cloudscape should boot a database at a time. Severe and non-recoverable corruption can result and may have already occurred.

But I am pretty sure that is because they create file locks and I have a snapshot of the DB while its open.  

Now, I just need to figure out how to dump what I need but thats probably a topic for another list.

By the way (for the next guy who might follow).  I did:

jar tf lib/db2j.jar | grep Driver

to get the exact name (which Karol had predicted correctly) -- and then changed the slashes into dots.

The thing I don't 100% understand is why not just do this for all the classes when the jar file is loaded?  Would that be a huge performance hit or something?

Thank you very much!
Perry

Karol Bucek

unread,
May 17, 2013, 9:54:14 AM5/17/13
to us...@jruby.codehaus.org

please note that `jar | grep` only works if the driver author named their java.sql.Driver implementation with "Driver" in the class name.

since you mentioned 0% Java knowledge you can not expect to 100% understand this :) - it's a Java (actually JDBC) thing ...

scanning (and thus loading) all .jar classes for driver implementations (which might have arbitrary names as mentioned) would certainly be a performance hit - there's a Java auto-discovery convention (META-INF/java.sql.Driver text file inside the .jar) that gets around this it's likely that your driver does not conform with it thus the manual loading.

enjoy Java with JRuby - it's certainly more enjoyable this way !

Thomas E Enebo

unread,
May 17, 2013, 2:32:14 PM5/17/13
to us...@jruby.codehaus.org
On top of Karol's mentioning of performance reasons...There is no nice API for reflectively querying all classes loaded.  Jar loading possibly could be done via examining classpath (although classpath can change at runtime), but JVM can also dynamically generate classes on the fly (or even get loaded by JNI).  The ability to just auto-process all defined things is difficult even if you ignore the performance penalty of processing thousands of classes you probably will never consume.

-Tom

--
blog: http://blog.enebo.com       twitter: tom_enebo
mail: tom....@gmail.com

Charles Monteiro

unread,
May 24, 2013, 5:37:03 AM5/24/13
to us...@jruby.codehaus.org
The following does not directly help and its unsolicited but if you use Ruby Sequel which I am currently using via jdbc drivers to hook up to Oracle and Postgres all of your load issues should go away. Furthermore, the ruby sequel dsl is nice to work with but you can also just pass it raw sql or a combination of. Disclaimer I have not tried this out with Cloudscape but if there's a jdbc driver for it , my understanding is that it should work. May need to drop that jar under the jruby libs directory.

Check it out, you might be happier.

Charles

Perry Smith

unread,
May 24, 2013, 7:56:21 AM5/24/13
to us...@jruby.codehaus.org
Thanks. I will definitely check it out.
Reply all
Reply to author
Forward
0 new messages