Rails, PgBouncer and DigitalOcean — how to work with Databases Connection Pools? - ruby-on-rails

Recently in DigitalOcean has the opportunity to create Databases. This is something like a turnkey solution based on the usual Droplet with a bunch of additional database features.
One of these additional features — Connection Pools. As far as I understand this is a PgBouncer.
If I connect the database to the Rails 5.2 application in the usual way (directly), then everything is fine.
But if I use the connection through the "Connection Pools", I will get an error:
ActiveRecord::StatementInvalid: PG::DuplicatePstatement: ERROR: prepared statement "a1" already exists
This will occur when any data is received from the database.
Tell me, please, how to make it all work?

Set prepared_statements and advisory_locks to false in the database configurations.
More info here.

Related

SQL Server connection established during design, but not during runtime

I have created an Amazon RDS DB instance. I can connect to it and perform operations with SSMS. I can also bind to it using Entity Framework DB-first and generate my model. However, when I run my app, using the same connection string that was generated in the data access project, I get a "the network path was not found" error while trying to establish a connection to the DB.
Let me be clear: the db exists, the right ports are open, and the connection string is correct. I am the only one connecting to the database and the status is "available".
So what's going on? Has anyone experienced something like this?
Let me also further mention that I have already checked the usual things like internet connectivity, firewall rules, state of the database, etc.
well it started working all of a sudden. So I guess this problem will only pop up again in production or something.

Find out which DB Heroku is currently using

I recently had a short term project served off of Heroku that approached 10,000 Postgres records and needed to be upgraded from hobby-dev to hobby-basic. Being new to Heroku, I did my best in finding out how to do so and followed heroku's upgrade using PG Backup guide. With a bit of trial and error, I thought I got it working. I erred on the side of caution and didn't do the last deletion of the old DB step. However, I soon got a message from Heroku telling me I've passed the 10,000 record limit. I went to my Heroku dashboard and checked out the DB size. It doesn't look like the Hobby-Basic DB had anything stored in it. I waited a couple weeks and checked again, thinking it may have been a realtime updating issue. Still no tables in the new Hobby-Basic DB.
So my question is, is there a way for me to find out which DB my Heroku app is set up to use currently? How do I force it to switch to a different DB in my app?
This is the guide I followed to upgrade from Hobby-Dev to Basic: https://devcenter.heroku.com/articles/upgrade-heroku-postgres-with-pgbackups
Presumably your app is reading from the environment variable DATABASE_URL. If you have multiple database then you should also have other environment variables like HEROKU_POSTGRESQL_(color). You can access those environment variables from within your app to inspect the configured database information, and you can change them in the Heroku panel. If you change DATABASE_URL to one of the values of HEROKU_POSTGRESQL_(color), then your app should pick up on that and switch to the different database. You can also edit your configuration and access another environment variable while bypassing DATABASE_URL entirely.

SQL Server 2012 mirror in azure VM - on second failover app loses connectivity

We've got a mirrored SQL server 2012 database setup on Azure VM's - two servers plus a witness, all using client certificates, with SQL logins with the same SID set.
When testing our app from a different VM, everything works as expected when we manually failover the database, there's a one second wait and then it continues to operate quite happily.
If we then do another manual failover, ie moving the principal back to the original server, the app errors and throws a 'no such host in known' error. Recycling the app pool fixes the issue, but this clearly isn't workable in production when one of the servers is updated followed by the other at some later point (both are in an availability set).
The host not known error is somewhat baffling as it was communicating with it happily before the initial failover, and will again after the app pool recycle.
Here's the connection string as it is right now, after a lot of faffing around:
"Data Source=server1,1433;Failover Partner=server2,1433;Initial
Catalog=;MultipleActiveResultSets=True;User Id=user;
Password=password; Network=dbmssocn;Connect Timeout=60; async = true;"
providerName="System.Data.SqlClient"
The app is running on .net 4.5.2, so should be up to date with hotfixes, and we're out of ideas after much Googling with Bing.
I've just solved a problem that I had that looks very similar to your problem. I'd get the host not known error whenever the database switched from the first one listed in the web.config file to the failover one. It was fine switching from the failover to the primary.
The problem that I had was that I set up the database mirroring using server names but my web server did not know the database servers by name. Once I fixed this, I was able to get the failover working smoothly both ways.
This is what I think was happening:
I set up the mirroring using the names SQL1 and SQL2 as the principal and mirror servers
I have their ip addresses in my connection string: 10.1.1.5 and 10.1.1.6
The application tries to get to the first server 10.1.1.5 and succeeds and is then told that the mirror server is SQL2
SQL1 goes down and the database is successfully switched to the mirror server.
The web application attempts to connect, fails and determines that it should try the second server.
It tries to connect to SQL2, which it doesn't know, and fails with the message that the host is unknown.
This answer would only apply to your situation if you actually put ip addresses in your web.config and that server1,1433 and server2,1433 were actually masking place-holders for the ip addresses that you actually used.
I haven't really solved the naming issue though. I just added the two database server names to the HOSTS file which isn't an acceptable situation but does prove my theory on what my problem was.
I am researching a setup just like you have and upon reading this and the response by Steve Kaye, I'm wondering if you have SQL browser running. Take a look at this article for how SQL browser comes into play:
http://blogs.msdn.com/b/spike/archive/2010/12/15/running-a-database-mirror-setup-with-the-sqlbrowser-service-off-may-produce-unexpected-results.aspx

Is it possible to view a remote database in heroku on my computer (using Induction)?

In my rails 4 application directory, I typed "heroku pg:credentials DATABASE" into terminal to get all the information about the database for my application which is deployed on heroku. Since I'd like to view the data inside my postgresql database, I tried inputting the information into Induction, but it ended up not responding and I was forced to enter the activity monitor and force quit. I followed the same procedure several more times all with the same result. Is my version of Induction faulty? Should I be using a different program to view my database? Or am I doing something wrong?
I'm new to rails, so thanks for your help!
Yes, you can. Heroku Postgres allows connections from outside Heroku's network. The credentials displayed from heroku pg:credentials will be all you need and should work. Maybe try using the official PGAdmin tool instead?

Connecting directly to Database in Rails (Heroku?)

I am currently writing RoR applications and deploying using Heroku.
Is there any way to connect directly to the DB using a connection string?
I guess what I am asking here is can I connect directly to the DB, is there a connection string, how can I get the connection string, etc. I want to be able to perform querys on the DB outside of the terminal I am developing. My current solution is using
heroku db:pull 'anotherPOSTGRESQLdatabasesCONNECTIONstring'
and then performing queries on that database, but this is not a valid solution, because I am developing this application for users who are not code savvy, and they should be able to perform queries on the database without me or them using the terminal.
I think what you might be asking is available here: https://postgres.heroku.com/
Click "Try For Free", Select the option where you already have a heroku account, login with the correct credentials.
Now you can choose the database you want to perform actions on, and create "Data Clips" that your clients can run to get data reports etc (data clips can be used to run arbitrary select SQL commands).

Resources