Issue Details (XML | Word | Printable)

Key: DERBY-3338
Type: Improvement Improvement
Status: Resolved Resolved
Resolution: Fixed
Priority: Minor Minor
Assignee: Mamta A. Satoor
Reporter: Daniel John Debrunner
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Derby

CancelQueryTask.forgetContext() could be simplified.

Created: 21/Jan/08 10:56 PM   Updated: 29/Jun/09 01:13 PM
Return to search
Component/s: Services, SQL
Affects Version/s: None
Fix Version/s: 10.4.2.0, 10.5.1.1

Time Tracking:
Not Specified

File Attachments:
  Size
File Licensed for inclusion in ASF works DERBY_3338_rework_forgetContext_v1.diff 2008-06-09 07:59 PM Mamta A. Satoor 0.8 kB
Issue Links:
Reference
 

Resolution Date: 11/Jun/08 04:36 AM


 Description  « Hide
Minor issue but CancelQueryTask.forgetContext() has this code (in GenericStatementContext.java)

        public void forgetContext() {
            boolean mayStillRun = !cancel();
            if (mayStillRun) {
                synchronized (this) {
                    statementContext = null;
                }
            }
        }

The mayStillRun = !cancel() is somewhat confusing. I can't see from the javadoc of TimerTask.cancel() how its return value could indicate the task may still run.
Less confusing code could be:

        public void forgetContext() {
               synchronized (this) {
                    statementContext = null;
                }
                cancel();
        }

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Mamta A. Satoor added a comment - 09/Jun/08 07:58 PM
Reworking the code as suggested by Dan may actually fix the problem experienced on weme6.1 (DERBY-1848 for jdbcapi/SetQueryTimeoutTest.java) The way the CancelQueryTask.forgetContext() is written right now, it leaves a small window of code path through it that leaves the CancelTimerTask linked to the StatementContext, a timing window allows the CancelTimerTask's run method to later cancel a different statement once the StatementContext is re-used. A short example here might be helpful.

Let Stmt1A be a JDBC Statement object(with set query timeout set on it) and SC1 be a StatementContext. Say that user has requested a ResultSet object movement for Stmt1A. At the start of the ResultSet movement code, we use a StatementContext (say in this case it is SC1) and push that StatementContext for Stmt1A. During the push, we start a Timer say CQt1 since user has set query timeout for Stmt1A. After the ResultSet movement code, Stmt1A starts its pop on SC1. During the pop, say the Timer CQt1 expires and so CQT1 is queued upto run. Before CQT1 runs, say the control goes back to finish the rest of the pop SC1 code. During the pop, we call CancelQueryTask.forgetContext(). The current code here checks if the Timer has been cancelled. If not cancelled, then we mark the StatementContext associated with CQT1 as null otherwise we don't touch StatementContext object associated with CQT1. Since in our specific case, the Timer has already been cancelled, we leave SC1 associated with CQT1. After this we finish the rest of the code to pop SC1 for Stmt1A. Keep in mind that CQT1 has not run yet. Since SC1 is available for some other Statement to use, say Stmt2B pushes SC1 before it does it's ResultSet movement. So, now SC1 is associated with Stmt2B. At this point, say CQT1's run gets scheduled. Since it still has SC1 associated with it (because we never cleared it in forgetContext), CQT1 is going to mark SC1 as timedout. This is where we run into problem. We are indirectly marking wrong JDBC Statement as timed out (in this particular case Stmt2B) and that is what I think is causing occassional timeouts in jdbcapi/SetQueryTimeoutTest.java. I have rearranged the code and run the tests and I have not been able to reproduce the problem with jdbcapi/SetQueryTimeoutTest.java even after 30 runs (normally it reproduces for me in about 3-5 runs). I will attach the simple patch for review and will plan on committing it tomorrow if no one has any comments.

Dag H. Wanvik added a comment - 10/Jun/08 02:28 AM - edited
Your explanation sounds reasonable and the patch looks safe to me.

+1

Mamta A. Satoor added a comment - 11/Jun/08 04:36 AM
Thanks, Dag, for the review. I have committed the changes into trunk with revision 666519.

Mamta A. Satoor added a comment - 11/Jun/08 10:11 PM - edited
Migrated the changes into 10.4(with revision 666871) codeline. Will work on migrating next to 10.3 codeline.

Mamta A. Satoor added a comment - 12/Jun/08 03:56 PM
Migrated the changes into 10.3.3.1 codeline also with revision 667136.