How to connect to SQLite3 database in JRuby?

77 views Asked by At

I am trying to connect to an SQLite3 database in Puppet 7. I used the same functionality in Puppet 5, but now I need to use JRuby jdbc-sqlite3-3.28.0.

require 'jdbc/sqlite3'

Jdbc::SQLite3.load_driver
java.sql.DriverManager.registerDriver(org.sqlite.JDBC)

database_path = "/infra-db/sqlite.db"
connection = java.sql.DriverManager.getConnection("jdbc:sqlite:#{database_path}")
# Traceback (most recent call last):
#        16: from RUBY.<main>(uri:classloader:/META-INF/jruby.home/lib/ruby/gems/shared/gems/irb-1.0.0/exe/irb:11)
#        15: from org.jruby.RubyKernel$INVOKER$s$rbCatch.call(org/jruby/RubyKernel$INVOKER$s$rbCatch.gen)
#        14: from org.jruby.RubyKernel.catch(org/jruby/RubyKernel.java:1237)
#        13: from org.jruby.RubyKernel$INVOKER$s$rbCatch.call(org/jruby/RubyKernel$INVOKER$s$rbCatch.gen)
#        12: from org.jruby.RubyKernel.catch(org/jruby/RubyKernel.java:1237)
#        11: from org.jruby.RubyKernel$INVOKER$s$0$0$loop.call(org/jruby/RubyKernel$INVOKER$s$0$0$loop.gen)
#        10: from org.jruby.RubyKernel.loop(org/jruby/RubyKernel.java:1507)
#         9: from org.jruby.RubyKernel$INVOKER$s$0$3$eval.call(org/jruby/RubyKernel$INVOKER$s$0$3$eval.gen)
#         8: from org.jruby.RubyKernel.eval(org/jruby/RubyKernel.java:1091)
#         7: from org.jruby.RubyKernel.evalCommon(org/jruby/RubyKernel.java:1129)
#         6: from RUBY.evaluate((irb):5)
#         5: from org.jruby.javasupport.JavaMethod.invokeStaticDirect(org/jruby/javasupport/JavaMethod.java:369)
#         4: from org.jruby.javasupport.JavaMethod.invokeDirectWithExceptionHandling(org/jruby/javasupport/JavaMethod.java:457)
#         3: from java.lang.reflect.Method.invoke(java/lang/reflect/Method.java:498)
#         2: from java.sql.DriverManager.getConnection(java/sql/DriverManager.java:270)
#         1: from java.sql.DriverManager.getConnection(java/sql/DriverManager.java:689)
# Java::JavaSql::SQLException (No suitable driver found for jdbc:sqlite:/infra-db/sqlite.db)

If I establish connection in Activerecord, it will fail:

require 'active_record'
ActiveRecord::Base.establish_connection(adapter: 'jdbc', driver: 'org.sqlite.JDBC', url: 'jdbc:sqlite:/infra-db/sqlite.db')
# => #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x50841932 @connection_klass=ActiveRecord::Base, @connections=[], @thread_cached_conns=#<Concurrent::Map:0x68fa8ea5 entries=0 default_proc=nil>, @checkout_timeout=5.0, @mon_data_owner_object_id=4024, @now_connecting=0, @pool_config=#<ActiveRecord::ConnectionAdapters::PoolConfig:0x79982bcc @connection_klass=ActiveRecord::Base, @db_config=#<ActiveRecord::DatabaseConfigurations::UrlConfig:0x68c47cf9 @url="jdbc:sqlite:/infra-db/sqlite.db", @name="primary", @env_name="default_env", @configuration_hash={:adapter=>"jdbc", :driver=>"org.sqlite.JDBC", :url=>"jdbc:sqlite:/infra-db/sqlite.db"}>, @_mutex=#<Thread::Mutex:0x7e764e5c>, @pool=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x50841932 ...>>, @automatic_reconnect=true, @threads_blocking_new_connections=0, @mon_data=#<Monitor:0x42457891>, @available=#<ActiveRecord::ConnectionAdapters::ConnectionPool::ConnectionLeasingQueue:0x92de794 @lock=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x50841932 ...>, @queue=[], @cond=#<MonitorMixin::ConditionVariable:0x5bed0ff4 @monitor=#<Monitor:0x42457891>, @cond=#<Thread::ConditionVariable:0x17a5ae7b>>, @num_waiting=0>, @idle_timeout=300.0, @db_config=#<ActiveRecord::DatabaseConfigurations::UrlConfig:0x68c47cf9 @url="jdbc:sqlite:/infra-db/sqlite.db", @name="primary", @env_name="default_env", @configuration_hash={:adapter=>"jdbc", :driver=>"org.sqlite.JDBC", :url=>"jdbc:sqlite:/infra-db/sqlite.db"}>, @query_cache_enabled=#<Concurrent::Map:0x7e4dddee entries=0 default_proc=#<Proc:0x183a84f4@/opt/puppetlabs/server/data/puppetserver/jruby-gems/gems/activerecord-6.1.7.3/lib/active_record/connection_adapters/abstract/query_cache.rb:32>>, @size=5, @reaper=#<ActiveRecord::ConnectionAdapters::ConnectionPool::Reaper:0x2b9bbaa8 @pool=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x50841932 ...>, @frequency=60.0>, @lock_thread=false>
irb(main):015:0> ActiveRecord::Base.connected?
=> false

How to connect to SQLite3 database in JRuby?

0

There are 0 answers