Solved

Setting the Connection Timeout for OdbcConnection

Posted on 2014-01-20
5
686 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
[X]
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
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

733 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