Solved

Setting the Connection Timeout for OdbcConnection

Posted on 2014-01-20
5
632 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Contant UI messages in .NET 1 41
Voice recognition ASP or ASP.NET or JavaScript 2 45
Image(7) 1 37
VB.NET HttpWebRequest 12 34
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

21 Experts available now in Live!

Get 1:1 Help Now