Solved

Will the fail of DB link inside a function affect the outside

Posted on 2006-07-07
4
264 Views
Last Modified: 2013-11-20
I passed a DB link into a function

GetLatestTimeUTC(DataStruct dbSession, CString db2_instid, CString viType);

Inside the function the dbSession will fail somtimes, will it affect the following code outside the function, thx.

struct DataStruct
{
      SQLHENV            hEnv;            
      SQLHDBC            hDbc;            
      bool            connORA;      
public:
      void Clear();
};
0
Comment
Question by:turbot_yu
  • 3
4 Comments
 
LVL 5

Assisted Solution

by:bastibartel
bastibartel earned 500 total points
ID: 17057206
Hi turbot_yu,
YOu should pass the struct dbSession by reference.

change
     GetLatestTimeUTC(DataStruct    dbSession, CString db2_instid, CString viType);
=> GetLatestTimeUTC(DataStruct &dbSession, CString db2_instid, CString viType);

passing structs rather than their address will give you unpredictable results unless you have a copy constructor

Cheers!
0
 
LVL 5

Assisted Solution

by:bastibartel
bastibartel earned 500 total points
ID: 17057229
turbot_yu,

I think I misread your question.
No, as it is it will not affect your outside code. Neverthelesse the parameter dbSession inside the function won't have the same content as the one you passed to the function.

Therefor, in correction of my previous post.
=> GetLatestTimeUTC(const DataStruct &dbSession, CString db2_instid, CString viType);

If you now attempt to change dbSession inside the function, the compiler will give you an error message.
The compiler will ensure, that it's not changed.

Cheers,
Sebastian
0
 

Author Comment

by:turbot_yu
ID: 17068246
It is like this, inside the function, sometime, the DB connection will timeout since it will go by internet.

If the connection failed, it will affect the following outside code. The connection outside seems also lost.

What I want to do is to check the connection inside the function, if it failed by timeout, I will reconnect it and create a new connection. And want to pass the reconnected connection to the others outside the function. Any suggestion?
0
 
LVL 5

Accepted Solution

by:
bastibartel earned 500 total points
ID: 17079807
Hi  turbot_yu,

If chances are that the connection state is being altered within the function, how about opening an alltogether independent connection inside GetLatestTimeUTC, so the function has definiteley no side effects.
That would probably be easier than reinstating the exact same state as it had before the function call.

After all, to be on the safe side you'd have to re-set any options, cursor states, whatever,  that calling functions might depend upon.
If you expect GetLatestTimeUTC to not alter the session variable 'dbSession', pass it by const reference ('const DataStruct &').

If the compiler then bothers you about altering a const object - rethink your approach to match your requirement (a const dbsession)  

Cheers,
Sebastian
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows 10 how make picture as desktop background 2 65
wordappend challenge 8 200
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 848
unix example issues 18 98
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

860 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