473,396 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

database connection loss

Hallo!

I'm working with C# .NET 2.0, implementing Client/Server Applications which
are connecting via Network to SQL-Server or Oracle Databases.
To stay independent from the underlaying Database I use
System.Data.Common.DBConnection and .DBCommand.

How can I keep aware from connection losses (network not availeable,
db-server not available...)?
Are there any strategies to detect this broken connections, and how can I
implement reconnecting to the database?

The only solution I figured out, is creating a thread which frequntly
performs a SELECT 1 (MSSQL) or SELECT 1 FROM DUAL (Oracle) within a
try-catch block. When an exception occurs I have to send my other
worker-threads to sleep, till I'm able to reopen the database connection and
my Select-Statement works again. Then I have to trigger the other threads to
reopen their connections and continue working (every thread gots his own
DBConnection).

For me this seems a little bit to complicated and insecure.
It also could happen that one of the worker threads gots a timeout due
locking or delayed server responstime, and will loose his Connection in fact
of this exception. Such, or similar problems I can not get handled with my
solution.

Are there any suggestions, patterns or automathisms with ADO 2.0?

with friendly regards

Martin
Apr 12 '06 #1
3 10256
Martin,

Best-practices coding in ADO.NET dictates that a connection should be opened
immediately prior to its use, and closed immediately thereafter. Your post
seems to indicate that you have connections that are kept open. When you
close an ADO.NET connection with most providers, it is returned to the
connection pool automatically, which is almost always the preferential way to
handle database work.

When you attempt to open a connection and it throws a timeout exception,
your code should be engineered to handle this.

Hope that helps.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Martin B" wrote:
Hallo!

I'm working with C# .NET 2.0, implementing Client/Server Applications which
are connecting via Network to SQL-Server or Oracle Databases.
To stay independent from the underlaying Database I use
System.Data.Common.DBConnection and .DBCommand.

How can I keep aware from connection losses (network not availeable,
db-server not available...)?
Are there any strategies to detect this broken connections, and how can I
implement reconnecting to the database?

The only solution I figured out, is creating a thread which frequntly
performs a SELECT 1 (MSSQL) or SELECT 1 FROM DUAL (Oracle) within a
try-catch block. When an exception occurs I have to send my other
worker-threads to sleep, till I'm able to reopen the database connection and
my Select-Statement works again. Then I have to trigger the other threads to
reopen their connections and continue working (every thread gots his own
DBConnection).

For me this seems a little bit to complicated and insecure.
It also could happen that one of the worker threads gots a timeout due
locking or delayed server responstime, and will loose his Connection in fact
of this exception. Such, or similar problems I can not get handled with my
solution.

Are there any suggestions, patterns or automathisms with ADO 2.0?

with friendly regards

Martin

Apr 12 '06 #2
Hallo Peter!

Thank you for immediate response.

I know that ADO.NET offers a pooling strategy to accelerate establishing
database connectivity, but can you tell me what is the great benefit for
this architecture? I understood it that way, that this solution makes only
sense when several forms not so frequently performs their updates against
the database.

In my case I've coded a Win-Service which performs the entire booking
operations for all my Terminal-Programs, that means that the Service is busy
all the time and for me it would not make any sense closing and reopening
the database connection after each transaction. The Service performs up to 5
transactions per second so I have to pay attention of optimizing the
performance.

By closing the connection you mean releasing it to the connection-pool?

When exactly will the connections of the pool be opened and what happens if
the network is not available, are all connections marked as broken, will
they be released or do I get at each open command an exception till all
connections of the pool are passed through and detected as broken. Is it up
to the programmer to flush the pool when he detects first appearance of a
network loss (using SQLConnection.ClearPool or ClearAllPools).

Do I alwayes get the same Timeout Exception at opening or can it change
depending on the network error (for example: Server down, Database down,
cutting network cable on server-side, cutting network cable on client-side
....).
I could not exactly figure out how this pooling mechanism works. Are all
connections prepared and opened at the first Open command and all further
Open commands get the next available Connection from the Pool. But there
must be any verifications whether the Connection state is valid or not to
get the Timeout Exception.
with friendly regards

Martin
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:4A**********************************@microsof t.com...
Martin,

