how to set Seamless database pool read slave database as default? - ruby-on-rails

I have problem with seamless gem
development:
adapter: jdbcmysql
database: mydb_development
username: read_user
password: abc123
pool_adapter: jdbcmysql
port: 3306
master:
host: master-db.example.com
port: 6000
username: master_user
password: 567pass
read_pool:
- host: read-db-1.example.com
pool_weight: 2
- host: read-db-2.example.com
it should read for slave right [read-db-1.example.com] ? but it was weird.. it always read to master database [mydb_development] .
do you have any suggestion, how should i do to configure this gem for default read to slave database?
Thank you

Specify pool_weight=0 in the master configuration
By default, the master connection will be included in the read pool. If you would like to dedicate this connection only for write operations, you should set the pool weight to zero.
seam_leass_database_pool plugin

Related

Rails CQL cannot connect to AWS Keyspaces (AWS Cassandra)

I am trying to connect from a Ruby on Rails application to AWS Keyspaces (AWS Cassandra), but I cannot manage to do it. I use the cequel gem and generated the config/cequel.yml which contains a similar thing to the following:
development:
host: "CONTACT_POINT"
username: "USER"
password: "PASS"
port: 9142
keyspace: key_development
max_retries: 3
retry_delay: 0.5
newrelic: true
ssl: true
server_cert: 'config/certs/AmazonRootCA1.pem'
replication:
class: NetworkTopologyStrategy
datacenter1: 3
datacenter2: 2
durable_writes: false
(Credentials where used in another app and they work which is working as expected.)
when I try to run:
rake cequel:keyspace:create
I get the following errors:
Cassandra::Errors::NoHostsAvailable: All attempted hosts failed: x.xxx.xxx.xxx (Cassandra::Errors::ServerError: Internal Server Error)
Set the dc to us-east-1 . drop the replication definition.

Postgree too many connections in rails console

