activerecord-sqlserver-adapter icon indicating copy to clipboard operation
activerecord-sqlserver-adapter copied to clipboard

Still problems with ActiveRecord::ConnectionNotEstablished (TinyTds::Error: failed to execute statement)

Open marino-mrc opened this issue 4 years ago • 4 comments

Issue

Hi, here is a description of my issue:

2.7.0 :001 > Pratica.count
  SQL (17.4ms)  USE [WinLEG]
   (18.0ms)  SELECT COUNT(*) FROM [Pratiche]
 => 10854 

... Waiting 30seconds here, then

2.7.0 :002 > Pratica.count
/home/developer/.rvm/gems/ruby-2.7.0/gems/activerecord-sqlserver-adapter-6.1.2.1/lib/active_record/connection_adapters/sqlserver/database_statements.rb:461: warning: TinyTds: dbsqlsend() returned FAIL.

   (1.9ms)  SELECT COUNT(*) FROM [Pratiche]
Traceback (most recent call last):
        1: from (irb):2
ActiveRecord::ConnectionNotEstablished (TinyTds::Error: failed to execute statement)
2.7.0 :003 > Pratica.count
   (0.4ms)  SELECT COUNT(*) FROM [Pratiche]
Traceback (most recent call last):
        2: from (irb):2
        1: from (irb):3:in `rescue in irb_binding'
ActiveRecord::StatementInvalid (TinyTds::Error: DBPROCESS is dead or not enabled)
2.7.0 :004 > Pratica.connection
 => #<ActiveRecord::ConnectionAdapters::SQLServerAdapter version: 6.1.2.1, mode: dblib, azure: false>
...
2.7.0 :009 > Pratica.connection.reconnect!
  SQL (17.8ms)  USE [WinLEG]
 => -1 
2.7.0 :010 > Pratica.count
   (19.1ms)  SELECT COUNT(*) FROM [Pratiche]
 => 10854 
2.7.0 :011 >

Expected behavior

It should reconnect automatically. Is there a way to configure this? I checked the code and it seems that "reconnect" option is not used. This case seems related to https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues/623 but I don't see a solution. Is there a way to force automatic reconnection?

How to reproduce

app/model/pratica.rb:

class Pratica < ActiveRecord::Base
        self.abstract_class
        self.table_name = 'Pratiche'
        self.primary_key = 'pra_ID'
end

configu/database.yml:

default: &default
  adapter: sqlserver
  encoding: utf8
  username: xxxxxxxx
  password: xxxxxxxxxx
  host: X.X.X.X

Details

  • Rails version: 6.1.4.4

  • SQL Server adapter version: 6.1.2.1

  • TinyTDS version: 2.1.5

  • FreeTDS details:

    run `tsql -C` and paste here the output.
    
$ tsql -C
Compile-time settings (established with the "configure" script)
                            Version: freetds v1.3.3
             freetds.conf directory: /etc
     MS db-lib source compatibility: yes
        Sybase binary compatibility: yes
                      Thread safety: yes
                      iconv library: yes
                        TDS version: auto
                              iODBC: no
                           unixodbc: yes
              SSPI "trusted" logins: no
                           Kerberos: yes
                            OpenSSL: no
                             GnuTLS: yes
                               MARS: yes

marino-mrc avatar Dec 29 '21 12:12 marino-mrc

I'm unable to recreate the issue. The issue could be with your SQL Server database.

Could you create a test-case using the script in https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/wiki/How-to-report-a-bug#minimal-reproducible-script and attach it to this issue?

aidanharan avatar Jan 13 '22 14:01 aidanharan

Hi. I have a few questions for you:

  1. What happened between you first ran Pratica.count and the second time you tried to execute the same query? Was the database restarted or something like that?
  2. Unrelated, but why do you call self.abstract_class in your class? That should probably be removed.

wpolicarpo avatar Jan 13 '22 16:01 wpolicarpo

Hi Guys, thanks for your help.

@wpolicarpo:

  1. Nothing happened to the DB during my test. No restarts or something like that
  2. Yes, you're right. I removed the abstract_class statement but I got the same error

@aidanharan:

Unfortunately, I have only read permissions on the database and it seems to me that the script you linked needs to create and drop a table. Is there something else I can do?

As a side note, I can say that I have the issue only from the rails console. My application actually works, probably because the connection is re-established automatically when I use the web app.

Regards, Marco

marino-mrc avatar Jan 13 '22 18:01 marino-mrc

Hi Marco,

Details of how to run a SQL Server instance using Docker is given in https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/wiki/How-to-run-SQL-Server-in-a-Docker-container. You could use that database instance to create a test case against.

aidanharan avatar Jan 18 '22 10:01 aidanharan