Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Setting the Connection Timeout for OdbcConnection

Posted on 2014-01-20
5
Medium Priority
?
843 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:James Atkin
[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 2000 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:James Atkin
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:
James Atkin 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:James Atkin
ID: 39808416
Thanks for the assistance :-)
(Hopefully if I've done it right, you should get the points!)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

604 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