I am developing a Ruby on Rails app using postgre gem and this is how my database.yml looks like:
development:
adapter: postgresql
encoding: utf-8
pool: 5
username: "hytxlzju"
password: "xxxxx"
host: "jumbo.db.elephantsql.com"
port: "5432"
database: "hytxlzju"
production:
adapter: postgresql
encoding: utf-8
pool: 5
username: "hytxlzju"
password: "xxxxxx"
host: "jumbo.db.elephantsql.com"
port: "5432"
database: "hytxlzju"
Whenever I am connecting to this db locally, from the rails console I am getting too many connections. How can I kill a connection in the code, once the user logged out, in the code, and how can I kill one in my rails console, after I finished altering the tables?
[EDIT]
This is the error message:
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-3.2.22.5/lib/active_record/connection_adapters/postgresql_adapter.rb:12
22:in `initialize': FATAL: too many connections for role "hytxlzju" (PG::ConnectionBad)
[EDIT] I added my initilizer, still no success:
Rails.application.config.after_initialize do
ActiveRecord::Base.connection_pool.disconnect!
ActiveSupport.on_load(:active_record) do
config = ActiveRecord::Base.configurations[Rails.env] ||
Rails.application.config.database_configuration[Rails.env]
config['pool'] = ENV['DB_POOL'] || ENV['RAILS_MAX_THREADS'] || 5
ActiveRecord::Base.establish_connection(config)
end
end
You can try the below approach
Active Record limits the total number of connections per application
through a database setting pool; this is the maximum size of the
connections your app can have to the database
in config/datbase.yml
pool: <%= ENV['RAILS_MAX_THREADS'] || 5 %>
If you are using puma then use ENV['RAILS_MAX_THREADS'] more here
It might solve the problem.
[SOLVED]
Somehow my demo app was not finding the entries in the tables, so it was creating multiple pools connections, without closing them, because before closing the connection, a 500 error was getting thrown, hence that bit of code where I closed the pool was never closed. More abut postgre session here
https://devcenter.heroku.com/articles/concurrency-and-database-connections#connection-pool

What are all possible keys for database.yml

I've just discovered that the reconnect: true configuration option is possible in the database.yml file.
What other possible configuration options are there? Is there a complete reference for all options?
Known key examples:
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: foo
password: bar
reconnect: true
socket: /var/sock/thing.sock
development:
<<: *default
database: app_development
I don't think there is any place that just lists them but I checked the ActiveRecord's ConnectionAdapaters. Keep in mind that options change which database you use, but this was listed within the MySQL connection adapter.
List of Options for MySQL
:host - Defaults to "localhost".
:port - Defaults to 3306.
:socket - Defaults to "/tmp/mysql.sock".
:username - Defaults to "root"
:password - Defaults to nothing.
:database - The name of the database. No default, must be provided.
:encoding - (Optional) Sets the client encoding by executing "SET NAMES <encoding>" after connection.
:reconnect - Defaults to false (See MySQL documentation: http://dev.mysql.com/doc/refman/5.7/en/auto-reconnect.html).
:strict - Defaults to true. Enable STRICT_ALL_TABLES. (See MySQL documentation: http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html)
:variables - (Optional) A hash session variables to send as SET ##SESSION.key = value on each database connection. Use the value +:default+ to set a variable to its DEFAULT value. (See MySQL documentation: http://dev.mysql.com/doc/refman/5.7/en/set-statement.html).
:sslca - Necessary to use MySQL with an SSL connection.
:sslkey - Necessary to use MySQL with an SSL connection.
:sslcert - Necessary to use MySQL with an SSL connection.
:sslcapath - Necessary to use MySQL with an SSL connection.
:sslcipher - Necessary to use MySQL with an SSL connection.
The github for Rails ActiveRecord adapters, https://github.com/rails/rails/tree/master/activerecord/lib/active_record/connection_adapters
edit:
Adding what #pjrebsch commented below. You can also see MySQL options on the Mysql2 gem's Readme

How to properly connect ROR with Oracle database with Ruby-OCI8 gem?

I'm trying to use Oracle Database Xe on my ruby on rails app
but I'm having a lot of trouble with my database connection I'm currently not sure what the problem is, but according to what I have read I may have a problem with my TNS setup the error message that I'am having is
OCIError: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
The error appears every time I try to run rake db:migrate
on my rails console I try to run OCI8.new and it gives me this error
OCIError: ORA-12545: Connect failed because target host or object does not exist
I'm pretty much stuck and I'm really not sure what to do here.
TNS:
METRO=
(description=
(address_list=
(address = (protocol = TCP)(host = 127.0.0.1)(port = 1521))
)
(connect_data =
(service_name=METRO)
)
)
Database.yml :
development:
adapter: oracle_enhanced
database: metro
host: 192.168.18.55
username: metro
password: imperium
Looks like you are missing the port in database.yml file.
development:
adapter: oracle_enhanced
host: localhost
port: 1521
database: xe
username: user
password: secret

How to set up database.yml

I am trying to set up ruby on rails to develop locally on a 64 bit windows 7 machine using SQL server 2005. We have an existing database and it does not conform to the ruby way so I created a table to test conectivity.
Unable to find information on properly configuring the database.yml file I suspect my problem is there. I have both 32-bit and 64-bit DSNs defined. When I run the console I get "Table doesn't exist"
Here's my database.yml. I don't know
default: &default
host: 111.222.333.444
adapter: sqlserver
mode: odbc
dsn: DSN_Name
database: ERP
username: sa
password: ********
development:
<<: *default
host: 111.222.333.444
adapter: sqlserver
mode: odbc
dsn: DSN_Name
database: ERP
username: sa
password: ********
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
Does anyone know how the default works? I can't find any information on it. Specifically "<<: *default"
I updated my database.yml file. Created a table for testing; used the plural and ID for the primary key:
CREATE TABLE [dbo].[Cars](
[ID] [int] NULL,
[Name] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
Created a model
class Car < ActiveRecord::Base
end
But I get "Table doesn't exist" after connecting to Car and entering 'Car'. Anyone have any idea what the next step would be to debug this?
The &default in the line default: &default, means make the key/value pairs under this YAML namespace available as the variable *default.
The <<: *default lines are taking those key/value pairs, and making them part of the other groups, so you only need to change values that are different from the default: group.
If all your databases are on the same machine, then your database.yml should look a lot like this:
default: &default
host: 111.222.333.444
adapter: sqlserver
mode: odbc
dsn: DSN_Name
username: sa
password: ********
development:
<<: *default
database: ERP_development
test:
<<: *default
database: ERP_test
production:
<<: *default
database: ERP_production
Since you are on sqlserver, you'll probably have to create all those databases yourself (instead of letting Rails do it for you).
from: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter
Force Schema To Lowercase
Although it is not necessary, the Ruby convention is to use lowercase
method names. If your database schema is in upper or mixed case, we
can force all table and column names during the schema reflection
process to be lowercase. Add this to your config/initializers file for
the adapter.
ActiveRecord::ConnectionAdapters::SQLServerAdapter.lowercase_schema_reflection = true
Schemas & Users
Depending on your user and schema setup, it may be needed to use a
table name prefix of dbo.. So something like this in your initializer
file for ActiveRecord or the adapter.
ActiveRecord::Base.table_name_prefix = 'dbo.'

Resources