Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Setting the Connection Timeout for OdbcConnection

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

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 143

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

860 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