Hi,

Again, we will have to guess! One important information is the order of
magnitude of userDepartmentsNo, userAnalysersNo, userControlsNo and
userTestsNo. As you are using nested loops, it could result in a quite long
execution time and increase dramatically the impact of the synchronisation
problem. If the second request is issued before the first one has completed,
you are having synchronisation problem because you are using what you call
Class level variables.

However, are making a little confusion. The nearest approximation to 'Class
variables' should be static variables. If you do not declare your variables
static, they can be members or local. As you say you are declaring your
variables right after the class declaration, you are probably using members.
This kind of variables are shared by all threads going through your servlet.

TotalPlotsDrawn doesn't cause problem in your code because it's only written
to. TotalScreensDrawn, PlotsDrawn have to be taken care off because they are
incremented and used for computation. To avoid the problem, you have to make
this atomic, ie to synchonize the smallest possible block including write an
read for the variables, where the read operation is supposed to depends on
the written value. Otherwise, another thread could have modified the value
between the write and read operations. As you are using nested loops, its
not straightforward deciding which block to synchronize. In any case, you
might have to rewrite your code to minimize the size of this block.

Synchronizing the whole method works, but there is a drawback: The longer it
takes to execute your method, the more penalty you get by synchronising the
whole method, although the more you need to synchronize! So it's even more
important to reduce the size (in terms of execution time, not line of code)
of the synchronized block.

There are other techniques that can be used to avoid synchronisation. For
example, you could use local copy of the variables and update the member
variables in one place. That way, you might not need to use synchronisation
at all or, if the the variables are to be updated atomically, yo might use
the smallest possible synchronized block.

However, you should pay attention to the two following points:

1) You should determine why the second request is sent and try to solve this
problem in any case.

2) If you solve this problem, you should still solve the synchronisation
problem, at least if more than one client is to access your serlvet. (If two
clients access the servlet simultaneously, you will face the same problem.)

Pierre-Yves

-----Message d'origine-----
De : A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]]De la part de
Graham, Billy
Envoy� : mercredi 19 septembre 2001 17:31
� : [EMAIL PROTECTED]
Objet : Re: Inconsistent behaviour in Servlet output


Hi comrades.

As requested I have posted the 'dodgy' section of code (see below).
There are 3 counter variables used within this section of the method
(doGet()) that are all defined at the class level (i.e. right after the
Class definition, what I would refer to as global variables). I'm fairly
sure I have tried defining them locally with no change in the bug (though I
may be mistaken here as I've tried a lot of things over the last week or
so). The counter variables are PlotsDrawn, TotalPlotsDrawn,
TotalScreensDrawn. The variable that seems to change 'at will' is the
'PlotsDrawn' one. The variable 'Plots' is hardcoded a value of 3.

Hopefully someone can finally suggest the cause of this bug.

Many thanks yet again in anticipation.

Regards,   Billy Graham.


==================================

for (int a=0; a< userDepartmentsNo; a++){
    for (int b=0; b< userAnalysersNo; b++){
        for (int c=0; c< userControlsNo; c++){
            for (int d=0; d< userTestsNo; d++){


 mysqlStatement = "SELECT [Dept], [Anal], [Material], [Date], [Time], " +
                  "[Test], [Result], format(Results.[Date],'dd'), " +
                  "[Id], [BatchNo], [TargetValue], [SD], [Units], " +
                  "format(Results.[Date],'dd/mm/yy'), format(Results.[Time],
'hh:mm') " +
                  "FROM [Results] " +
                  "WHERE [Id] in " +
                  "(SELECT TOP " + (VerticalDivisions + 2) + " [Id] " +
                  "FROM [Results] WHERE ([Dept] = '" + userDepartments[a] +
"') " +
                  "AND ([Anal] = '" + userAnalysers[b] + "') " +
                  "AND ([Material] = '" + userControls[c] + "') " +
                  "AND ([Test] = '" + userTests[d] + "') " +
                  "ORDER by [Date] DESC, [Time] ASC) ORDER by [Date] ASC,
[Time] ASC";

               try {
                    rs = stmt.executeQuery(mysqlStatement);

                    if (rs.next()){
                       PlotsDrawn++;
                       drawLJPlot(out, rs, TotalScreensDrawn, PlotsDrawn,
PlotStyleInUse, PlotX1, PlotY1, PlotX2, PlotY2);
                       ScreenNeedsClosed = true;
                       PlotY1 = PlotY2;
                       PlotY2 = PlotY2 + PlotYSpace;
                       TotalPlotsDrawn ++;
                       }
                   }
                catch (Exception e)
                   {
                    out.println(e);
                   }


             if (PlotsDrawn >= Plots){

              //Draw the navigational controls
                drawNavigationalControls(TotalScreensDrawn, out, (PlotX2 -
GraphRightBorder), 0, PlotX2, ScreenHeightY);

              //Close the SVG Screen Object
                out.println("</g>");
                ScreenNeedsClosed = false;

              //Reset the plotting co-ordinates
                PlotX1 = 0;
                PlotY1 = 0;
                PlotX2 = ScreenWidthX;
                PlotY2 = PlotY1 + PlotYSpace;

              //Increment or reset any counters
                PlotsDrawn = 0;
                TotalScreensDrawn++;
                PlotStyleInUse = "&hiddenPlot;";}

             } // End For 4
          } // End For 3
       } // End For 2
    } // End For 1

=====================================================


-----Original Message-----
From: Geeta Ramani [mailto:[EMAIL PROTECTED]]
Sent: 19 September 2001 15:36
To: [EMAIL PROTECTED]
Subject: Re: Inconsistent behaviour in Servlet output


Billy,

Maybe now would be a good time to post some code..?!
(:-)

Geeta

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to