Solved

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

Posted on 2006-07-07
4
259 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
conditional code and condition difference 9 82
MaxSpan challenge 9 96
building a service to accessed via telnet client, want colors ? 4 88
Line meaning 9 85
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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