TinyTds::Error: closed connection when trying to connect to a sql server named instance

1,560 views
Skip to first unread message

Tom Hoen

unread,
May 12, 2011, 9:15:44 AM5/12/11
to Rails SQLServer Adapter
I am trying to make two changes at the same time (never a good thing).
They are to move from using ODBC to TinyTDS and to go from ruby 1.87
to 1.9.2 (and rails 3.0, so perhaps 3 things).

I currently have a rails 2.3.5 application that uses sql server. I can
run this on my Mac Book (OSX 10.6.7), with freetds (version 0.82) with
the following configuration:

Version: freetds v0.82
freetds.conf directory: /usr/local/Cellar/freetds/0.82/
etc
MS db-lib source compatibility: yes
Sybase binary compatibility: no
Thread safety: yes
iconv library: yes
TDS version: 8.0
iODBC: no
unixodbc: yes

My freetds.conf file points to SQL Server instance running on VMWare
using the IP address of the VMware instance:

[MACBOOKPRO_MSSQL]
host = 172.16.27.10
instance = MACBOOKPRO
tds version = 8.0

I have an odbc.ini entry that uses the freetds.conf entry as follows:
[IAN_DEV]
Driver = /usr/local/lib/libtdsodbc.so
Description = ODBC connection via FreeTDS
Trace = Yes
TraceFile = /Users/user/isql.out
ServerName = MACBOOKPRO_MSSQL
Database = ian_dev

with a database.yml entry that uses the dsn:
development:
adapter: sqlserver
mode: ODBC
dsn: IAN_DEV
username: user
password: password

All of this works fine.

Now, in trying to build a new app, in rails 3.0, with Ruby 1.9.2 and
TinyTDS, I have done the following:
* Set up rvm and ruby 1.9.2
* Installed Rails 3.0
* Created a new rails app
* Added necessary gems to my gem file (including TinyTDS)
* Ran bundle install (TinyTDS installed without error)

Before making any other changes, I started console and tried the
following statement (after some trial and error)
client = TinyTds::Client.new(:username => 'user', :password =>
'password', :dataserver => 'WIN-MR6B172QT2R\MACBOOKPRO', :adapter =>
'sqlserver', :database => 'ISAAC')

This returned:
=> #<TinyTds::Client:0x00000100f69180>

But when I try to run a statement:
result = client.execute("select * from users")

I get: TinyTds::Error: closed connection

If I check the connection:
client.dead?

I get a segmentation fault:
(irb):9: [BUG] Segmentation fault
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.6.0]

