Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
marino-mrc opened this issue Dec 29, 2021 · 4 comments

Comments

@marino-mrc
Copy link

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 #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
@aidanharan
Copy link
Contributor

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?

@wpolicarpo
Copy link
Member

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.

@marino-mrc
Copy link
Author

marino-mrc commented Jan 13, 2022

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

@aidanharan
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants