ruby OCI8 slow initialization - ruby

I'm developing API for product based on Oracle database and I tried to use ruby-oci8 gem, but I faced a strange problem - OCI8.new is very slow, it takes 1,2-1,5 second to connect to database.
For example:
~ mmulev$ irb
2.1.1 :001 > require 'oci8'
=> true
2.1.1 :002 > def test
2.1.1 :003?> t0 = Time.now
2.1.1 :004?> OCI8.new('SCHEMA', 'STRONG_PASS', '//db_host:port/service_name')
2.1.1 :005?> Time.now - t0
2.1.1 :006?> end
=> :test
2.1.1 :007 > test
=> 1.217809
2.1.1 :008 >
The same thing in php (oci_new_connect) needs approximately 0,1-0,2 second to establish connection and return handler.
Is there any other solutions besides connection pooling?
P.S. ruby API code was profiled by method_profiler and by Benchmark, oracle v - 11g

You can also use this gem to connect to oracle
https://github.com/rsim/oracle-enhanced
Make sure that you have installed oracle properly you can install this by following link : How to install ruby-oci8?

Related

Why does require choose the latest version of a gem?

If I load a gem, let's say activerecord, in IRB require chooses the latest version of activerecord. Programmatically, what is influencing this decision to choose the latest gem version? Is require doing this, or is there something in the loaded IRB that forces requires to choose the latest version?
Here are my activerecord gems installed by bundler:
➜ ~ ls -al /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activere
activerecord-4.2.10/ activerecord-5.0.0.1/ activerecord-5.1.2/ activerecord-5.1.3/ activerecord-5.1.4/ activerecord-5.1.5/ activerecord-5.1.6/ activeresource-5.0.0/
And here is an example IRB session:
➜ ~ irb
2.4.1 :001 > require 'active_record'
=> true
2.4.1 :002 > Gem.loaded_specs['activerecord'].version
=> #<Gem::Version "5.1.6">
2.4.1 :003 >
If I load a gem, let's say activerecord, in IRB require chooses the latest version of activerecord.
Actually, it chooses the latest version that doesn't conflict with any already activated gem.
Programmatically, what is influencing this decision to choose the latest gem version? Is require doing this, or is there something in the loaded IRB that forces requires to choose the latest version?
This is requires job. More specifically, it is the job of the monkey-patched require from the RubyGems library, not the original require from the Ruby core library.
This is just simple separation of concerns: IRb is a REPL, not a package management system, it shouldn't know anything about packages.

Ruby 2.4 Enumerable#sum breaks in Rails 5.1

In Rails 5.1.4 using Ruby 2.3.5, I get this behavior:
>> [].sum
#> nil
I'd like to upgrade to Ruby 2.4, where Enumerable#sum is implemented natively. Testing this in IRB using Ruby 2.4.2, I get this result:
>> [].sum
#> 0
That's OK, and I can handle the different result. But going back to the Rails console in Rails 5.1.4 using Ruby 2.4.2, I get this:
>> [].sum
#> NoMethodError: undefined method `each' for nil:NilClass
However, in a newly created Rails 5.1.4 project, I don't get this error. What's going on here?
Looking at the source for the Active Support enumerable extensions it definitely seems like something odd is going on because you shouldn't have been getting the behaviour you described for Rails 5.1.4 using Ruby 2.3.5 i.e. you should have been getting 0 not nil there too.
Active Support's Array#sum checks if Ruby's own sum can be used by checking first.is_a?(Numeric). This will be false for an empty array so the call to super will call Enumerable#sum and both implementations of that have a default of 0.
Try [].method(:sum).source_location in the Rails console of your existing project to see if Array#sum is being overridden somewhere.
if that returns the expected line from active_support/c‌​ore_ext/enumerable.r‌​b then the next step will be to check [].method(:sum).super_method.source_location and see if a customised Enumerable#sum is the culprit.
I think that your application have some overhidden on sum method. Look the example below with 2 new applications using the 2 different versions of ruby
/testapp# ruby --version
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
/testapp# rails --version
Rails 5.1.4
/testapp# rails c
irb(main):001:0> [].sum
=> 0
and the other version
/testapp# ruby --version
ruby 2.3.5p376 (2017-09-14 revision 59905) [x86_64-linux]
/testapp# rails --version
Rails 5.1.4
/testapp# rails c
irb(main):001:0> [].sum
=> 0

Where is Rubinius' Actor API?

