Archived

This forum has been archived. Please start a new discussion on GitHub.

Freeze example from manual loops (Java)

Maybe related to my recent request-for-help on the C++ example in section 40.3.7, here's a different problem with the Java example in section 40.3.8:

After "slice2freezej --dict StringIntMap,string,int" and keying the code (Client.java), I compile the code sucessfully with "javac Client.java". When I invoke the class ("java Client"), the program does not terminate. Debugging shows that the "while (p.hasNext()) {}" loop does not finish as expected. When I insert "System.out.println(e);" as a fourth line in the block, the output is "a=1 ... z=26" and then the program "just sits there".

Does it wait for a "next" element in the Freeze map? ;)

Andreas

Comments

  • BTW, here's the Java code for my Client. My experiment uses the system configuration of OpenSUSE 10.0 with BDB 4.3.27. I didn't try BDB 4.6.

    Andreas
  • matthew
    matthew NL, Canada
    Thanks for the report. The code in the manual appears to be buggy. We'll fix this.

    As described in the Ice manual 40.3.3 http://www.zeroc.com/doc/Ice-3.2.1/manual/Freeze.41.3.html

    There is, however, one situation where an explicit iterator close is needed to avoid self-deadlock:
    • you do not use transactions, and
    • you have an opened iterator that was used to update a map (it holds a write lock), and
    • in the same thread, you read that map.

    This is exactly the case in your demo application. Therefore you must explicitly close the map iterators. The following fixes your demo:
    		// Iterate over the map and change the values.
    		java.util.Iterator p = map.entrySet().iterator();
    		while (p.hasNext())
    		{
    			java.util.Map.Entry e = (java.util.Map.Entry)p.next();
    			Integer in = (Integer)e.getValue();
    			e.setValue(new Integer(in.intValue() + 1));
    			System.out.println(e);
    		}
                    map.closeAllIterators();
    
  • Thank you, Matthew, for the fix. Client.java now works fine, even with BDB 4.3.27.

    Andreas