473,387 Members | 1,619 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,387 software developers and data experts.

C++ programmatically reconnection to SQL Server 2000 after stop/restart SQL service

I have write a program using MS Visual studio C++ 7.0 (platform Windows XP professional). I'm not using .NET.
This program save data in a SQL server 2000 database using ADO.
Everything works correctly, yet
if i stop the sql service and then restart it, while the programs write records, the reconnection to the server dont't work.

When the service stops i catch all the exceptions. I want try to reconnect to the database continually till the service restart and the connection reestablish.

I use connection, recordset and command object to execute an SQL statement (UPDATE MyTable SET filed1=value1 etc..

After the record write failure i call the method to close the connection. Even if the service is stopped this function don't generate an exception. Later when i call the ReconnectDatabase function i detect that the connection state is down and i try to reconnect to the database.

The reconnection will be reestablish correctly.

If i call BeginTransaction/RollbackTransaction/CommitTransaction when i write the record i don't succed to close the connection after the record writing failure. The Close() function generate an exception. If i read the connection state it is ON even if it is not true.
I have tried to rewrite my ReconnectDatabase function but i don't succed to reconnect to the database even if the service has been restared.

This is the code to write the record:
Expand|Select|Wrap|Line Numbers
  1.  
  2.     try
  3.     {
  4.         pConnenction->BeginTrans();
  5.     }
  6.     catch(_com_error &e) 
  7.     {
  8.         .......        
  9.     }
  10.  
  11.  
  12.     // CADOCommand is my wrapper class around ADO object
  13.     CADOCommand cmd(pDatabase, cmdString, adCmdText);
  14.     if (myRecordset.Execute(&cmd) == FALSE) {
  15.  
  16.         try
  17.         {    
  18.             pConnection->RollbackTrans();
  19.         }
  20.         catch(_com_error &e) 
  21.         {
  22.              .........
  23.         }
  24.  
  25.         // When the service is stop this function generate an exception only if i 
  26.         // have previously call  BeginTrans/Rollback.
  27.         // I can catch this exception but my ReconnectionDatabase() function 
  28.         // (see later) don't succed to reconnect to the database
  29.         rsMachines.GetActiveConnection()->Close();
  30.  
  31.         return FALSE;
  32.  
  33.     }
  34.  
  35.     try
  36.     {
  37.         pConnection->CommitTransaction();
  38.     }
  39.     catch(_com_error &e) 
  40.     {
  41.         ...........
  42.     }
  43.  
  44.  
This is the function that i use to try to reconnect to the database:

Expand|Select|Wrap|Line Numbers
  1. BOOL CDbase::ReconnectDatabase(_ConnectionPtr pConnectionPtr)
  2. {
  3.     long state;
  4.     _bstr_t strConnection;
  5.  
  6.     state = pConnectionPtr->GetState(); 
  7.  
  8.     if (state == 0) {    
  9.  
  10.         strConnection = (_bstr_t) pDatabase->GetConnectionString();
  11.  
  12.  
  13.         pConnectionPtr->CursorLocation = ADODB::adUseClient;
  14.  
  15.         try {
  16.  
  17.                  pConnectionPtr->Open(strConnection,_bstr_t(L""),_bstr_t 
  18.                                                             (L""),ADODB::adModeUnknown);
  19.         }
  20.         catch(_com_error& e) {
  21.  
  22.  
  23.                 return FALSE;
  24.         }
  25.     }
  26.  
  27.     return TRUE;
  28. }
  29.  
I have tryed to close/open the database
I have tryed to delete and new all the object and then reopen the database.
If the sql service has been stopped and then restarted I only have exception.
Only if i don't use recovery the reconnection restart but i need recovery.


I have the same problem even when i disconnect/reconnect the network cable using a remote database.

Thanks for every kind of suggestion.

Emanuele.
Oct 11 '06 #1
0 2178

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Keith | last post by:
I'm in the same boat as the fellow who posted this message back in August: Title : Windows Service, How does one make a service "fail" properly? Author : Ross Bennett Group :...
2
by: John Spiegel | last post by:
Hi all, Is there a way to programmatically restart Windows (2000, specifically), login automatically then start an application? I've been running across some Windows Shell options (which...
9
by: Steve Buster | last post by:
All right, I have read every forum, newsgroup etc about this issue and no one seems to know how to fix it. I am getting a "Server Application Unavailable" exception running my .NET 1.1...
2
by: Tedmond | last post by:
Dear all, I wrote a service by C# but I found the service is not quite stable. I want to add some code that refresh or restart the service itself for a fixed period. Does anyone know how to...
7
by: shai | last post by:
I am working at .net 1.1, writing in c#. I have windows service with a COM object. Every unexpected time The COM object throw an error that make my service get stuck (do not respond). I can catch...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
0
by: Emanuele | last post by:
I have write a program using MS Visual studio C++ 7.0 (platform Windows XP professional). I'm not using .NET. This program save data in a SQL server 2000 database using ADO. Everything works...
1
by: sherifbk | last post by:
Problem description ============== - I have 4 clients and 1 server (SQL server) - 3 clients are Monitoring console 1 client is operation console - Monitoring console collects some data from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.