I got the following output from pry when trying out the example from http://rubini.us/doc/en/systems/concurrency/ with Rubinius 2.2.9:
2.1.0 (main):0 > RUBY_VERSION
=> "2.1.0"
2.1.0 (main):0 > RUBY_PATCHLEVEL
=> 0
2.1.0 (main):0 > RUBY_PLATFORM
=> "x86_64-darwin13.2.0"
2.1.0 (main):0 > RUBY_ENGINE
=> "rbx"
2.1.0 (main):0 > require 'actor'
LoadError: no such file to load -- actor
from kernel/common/code_loader.rb:441:in `load_error'
Is the documentation outdated?
It looks like the documentation is slightly outdated.
Commit 79bd4c30 appears to remove the Actor api from the Rubinius standard library and extract it into the rubinius-actor gem. As far as I can tell with a quick glance, it's the exact same API as the documentation describes.

number conversion - Fixnum / Bignum

I thought Ruby automatically converts to Bignum. I found confirmation here
However, it is not happening:
ruby 1.8.7 (358) [universal-darwin12.0]
>> 2 ** 62
=> 4611686018427387904
>> 2 ** 63
=> -9223372036854775808 #why minus - how about automatic Bignum conversion?
>> 2 ** 64
=> 0 #- how about automatic Bignum conversion?
Use a Newer Ruby Version
Ruby 1.8.7 is (in Internet terms) ancient. Use something more recent. For example:
[1] pry(main)> RUBY_VERSION
=> "2.0.0"
[2] pry(main)> 2 ** 63
=> 9223372036854775808
[3] pry(main)> 2 ** 64
=> 18446744073709551616
That is probably a bug in the old version of Ruby. Switch to a newer version, the problem is gone. Today is the release day for Ruby 2.0. Ruby 1.8 will be dead soon. On my Ruby 1.9.3, I just did 2**1000000 without any problem except that it goes on for a while, so I had to terminate it.
Most likely a bug specific to the build you're using. For example, when I do ruby -v I get:
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin12.2.1], MBARI 0x6770, Ruby Enterprise Edition 2011.03
...and in an irb session I get:
1.8.7 :006 > 2 ** 64
=> 18446744073709551616
1.8.7 :007 > (2 ** 64).class
=> Bignum
1.8.7 :008 > RUBY_VERSION
=> "1.8.7"
I also don't get this problem if I use newer versions. If you can post your output from ruby -v that would shine some light on the situation. For example Ruby REE vs. MRI vs. JRuby, etc.
Also, and this is just an opinion so take it for what it's worth, but I don't think Apple is very good about keeping their built-in version of Ruby updated, so just in case you're using the built-in version then consider moving to another build.

Can't convert Range in to Integer (Ruby on Rails)

I am using Ruby (1.9.3) and Rails (3.2.2). I have tasks file which contains a bunch of fake data to be populated to my database.
Here is some part of the task that I believe is causing the problem
#Create random Tender and populate the db
20.times do |n|
title = "#{Faker::Company.bs()} tender "
company_name = Faker::Company.name
opening_date=Time.at(rand * Time.now.to_i)
closing_date=Time.at(opening_date + ( 8*7*24*60*60)) #add 8 weeks to the deadline
bid_amount= rand(10000..100000)
description=Faker::Lorem.paragraph(sentence_count = 3)
Tender.create!(title: title,
company_name: company_name,
opening_date: opening_date,
closing_date: closing_date,
bid_amount: bid_amount ,
bid_amount: bid_amount ,
description: description )
end
It works fine with dev but only the above part is not executed on production database. I am using gem 'sqlite3', '1.3.5' on dev. and
gem 'pg', '0.12.2' on production (heroku)
When I run
git push heroku
$ heroku pg:reset SHARED_DATABASE --confirm myapp
$ heroku run rake db:migrate
$ heroku run rake db:populate
db:populate throws an error that says **can't covert Range to Integer.**
Any ideas what the problem might be?
EDIT: The data type of bid_amount is decimal
Your production ruby version is not 1.9.3. It is probably 1.8.7
$ ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]
$ irb
>> rand(10000..100000)
TypeError: can't convert Range into Integer
from (irb):1:in `rand'
from (irb):1
>> exit
$ rvm use 1.9.3
Using /Users/chirantan/.rvm/gems/ruby-1.9.3-p0
$ irb
1.9.3p0 :001 > rand(10000..100000)
=> 37036
Install ruby 1.9.3 on production and the rand method should work as expected.

Resources