Solved

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

Posted on 2006-07-07
4
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Doc'in system (example?) BA 7 120
NotAlone Challenge 20 88
Unix Command -- Challenging  question 7 102
Not needed 13 130
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 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