Friday, March 09, 2007

Ruby/Rails/ActiveRecord/Oracle : TNS-12560 Protocol Adapter Error

Ok, this is just stupid enough of me that I'm blogging it mostly so that when I do it again in a month I'll be able to find the answer.

90% of the time with my Ruby code I point it to my local OracleExpress instance, which I tell ActiveRecord about thusly:

  :adapter=>'oci', :database=>'xe', :username=>'foo', :password=>'bar'

and everything is ducky.  Today, though, for the first time in months I had to point to a new schema on the common development machine at work, so I tried:
 

:adapter=>'oci', :database=>'dev_common', :username=>'foo', :password=>'bar'

And for most of the afternoon now I've gotten the dreaded 12560 Protocol Adapter Error message.  My tnsnames.ora file is in order.  I can tnsping the server.  I can fire up sqlplus and hit the server.  Heck, I can even run this little tasty bit of code I picked up directly from Oracle's Ruby/Rails FAQ:

ruby -r oci8 -e "OCI8.new('foo', 'bar', 'dev_common').exec('select * from user_table') do |r| puts r.join('|'); end"

and have it work (insert your own SQL, of course).

But when I fire up irb, require 'active_record' and try to do this:

ActiveRecord::Base.establish_connection :adapter=>'oci', :database=>'dev_common', :username=>'foo', :password=>'bar'

I'm back to square one.

Until I figured it out.  Know what the problem is, apparently?  I'm saying "database" when I should be saying "host".  Try this:

ActiveRecord::Base.establish_connection :adapter=>'oci', :host=>'dev_common', :username=>'foo', :password=>'bar'

Now everything works fine. Go figure!  When I'm connecting to XE I get the opposite problem - I have to say :database=>'xe'.  I'm not fully clear on the different between what's a host and what's a database, cuz it's sure not obvious since they both contain the same value.  Maybe somebody can explain it to me.

AddThis Social Bookmark Button

1 comment:

Wham my testes said...

Host is the name of the machine (think IP address) hosting the database
.
You can have many Databases on one host.