Best-practices coding in ADO.NET dictates that a connection should be
opened
immediately prior to its use, and closed immediately thereafter. Your post
seems to indicate that you have connections that are kept open. When you
close an ADO.NET connection with most providers, it is returned to the
connection pool automatically, which is almost always the preferential way
to
handle database work.

When you attempt to open a connection and it throws a timeout exception,
your code should be engineered to handle this.

Hope that helps.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Martin B" wrote:
Hallo!

I'm working with C# .NET 2.0, implementing Client/Server Applications
which
are connecting via Network to SQL-Server or Oracle Databases.
To stay independent from the underlaying Database I use
System.Data.Common.DBConnection and .DBCommand.

How can I keep aware from connection losses (network not availeable,
db-server not available...)?
Are there any strategies to detect this broken connections, and how can I
implement reconnecting to the database?

The only solution I figured out, is creating a thread which frequntly
performs a SELECT 1 (MSSQL) or SELECT 1 FROM DUAL (Oracle) within a
try-catch block. When an exception occurs I have to send my other
worker-threads to sleep, till I'm able to reopen the database connection
and
my Select-Statement works again. Then I have to trigger the other threads
to
reopen their connections and continue working (every thread gots his own
DBConnection).

For me this seems a little bit to complicated and insecure.
It also could happen that one of the worker threads gots a timeout due
locking or delayed server responstime, and will loose his Connection in
fact
of this exception. Such, or similar problems I can not get handled with
my
solution.

Are there any suggestions, patterns or automathisms with ADO 2.0?

with friendly regards

Martin

Apr 13 '06 #3
Hi Martin,
I know that ADO.NET offers a pooling strategy to accelerate establishing
database connectivity, but can you tell me what is the great benefit for
this architecture? I understood it that way, that this solution makes only
sense when several forms not so frequently performs their updates against
the database.
This is the way ADO.Net works. The solution always makes sense because that
is the way ADO.Net is architected. If you go against the grain, you're in
trouble (as you are).

Here's how it works: The most expensive and time-consuming database
operation is opening a Connection. Therefore, ADO.Net employs Connection
Pooling, which means that when you close a Connection, it is not actually
closed, but returned to the Connection Pool. When you open a new Connection
that uses the same Connection parameters (i.e. Connection String), it
fetches that Connection, or the next available one from the Pool. ADO.Net
manages the Connection Pool itself, and you don't even have to think about
it.

When you bypass Connection Pooling, by keeping a Connection opened, you
break it. Since you keep the Connection open, it cannot be returned to the
pool and re-used by another thread.
In my case I've coded a Win-Service which performs the entire booking
operations for all my Terminal-Programs, that means that the Service is
busy
all the time and for me it would not make any sense closing and reopening
the database connection after each transaction.
As you should be able to see now, it doesn't make any sense *not* to
"reopen" a Connection after each transaction. In a very real sense, it is
already opened after the first use. In addition, if a Connection from the
Pool is in use, the Pool will create another one.

As for optimization, you should probably look at client-side caching of data
when you can. Of course, to perform some sort of transaction that *changes*
data, this requires another round-trip to the database.
When exactly will the connections of the pool be opened and what happens
if
the network is not available, are all connections marked as broken, will
they be released or do I get at each open command an exception till all
connections of the pool are passed through and detected as broken. Is it
up
to the programmer to flush the pool when he detects first appearance of a
network loss (using SQLConnection.ClearPool or ClearAllPools).
Again, this is handled by the framework. If the network is not available,
what difference does it make to you? You cannot perform any database
operations regardless of whether you or the Framework manages your
Connections, until the Network becomes available again.
Do I alwayes get the same Timeout Exception at opening or can it change
depending on the network error (for example: Server down, Database down,
cutting network cable on server-side, cutting network cable on client-side
...).
Depending on the type of "Timeout Exception," you may be able to tweak this
via your Connection String. However, your first priority should be to fix
the Connection model you're using to make use of the built-in Connection
Pooling. If you still have a Timeout problem, you can fix that later.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"Martin B" <ma************@gamed.com> wrote in message
news:44***********************@newsreader.inode.at ... Hallo Peter!

Thank you for immediate response.

I know that ADO.NET offers a pooling strategy to accelerate establishing
database connectivity, but can you tell me what is the great benefit for
this architecture? I understood it that way, that this solution makes only
sense when several forms not so frequently performs their updates against
the database.