-- control frame ----------
c:0029 p:---- s:0103 b:0103 l:000102 d:000102 CFUNC :dead?
c:0028 p:0011 s:0100 b:0100 l:000638 d:000099 EVAL (irb):9
c:0027 p:---- s:0098 b:0098 l:000097 d:000097 FINISH
c:0026 p:---- s:0096 b:0096 l:000095 d:000095 CFUNC :eval
c:0025 p:0028 s:0089 b:0089 l:000088 d:000088 METHOD /Users/user/.rvm/
rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb/workspace.rb:80
c:0024 p:0033 s:0082 b:0081 l:000080 d:000080 METHOD /Users/user/.rvm/
rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb/context.rb:254
c:0023 p:0031 s:0076 b:0076 l:001008 d:000075 BLOCK /Users/user/.rvm/
rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb.rb:159
c:0022 p:0042 s:0068 b:0068 l:000067 d:000067 METHOD /Users/user/.rvm/
rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb.rb:273
c:0021 p:0011 s:0063 b:0063 l:001008 d:000062 BLOCK /Users/user/.rvm/
rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb.rb:156
c:0020 p:0144 s:0059 b:0059 l:000042 d:000058 BLOCK /Users/user/.rvm/
rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb/ruby-lex.rb:243
c:0019 p:---- s:0056 b:0056 l:000055 d:000055 FINISH
c:0018 p:---- s:0054 b:0054 l:000053 d:000053 CFUNC :loop
c:0017 p:0009 s:0051 b:0051 l:000042 d:000050 BLOCK /Users/user/.rvm/
rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb/ruby-lex.rb:229
c:0016 p:---- s:0049 b:0049 l:000048 d:000048 FINISH
c:0015 p:---- s:0047 b:0047 l:000046 d:000046 CFUNC :catch
c:0014 p:0023 s:0043 b:0043 l:000042 d:000042 METHOD /Users/user/.rvm/
rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb/ruby-lex.rb:228
c:0013 p:0046 s:0040 b:0040 l:001008 d:001008 METHOD /Users/user/.rvm/
rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb.rb:155
c:0012 p:0011 s:0037 b:0037 l:000fe8 d:000036 BLOCK /Users/user/.rvm/
rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb.rb:70
c:0011 p:---- s:0035 b:0035 l:000034 d:000034 FINISH
c:0010 p:---- s:0033 b:0033 l:000032 d:000032 CFUNC :catch
c:0009 p:0183 s:0029 b:0029 l:000fe8 d:000fe8 METHOD /Users/user/.rvm/
rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb.rb:69
c:0008 p:0210 s:0024 b:0024 l:002310 d:002310 METHOD /Users/user/.rvm/
gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/console.rb:
44
c:0007 p:0019 s:0020 b:0020 l:000019 d:000019 METHOD /Users/user/.rvm/
gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/console.rb:
8
c:0006 p:0430 s:0016 b:0016 l:000015 d:000015 TOP /Users/user/.rvm/
gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands.rb:23
c:0005 p:---- s:0012 b:0012 l:000011 d:000011 FINISH
c:0004 p:---- s:0010 b:0010 l:000009 d:000009 CFUNC :require
c:0003 p:0061 s:0006 b:0006 l:0014e8 d:000a88 EVAL script/rails:6
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
c:0001 p:0000 s:0002 b:0002 l:0014e8 d:0014e8 TOP
---------------------------
-- Ruby level backtrace information
----------------------------------------
script/rails:6:in `<main>'
script/rails:6:in `require'
/Users/user/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/
commands.rb:23:in `<top (required)>'
/Users/user/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/
commands/console.rb:8:in `start'
/Users/user/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/
commands/console.rb:44:in `start'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb.rb:69:in
`start'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb.rb:69:in
`catch'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb.rb:70:in
`block in start'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb.rb:155:in
`eval_input'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb/ruby-lex.rb:
228:in `each_top_level_statement'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb/ruby-lex.rb:
228:in `catch'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb/ruby-lex.rb:
229:in `block in each_top_level_statement'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb/ruby-lex.rb:
229:in `loop'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb/ruby-lex.rb:
243:in `block (2 levels) in each_top_level_statement'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb.rb:156:in
`block in eval_input'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb.rb:273:in
`signal_status'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb.rb:159:in
`block (2 levels) in eval_input'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb/context.rb:
254:in `evaluate'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb/
workspace.rb:80:in `evaluate'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/irb/
workspace.rb:80:in `eval'
(irb):9:in `irb_binding'
(irb):9:in `dead?'


I would be grateful for some insight into how I need to properly
configure TinyTDS to get it to work with Rails 3.0 and ruby 1.9.2.

Best,
Tom

Ken Collins

unread,
May 13, 2011, 12:36:51 PM5/13/11
to rails-sqlse...@googlegroups.com

Tom,

> My freetds.conf file points to SQL Server instance running on VMWare
> using the IP address of the VMware instance:
>
> [MACBOOKPRO_MSSQL]
> host = 172.16.27.10
> instance = MACBOOKPRO
> tds version = 8.0

Interesting, I never knew the conf file took an instance setting.

> Before making any other changes, I started console and tried the
> following statement (after some trial and error)
> client = TinyTds::Client.new(:username => 'user', :password =>
> 'password', :dataserver => 'WIN-MR6B172QT2R\MACBOOKPRO', :adapter =>
> 'sqlserver', :database => 'ISAAC')

Good, your going to want to make sure you can get a connection with a basic TinyTds::Client first before moving on to the adapter. What you find out here will have to translate down to your database.yml file. FYI, there is no :adapter option for a TinyTds::Client. It would just be ignored.

If you provide an instance to dataserver like that, I believe it will look for a DNS entry for the first part, in this case "WIN-MR6B172QT2R". I have no idea what that is. Why not use either one of these. Remember, :host is only used if :dataserver is blank.

:host => 172.16.27.10
:host => 'somednsname'
:dataserver => "MACBOOKPRO_MSSQL"

All three of those should work in isolation.

> I would be grateful for some insight into how I need to properly
> configure TinyTDS to get it to work with Rails 3.0 and ruby 1.9.2.

It should be dead simple. The only thing that could throw you off is that named instance parsing, but the right settings should negate that.


- Ken


iagot

unread,
May 22, 2011, 5:36:25 PM5/22/11
to Rails SQLServer Adapter
Ken,

