473,414 Members | 1,627 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,414 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 1602

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

Similar topics

3
by: Tony Hodgson | last post by:
I have a problem with an instance of SQL Server that refuses to respond to a shutdown request. I've managed to shutdown the SQL Manager and DTC services but the sqlservr.exe process is permanently...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.