Discussion Locked

I saw it, solved it, but I don't get it

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Some time ago when I was still learning programming, I learned that the lines of code execute in the order in which they appear unless otherwise branched by a conditional statement or something similar to that.

I have actually seen cases where statements do not execute in the order in with they appear. I know this is possible when using events, but I was not using them.

There is a function that I have used, it basically does three things:

1) Update a temporary table
2) Loads a report (crystal report) that uses the temporary table

3) Clear all records

So the function looks like this;

Private Function ShowSummary()

UpdateTable
ShowReport
ClearTable

End Function

The problem is that the when the report appears, it’s empty! If I stop the program just before the report is shown I find that the table has the updated records.

So then I paused the program just before the report is shown and then continued . It worked – the report showed all the records in the database. So somehow, the ShowReport statement executed before the UpdateTable statement was completed. So to make a permanent solution I inserted delaying statements between the two statements and function looked similar to the one below.

Private Function ShowSummary()

UpdateTable

For j = 1 to 1000000 ‘delaying loop

Next j

ShowReport
ClearTable

End Function

I'm aware that the updating statement takes a long time because it has to do some data access, but why does the next line of code execute before it has finished?

For every programmer that had a similar problem I prescribed this method. It worked fine. Now I feel ashamed. WhenI should be striving for faster algorithms as a programmer, I’m actually delaying them. This is clearly bad – there must be another solution.

What really causes thi

4 replies

by midiprogram

I'm not sure of the details of your program, but I have experienced something like the following in database access with SQL Server. It may apply...
The ShowReport statement is an instruction to the OS to interact with a server. The OS makes a request and waits for the server to respond. While the negotiation is taking place, the ClearTable statement executes. This does not require a reply from the server and so it executes more quickly, on the server, than the ShowReport statement. So, on theserver, ClearTable executes before ShowReport, even though ShowReport executes before ClearTable on the client.
Depending on the server, you may be able to configure, server-side, an instruction to follow commands in the order recieved, to prevent this - I think SQL server allows this, for instance.

by jay eckles

Is it possible that the database you're using requires you to "commit" the data? Perhaps the UpdateTable function is running correctly, but the data is not committed. Then when you run the report, it shows the data as if UpdateTable had not run. I have no idea why the for loop helps; perhaps it gives time for the database to "auto-commit". That's a shot in the dark.

If you do actually need a "pause" like the for loop provides, I would encourage you to seek out a time-based solution. Faster computers will run the loop faster than your current computer, thus breaking your solution. Also, if compilers get smarter, the code might never execute if it can be determined that you never use the value of j.

Good luck.

Jay

by prkvl

You have a temporary table for which dirty reads are
allowed. So when you execute an update which is
actually in execution "read, write and delete" so
naturally this takes more time. Mean while since dirty
reads are allowed, another statement of read is
executed ( your commit has not happend yet). The
read which is faster and is executed like a parallel
thread to your earlier update statement. ' Where ever
you put commit, it stops the parallel executions till the
commit is complete'.
The clear statement has nothing to do here. It just
clears what ever is ther in the table.
Thanks

by kolobe

I tried using the transaction approach (begin – commit transaction) and it worked beautifully.

Thank you.

This discussion is closed to new replies.