Solved

Setting the Connection Timeout for OdbcConnection

Posted on 2014-01-20
5
652 Views
Last Modified: 2014-01-25
Hi all,

I have an application that opens a connection to a database, using OdbcConnection
If the database cannot be reached, it takes the default 30s before timing out.
I wish to change this to just 5s.
I have followed the documentation on how to do this (I think!) but it ignores it and still times out after 30s!

OdbcConnectionStringBuilder cb = new OdbcConnectionStringBuilder();
cb.Driver = "InterBase ODBC Driver";
cb["Server"] = "a.b.c.d";
cb["Database"] = @"\\a.b.c.d\D:\mydatabase.gdb";
cb["Uid"] = "sysdba";
cb["Pwd"] = "*******";

OdbcConnection myConnection = new OdbcConnection();
myConnection.ConnectionString = cb;
myConnection.ConnectionTimeout = 5;
myConnection.Open();

Open in new window

Any idea what is wrong?

Any help would be appreciated.

Regards,

James
0
Comment
Question by:jatkin
  • 3
  • 2
5 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39794015
actually, the default is 15 seconds, according the documentation:
http://msdn.microsoft.com/en-us/library/system.data.odbc.odbcconnection.connectiontimeout%28v=vs.110%29.aspx

so, you are doing everything correctly.
hence, I must presume that it's actually not the odbc part as such timing out, but the network driver (server) resolution time.

does this happen when the server is down?
0
 
LVL 4

Author Comment

by:jatkin
ID: 39794162
If I have the machine running, but the Interbase Database stopped, the exception triggers almost immediately.

My timeout problem is when the machine itself cannot be reached (in my test cases, it is turned off).

I have the same problem using a simple Socket connect:

        private bool isContactable(String IPAddress, Int32 PortNo)
        {
            bool xReturn = false;
            System.Net.IPAddress IP;
            if (System.Net.IPAddress.TryParse(IPAddress, out IP))
            {
                System.Net.Sockets.Socket s = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
                
                try
                {
                    s.Connect(IP, PortNo);
                    s.Disconnect(true);
                    xReturn = true;
                }
                catch 
                {
                    xReturn = false;
                }
            }
            return xReturn;
        }

Open in new window

So I guess the problem is at a lot lower level.

Any ideas how I can override this default?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39794238
yes, it's what I thought: a low-level connection..

I am by far no network specialist, so I think I can only give you some stuff to continue on, eventually: ARP cache life
http://technet.microsoft.com/en-us/library/cc957524.aspx
http://technet.microsoft.com/en-us/library/cc957525.aspx

it might also be the local dns cache.,..
http://support.microsoft.com/kb/318803

hope this helps
0
 
LVL 4

Accepted Solution

by:
jatkin earned 0 total points
ID: 39794257
Thanks for the info...

Not sure if I can do what I need in the way I really want to, but I believe I have identified a solution:
        private bool isContactable(String IPAddress, Int32 PortNo)
        {
            bool xReturn = false;
            System.Net.IPAddress IP;
            if (System.Net.IPAddress.TryParse(IPAddress, out IP))
            {
                System.Net.Sockets.Socket s = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
                IAsyncResult result = s.BeginConnect(IP, PortNo, null, null);
                bool success = result.AsyncWaitHandle.WaitOne(3000, true);
                if (!s.Connected)
                {
                    xReturn = false;
                }
                else
                {
                    xReturn = true;
                }
                s.Close();
            }
            return xReturn;
        }

Open in new window

It looks like the only way to achieve this is to use asynchronous sockets...
This does appear to give me the end result I am after though :-)

Thanks for the help...

James
0
 
LVL 4

Author Closing Comment

by:jatkin
ID: 39808416
Thanks for the assistance :-)
(Hopefully if I've done it right, you should get the points!)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error on Add method 1 38
free scanner TWAIN can be operated with a Web application 9 41
Wav problem 4 19
rest webservice call over https via c# 6 28
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now