Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-07-07
4
Medium Priority
?
280 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 2000 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 2000 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 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

688 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