2016-09-19 124 views
1

My Rails 4.2.1應用必須連接到Microsoft SQL 2008 R2數據庫。我正在使用tiny_tds寶石版本1.0.4。 FreeTDS v1.00.15安裝在運行Ubuntu 14.04的生產服務器上。從Rails應用連接到Windows SQL Server 2008 R2

我在每個循環內運行查詢,並且無法完成循環,進程在完成之前崩潰。 我試着玩tiny_tds選項沒有成功。

下面是我使用來獲取tiny_tds客戶端的代碼(檢查tds_versiontimeout選項):

client = TinyTds::Client.new(username: db_conf['username'], password: db_conf['password'], host: db_conf['host'], port: db_conf['port'], database: db_conf['database'], tds_version: '7.3', timeout: 15000, appname: 'ERP') 

這裏的freetds的日誌這樣的錯誤發生之後。

packet.c:741:Sending packet 0000 12 01 00 ce 00 00 00 00-16 03 01 00 86 10 00 00 |........ ........| 0010 82 00 80 6e d9 e2 dc 97-9d 77 59 9a 5b da e3 e2 |...n.... .wY.[...| 0020 8b aa 66 ed ec 5e e2 02-e5 6c fd db e1 ef 47 1a |..f..^.. .l....G.| 0030 9d 63 03 ed 6d 3e 28 3b-b9 64 fd 92 71 34 ff ba |.c..m>(; .d..q4..| 0040 7d 3c 8d ee 7b 34 75 e9-d5 b7 c6 83 a9 7d e6 7f |}<..{4u. .....}..| 0050 71 7e 25 11 82 b8 76 b1-c6 ba 86 b4 c3 0a 47 f0 |q~%...v. ......G.| 0060 51 96 c7 e2 5f ca 07 b2-95 53 b9 9e bb 2c e7 cb |Q..._... .S...,..| 0070 be 0a b5 eb b0 f3 41 1d-cd 86 fc a6 53 08 5e 56 |......A. ....S.^V| 0080 29 85 79 14 dc 2b 74 7b-b2 43 2c e8 0e 87 60 e4 |).y..+t{ .C,... .| 0090 10 ef f8 14 03 01 00 01-01 16 03 01 00 30 c7 f0 |........ .....0..| 00a0 35 f5 2c 6e 79 8d 85 b9-bd 60 b7 09 8c 7e 29 18 |5.,ny... . ...~).| 00b0 4a 56 ea c3 4e 13 bf e3-c5 8d f6 68 31 31 54 ee |JV..N... ...h11T.| 00c0 bf 2f 75 8d e9 9e c0 a9-d0 d2 9e 5b c9 92 |./u..... ...[..|

tls.c:105:in tds_pull_func_login query.c:3796:tds_disconnect() util.c:165:Changed query state from IDLE to DEAD util.c:322:tdserror(0x80b75e0, 0xa04ca80, 20017, 0) dblib.c:7947:dbperror(0xae62780, 20017, 0) dblib.c:8015:dbperror: Calling dblib_err_handler with msgno = 20017; msg->msgtext = "Unexpected EOF from the server (192.168.32.105:1433)" dblib.c:5777:dbgetuserdata(0xae62780) dblib.c:8037:dbperror: dblib_err_handler for msgno = 20017; msg->msgtext = "Unexpected EOF from the server (192.168.32.105:1433)" -- returns 2 (INT_CANCEL) util.c:352:tdserror: client library returned TDS_INT_CANCEL(2) util.c:375:tdserror: returning TDS_INT_CANCEL(2) util.c:375:tdserror: returning TDS_INT_CANCEL(2) tls.c:942:handshake failed login.c:530:login packet rejected util.c:322:tdserror(0x80b75e0, 0xa04ca80, 20002, 0) dblib.c:7947:dbperror(0xae62780, 20002, 0) dblib.c:8015:dbperror: Calling dblib_err_handler with msgno = 20002; msg->msgtext = "Adaptive Server connection failed"

而這裏的tsql -C輸出:

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

任何想法,我應該做些什麼來解決這些Unexpected EOF from the server錯誤?

+0

安裝什麼版本的freetds的嗎?你應該可以運行'tsql -C'來查看。如果你使用0.91,你需要使用'tds_version:'7.2''; 7.3要求0.95或更高。 – FlipperPA

+0

@FlipperPA看來我正在使用FreeTDS 0.91(從Ubuntu數據包管理器安裝)。使用'tds_version:'7.2'',我得到了與服務器錯誤相同的'意外的EOF'。 –

+0

使用'tds_version:'7.2''會讓你頭痛不已(特別是如果你處理Unicode的話),如果你使用的是0.91。但回到手頭的問題:你可以連接'tsql'嗎?例如,'TDSVER = 7.2 tsql -H 192.168.32.105 -p 1433 -U your_username'?如果這不起作用,你可以telnet 192.168.32.105 1433'並連接?另外,如果您的密碼中包含非字母字符,則可能需要嘗試使用少於20個字符的簡短(ish)字母數字密碼。老版本的FreeTDS在用戶名和密碼上的最大尺寸爲25字符(但我認爲它們增加了0.91)。 – FlipperPA

回答

0

看着SQL事件探查器,我發現Rails應用程序在MSSQL服務器上打開太多連接。達到最大開放連接數後,MSSQL服務器拒絕打開任何新連接,導致出現Unexpected EOF from the server錯誤。

要解決此問題,我必須在發送查詢時重新使用打開的連接,而不是爲每個查詢打開新的連接。我想這是反正使用tiny_tds連接器的正確方法。

翻譯成代碼:

def self.get_pmi_client 
    if @@pmi_client.nil? or [email protected]@pmi_client.active? 
    db_conf = Rails.configuration.database_configuration["pmi_#{Rails.env}"] 
    @@pmi_client = TinyTds::Client.new(username: db_conf['username'], password: db_conf['password'], host: db_conf['host'], port: db_conf['port'], database: db_conf['database']) 
    raise MSSQLConnectionError, t('erp.errors.pmi_connection_error') unless @@pmi_client.active? 
    end 
    return @@pmi_client 
end 
0

在你freetds的配置(通常在/etc/freetds/freetds.conf在您的配置),改變text size值:

text size = 4294967295 

這是最大值,IIRC。我相信FreeTDS 0.91,您的默認值可能是64512.

相關問題