I'm seeing the same problem on a Windows system with a local
database. I did not get the crash Tom reported, but client.closed?
returns true.
The command is:
client=TinyTds.Client.new(:userneame=>'tag',:password=>'',:dataserver=>'JIM-
PC\SQLEXPRESS',:database=>'College')

password is null, JIM-PC\SQLEXPRESS is what Microsoft SQL Server
Management Studio shows.

I tried the :hosts thing you showed below. No luck.

I am using "gem install tiny_tds --pre", installed today.

Tnx

Jim

Ken Collins

unread,
May 22, 2011, 5:41:48 PM5/22/11
to rails-sqlse...@googlegroups.com

Try replacing the "JIM-PC" part with a valid hostname or IP?

- Ken

> --
> You received this message because you are subscribed to the Google Groups "Rails SQLServer Adapter" group.
> To post to this group, send email to rails-sqlse...@googlegroups.com.
> To unsubscribe from this group, send email to rails-sqlserver-a...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rails-sqlserver-adapter?hl=en.
>

Jim Taggart

unread,
May 22, 2011, 5:53:00 PM5/22/11
to rails-sqlse...@googlegroups.com
I tried 127.0.0.1 but it didn't make any difference


----- Original Message -----
From: Ken Collins <k...@metaskills.net>
To: rails-sqlse...@googlegroups.com
Sent: Sun, 22 May 2011 21:41:48 -0000 (UTC)
Subject: Re: [RailsSQLServer] Re: TinyTds::Error: closed connection when trying to connect to a sql server named instance


Try replacing the "JIM-PC" part with a valid hostname or IP?

- Ken

On May 22, 2011, at 5:36 PM, iagot wrote:

> Ken,
>
> I'm seeing the same problem on a Windows system with a local
> database.  I did not get the crash Tom reported, but client.closed?
> returns true.
> The command is:
> client=TinyTds.Client.new(:userneame=>'tag',:password=>'',:dataserver=>'JIM-
> PC\SQLEXPRESS',:database=>'College')
>
> password is null, JIM-PC\SQLEXPRESS is what Microsoft SQL Server
> Management Studio shows.
>
> I tried the :hosts thing you showed below.  No luck.
>
> I am using "gem install tiny_tds --pre", installed today.
>
> Tnx
>
> Jim
>

Ken Collins

unread,
May 22, 2011, 6:16:58 PM5/22/11
to rails-sqlse...@googlegroups.com

Jim,

Just to make sure, you do not have a freetds.conf file right, nor have you specified a path for one using the FREETDS environment variable?

Can you tell me a little more why or how I can setup a named instance? I just have one VM for each DB, 2000, 2005, and 2008. That you know if, is there a way I can download something and setup up another "instance". I tried googling around to download express, but it seems that it would only want to upgrade my old install on my 2005 machine to 2008. Also, when you have a named instance like that, what IPs are each instance running under?

Lastly, can you set a TDSDUMP environment variable to a path, could be C:\freetds.log and show me what that looks like when trying to connect?


- Ken


Jim Taggart

unread,
May 22, 2011, 6:47:26 PM5/22/11
to rails-sqlse...@googlegroups.com
Ken,

I forgot to mention that Jim-PC is the hostname for my computer.

Thank you for your efforts on the part of rails-sqlserver-adapter.  Your comments have been a great help to me (never would have known I needed the --pre, for example).

Here's the contents of the log file:
log.c:196:Starting log file for FreeTDS 0.92.dev.20110429
    on 2011-05-22 18:32:10 with debug flags 0x4fff.
config.c:711:Setting 'dump_file' to 'C:\Users\tag\Documents\Valulytics\Try6\College\tdsdump.log' from $TDSDUMP.
iconv.c:330:tds_iconv_open(0x414e780, UTF-8)
iconv.c:187:local name for ISO-8859-1 is ISO-8859-1
iconv.c:187:local name for UTF-8 is UTF-8
iconv.c:187:local name for UCS-2LE is UCS-2LE
iconv.c:187:local name for UCS-2BE is UCS-2BE
iconv.c:349:setting up conversions for client charset "UTF-8"
iconv.c:351:preparing iconv for "UTF-8" <-> "UCS-2LE" conversion
iconv.c:391:preparing iconv for "ISO-8859-1" <-> "UCS-2LE" conversion
iconv.c:394:tds_iconv_open: done
net.c:934:tds7_get_instance_port(192.168.1.102, SQLEXPRESS)
net.c:1057:instance port is 0
login.c:436:invalid port number
dblib.c:1428:dbclose(0x43d37a8)
dblib.c:258:dblib_del_connection(0x6b4b4588, 0x414e780)
mem.c:615:tds_free_all_results()
dblib.c:305:dblib_release_tds_ctx(1)
dblib.c:5863:dbfreebuf(0x43d37a8)
dblib.c:724:dbloginfree(0x434f3a8)


I am running Sql Server Express 2008, so that may be the answer to your named instance query.  I'm really just a hacker here.  I developed an application for a startup company (that isn't paying me) using squlite3 and rails 2.  Then they wanted to base it on Sql Server, so I've been on an odyssey  since November trying to accomplish the goal.  TinyTds looks like the solution I have sought, if I can connect to it.

Thanks for your (amazingly prompt) help.

Jim


----- Original Message -----
From: Ken Collins <k...@metaskills.net>
To: rails-sqlse...@googlegroups.com
Sent: Sun, 22 May 2011 22:16:58 -0000 (UTC)
Subject: Re: [RailsSQLServer] Re: TinyTds::Error: closed connection when trying to connect to a sql server named instance


Ken Collins

unread,
May 22, 2011, 7:00:35 PM5/22/11
to rails-sqlse...@googlegroups.com

Jim,

I googled around using some hints in that log file and came upon this thread.

http://lists.ibiblio.org/pipermail/freetds/2008q4/023972.html

Can you try verifying that your instance name is correct? Alternately, can you lookup the IP for that instance and just use the :host and :port options with those values to see if that works?


- Ken

Joe Rafaniello

unread,
May 23, 2011, 10:20:25 AM5/23/11
to rails-sqlse...@googlegroups.com
Ken/Jim,

To install a second 'named' instance, you would just re-run the sql server install for your current version.  I believe it will ask you to provide a name for the second installation 'instance'.  

Using a named instance requires running SQL Server Browser Service on the SQL Server.  On SQL server 2005, it's in SQL Server Configuration Manager.
Here's the instructions on sql server 2008 r2:

I believe 1433 is the default port if you have a single instance.  In your freetds.conf, you can specify the port or instance (the named instance).  If you use a named instance, it will query the SQL Server Browser Service to resolve the name to a port.

Of course, you will need to have the ports open on any firewalls.

Joe



 - Ken

Ken Collins

unread,
May 23, 2011, 10:30:32 AM5/23/11
to rails-sqlse...@googlegroups.com

Thanks for the instructions Joe, I'll follow them sometime this week to get a second instance up on one of my VMs. Jim is on Windows and would be leveraging the latest configuration options in FreeTDS. That means no freetds.conf file. One can be set, but we are moving away from "needing" such a file and allowing all configurations to be passed down at the time the client is initialized.

It is good to know that named instances are just an easy way to not worry about ports. It means we should always be able to be explicit if needed. But I do want to test the latest FreeTDS to make sure configurations like that do indeed work as advertised and then ultimately document the details on our wiki. Will reply back later this week when I have an new installed instance.

 - Ken

Joe Rafaniello

unread,
May 23, 2011, 11:04:09 AM5/23/11
to rails-sqlse...@googlegroups.com
I hope the instructions help.  It's actually pretty easy to get named instances working once you know about the ports required and the need for a running 'SQL Server Browser Service" on sql server (mine was disabled).  Just like DNS name resolution issues, start with trying to connect directly to the port number and only then try connecting to the named instance.  

I can't wait to be able to take advantage of the changes to no longer need to specify settings in the freetds.conf.  Since I'm way behind your work on tinytds, I currently have to set  host, tds version, client charset, port or instance in freetds.conf and database in odbc.ini and it's easy to miss something.

Let me know if you have any issues setting up named instances.

Joe


On Mon, May 23, 2011 at 10:30 AM, Ken Collins <k...@metaskills.net> wrote:

Thanks for the instructions Joe, I'll follow them sometime this week to get a second instance up on one of my VMs. Jim is on Windows and would be leveraging the latest configuration options in FreeTDS. That means no freetds.conf file. One can be set, but we are moving away from "needing" such a file and allowing all configurations to be passed down at the time the client is initialized.

It is good to know that named instances are just an easy way to not worry about ports. It means we should always be able to be explicit if needed. But I do want to test the latest FreeTDS to make sure configurations like that do indeed work as advertised and then ultimately document the details on our wiki. Will reply back later this week when I have an new installed SQL Server Browser Serviceinstance.
Reply all
Reply to author
Forward
0 new messages