Solved

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 107
Doc'in system (example?) BA 7 79
how to split multiple lines delimiter : 8 55
Not needed 13 58
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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 video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now