arton / rjb

Ruby Java Bridge
https://www.artonx.org/collabo/backyard/?RubyJavaBridge
GNU Lesser General Public License v2.1
117 stars 34 forks source link

RJB is not working for the second request. #81

Closed uyphu closed 7 months ago

uyphu commented 3 years ago

I am using RJB::Load in Rails App, for the first request api RJB load jar file and process correctly. However from the second request of that api, it looks like jars file not found and failed. Specification: I am writing an application running Rails and connect DB2 so I need to load a jar file jt400.jar to provider the proper driver. In controller /api/v1/test def index begin Rjb::load("#{Dir.pwd}/lib/support/jt400.jar", ['-Djdbc.drivers=com.ibm.as400.access.AS400JDBCDriver']) driver = Rjb::import('java.sql.DriverManager') puts "driver: #{driver}" connection = driver.getConnection("jdbc:as400://phu.test.com;libraries=MACS, MACSF, QGPL, SYSIBM","test", "test") puts "connection #{connection}" statement = connection.prepareStatement("Select DNNAME as AUCTNAME from test.pfdiv") result_set = statement.executeQuery while(result_set.next()) auctionName = result_set.getObject(1).toString puts "auctionName: #{auctionName}" end connection.close rescue => exception puts exception end render json: { status: "SUCCESS", count_available: "1", criteria: "hellow world", }, status: :ok end

When I call api/vi/test for the first time, it is working well but when I request the second time, it returns error "No suitable driver found for jdbc:as400://phu.test.com;libraries=MACS, MACSF, QGPL, SYSIBM"

If you have any ideas, please help me. Even I tried to use RjbLoader to load jar file while starting application and then reuse driver = Rjb::import('java.sql.DriverManager') but still cannot.

Thanks, Phu

arton commented 2 years ago

Hi uyphu, sorry for the late response.

I think that you should call Rjb::load just one time in the process's life time.

For example: config/initializer/99_load_rjb.rb => You may loads Rjb and jars, also calls Rjb::import and assing it in the module constant.

pseudo code:

module Jt400Rjb 
  Rjb::load ...
  driver = Rjb::import ...
  Connection = driver...
end

In your contollers or models, you can write such as

  statement = Jt400Rjb::Connection.prepareStatement...

I hope it helps.