Fix issue 597 -- entity manager is properly removed even if close operation fails + unit test.

can you confirm this test is actually running?  i didn't think we used junit4.  might need to convert to junit3.