In my case I've coded a Win-Service which performs the entire booking
operations for all my Terminal-Programs, that means that the Service is
busy
all the time and for me it would not make any sense closing and reopening
the database connection after each transaction. The Service performs up to
5
transactions per second so I have to pay attention of optimizing the
performance.

By closing the connection you mean releasing it to the connection-pool?

When exactly will the connections of the pool be opened and what happens
if
the network is not available, are all connections marked as broken, will
they be released or do I get at each open command an exception till all
connections of the pool are passed through and detected as broken. Is it
up
to the programmer to flush the pool when he detects first appearance of a
network loss (using SQLConnection.ClearPool or ClearAllPools).

Do I alwayes get the same Timeout Exception at opening or can it change
depending on the network error (for example: Server down, Database down,
cutting network cable on server-side, cutting network cable on client-side
...).
I could not exactly figure out how this pooling mechanism works. Are all
connections prepared and opened at the first Open command and all further
Open commands get the next available Connection from the Pool. But there
must be any verifications whether the Connection state is valid or not to
get the Timeout Exception.
with friendly regards

Martin
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:4A**********************************@microsof t.com...
Martin,

Best-practices coding in ADO.NET dictates that a connection should be
opened
immediately prior to its use, and closed immediately thereafter. Your
post
seems to indicate that you have connections that are kept open. When you
close an ADO.NET connection with most providers, it is returned to the
connection pool automatically, which is almost always the preferential
way to
handle database work.

When you attempt to open a connection and it throws a timeout exception,
your code should be engineered to handle this.

Hope that helps.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Martin B" wrote:
Hallo!

I'm working with C# .NET 2.0, implementing Client/Server Applications
which
are connecting via Network to SQL-Server or Oracle Databases.
To stay independent from the underlaying Database I use
System.Data.Common.DBConnection and .DBCommand.

How can I keep aware from connection losses (network not availeable,
db-server not available...)?
Are there any strategies to detect this broken connections, and how can
I
implement reconnecting to the database?

The only solution I figured out, is creating a thread which frequntly
performs a SELECT 1 (MSSQL) or SELECT 1 FROM DUAL (Oracle) within a
try-catch block. When an exception occurs I have to send my other
worker-threads to sleep, till I'm able to reopen the database connection
and
my Select-Statement works again. Then I have to trigger the other
threads to
reopen their connections and continue working (every thread gots his own
DBConnection).

For me this seems a little bit to complicated and insecure.
It also could happen that one of the worker threads gots a timeout due
locking or delayed server responstime, and will loose his Connection in
fact
of this exception. Such, or similar problems I can not get handled with
my
solution.

Are there any suggestions, patterns or automathisms with ADO 2.0?

with friendly regards

Martin


Apr 13 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Peter Larsson | last post by:
Hi there, I've recently developed a VBA-program in Excel that fetches and presents data from a distant Access database over a wireless peer-to-peer network (both computers running Win XP Pro)....
3
by: markaelkins | last post by:
I want to create a simple user interface to collect the following data and store the data in a SQL database…. Could someone please help me get started? Data to collect from user interface and...
1
by: Sonya | last post by:
Hello, I have trouble connecting to Access database from my web service. Database is located in a subdirectory of where service pages are. Code below produces the following exception: "No error...
3
by: Martin B | last post by:
Hallo! I'm working with C# .NET 2.0, implementing Client/Server Applications which are connecting via Network to SQL-Server or Oracle Databases. To stay independent from the underlaying Database...
5
by: Dennis | last post by:
I am totally confused between Access, SQL Express, and SQL Server and MSDE and OLEDB vs SQL in .net. Please someone tell me if I"m correct in the following: With MSDE installed, I can program...
3
by: Big Charles | last post by:
Hi, We have developed an ASP.NET web application and are planning to host it in an external Server, which provides us a good bandwidht. We need to put he web application outside because the...
6
by: mpmason14 | last post by:
I found a vbscript on vbskrypt.com that allows me to inventory the software installed on my computer and puts it in an Access database. problem: the script only finds those programs installed...
8
by: mark_aok | last post by:
Hi all, I have a split database. Both the forms, and the tables are stored on a shared network drive (this is Access 2003). The users use the forms, and the tables on the network drive, there...
8
by: Rahul | last post by:
Hi Everyone, I'm currently developing a class for a database, each object of the class will establish a connection to the database on a remote server and and all of this happens on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.