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

ConnectTimeout to be made configurable #50

Closed
praveend opened this issue Jul 23, 2013 · 15 comments
Closed

ConnectTimeout to be made configurable #50

praveend opened this issue Jul 23, 2013 · 15 comments

Comments

@praveend
Copy link

Hi Daniel

I see that the connectTimeOut interval is been set to 5 seconds, with a TODO for making it configurable.

We were trying to connect to DB2 server behind tight firewalls and started hitting a connect time out failure. Could you comment out setting the timeout until it is made configurable?

Thanks

Praveen

wankdanker referenced this issue in wankdanker/node-odbc Jul 23, 2013
Set the connectTimeout at runtime by passing and options object to the Database
constructor function:

var db = require('odbc')({ connectTimeout : 60 });

or

var odbc = require('odbc');
var db = new odbc.Database({ connectTimeout : 42 });
@wankdanker
Copy link
Collaborator

Hey Praveen,

I did one better, I hope. I implemented the ability to change the connectTimeout at run-time. Since you have a situation where you can test it, I figured I might as well make it happen. It's hard for me to test a connection timeout because everything on my local network accepts connections so quickly. :)

I released v0.5.24 on my fork. You can set the connectTimeout like so:

var db = require('odbc')({ connectTimeout : 60 });

or

var odbc = require('odbc');
var db = new odbc.Database({ connectTimeout : 42 });

If the connectTimeout is 0 then a connection timeout won't be set.

@wankdanker
Copy link
Collaborator

Install with npm install git://github.com/wankdanker/node-odbc.git#v0.5

Let me know if it works for you and we can close this issue.

Thanks,

Dan

@wankdanker
Copy link
Collaborator

Assuming fixed.

@rossipedia
Copy link

Curious, I'm running into an issue where this property seems to be ignored if the server is not accepting connections at all. I usually run SQL Server in a VM on my OSX machine, and use a connectTimeout value of 2.

If the VM is not running at all, then calls to open can take upwards of 75 seconds or more. Here's my test script and some sample output:

var odbc = require('odbc');
var db = odbc({connectTimeout: 1});

console.log('Connecting with connectTimeout: 2');
console.time('connect');
db.open(process.env.CONNECTION_STRING, function(err) {
  console.log('connect callback running');
  if (err) console.error(err);
  else console.log('connected!');
  if (db.connected) db.close();
  console.timeEnd('connect');
});

Output:

Connecting with connectTimeout: 2
connect callback running
Error: [unixODBC][FreeTDS][SQL Server]Unable to connect to data source
connect: 76009ms

@wankdanker
Copy link
Collaborator

That is interesting. Is there anyway to test this with isql or other tool? Have you ever seen this work as expected? Maybe my initial fix for it didn't actually work.

We use SQLSetConnectAttr with SQL_ATTR_LOGIN_TIMEOUT to enable the timeout. I wonder if this does not include TCP connection or DNS lookup time.

@wankdanker
Copy link
Collaborator

Ahh. Looks like there is a separate attribute for connection timeout: SQL_ATTR_CONNECTION_TIMEOUT (http://msdn.microsoft.com/en-us/library/ms713605(v=vs.85).aspx)

We should implement both the loginTimeout and connectTimeout.

@wankdanker
Copy link
Collaborator

I should be able to get this fixed in a few minutes.

wankdanker referenced this issue in wankdanker/node-odbc Apr 17, 2014
This should fix #50. Previously we were using SQL_ATTR_LOGIN_TIMEOUT as the connect
attribute for connectionTimeout. I think this is wrong. I have replaced the attribute
with SQL_ATTR_CONNECTION_TIMEOUT and introduce another attribute called loginTimeout
to set SQL_ATTR_LOGIN_TIMEOUT.
@wankdanker
Copy link
Collaborator

@rossipedia, could you test with odbc installed from npm install wankdanker/node-odbc#fix-connectTimeout?

If that works, I'll push out v0.6.5.

@rossipedia
Copy link

Connecting with connectTimeout: 2
connect callback running
ERROR: Error: [unixODBC][FreeTDS][SQL Server]Unable to connect to data source
connect: 8ms

That's much, much better, thanks for the prompt fix!

EDIT Although, I might be having an issue now that the server's up. Testing...

@rossipedia
Copy link

Yeah it's still bugged. Now I can't actually connect to the server even though it's running.

@wankdanker
Copy link
Collaborator

Are you getting any errors or anything? I can't replicate.

@rossipedia
Copy link

Let me put together a minimum-code replication

@rossipedia
Copy link

Okay so I'm having trouble replicating this in isolation, and it's a travel day for me. I'll try to pin down what's going on in my existing codebase either tonight or tomorrow.

@wankdanker
Copy link
Collaborator

OK. I'll hold off on merging this until we get this figured out. Safe travels.

@wankdanker
Copy link
Collaborator

FYI: I am randomly encountering an assertion error in the FreeTDS driver when specifying a value for connectTimeout which in turn sets SQL_ATTR_CONNECTION_TIMEOUT:

node: net.c:350: tds_select: Assertion `timeout_seconds >= 0' failed.
Aborted (core dumped)

Sometimes it does assert and some times it does not. May be a race condition some[where|how]. I'm not quite sure how though. We are using a mutex around the SQLSetConnectAttr and SQLDriverConnect calls.

I do not have this issue with the SQL Server Native Client 11.0 driver and connectTimeout > 0.

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

No branches or pull requests

3 participants