{"id":1158,"date":"2016-05-05T22:26:36","date_gmt":"2016-05-06T05:26:36","guid":{"rendered":"https:\/\/dubinko.info\/blog\/?p=1158"},"modified":"2016-05-11T22:56:11","modified_gmt":"2016-05-12T05:56:11","slug":"unsafe-java","status":"publish","type":"post","link":"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/","title":{"rendered":"Unsafe Java"},"content":{"rendered":"<p>Pop quiz. Why is the following Java 8 code unsafe? UPDATE: this code is fine, see comments. Still good to think about, though.<\/p>\n<p><code>Entity e = new Entity();<br \/>\ne.setName(\"my new entity\");<br \/>\npersistanceLayer.put(e);<br \/>\n<\/code><\/p>\n<p>To provide some\u00c2\u00a0context, <code>Entity<\/code> is a <abbr title=\"plain old java object\">POJO<\/abbr> representing something we want to store in a database. And <code>persistanceLayer<\/code> is an instance of a Data Access Object responsible for storing the object in the database. Don\u00e2\u20ac\u2122t see it yet?<\/p>\n<p>Here\u00e2\u20ac\u2122s the function signature of that <code>put<\/code> method<\/p>\n<p><code>@Async<br \/>\n&lt;E extends AbstractEntity&gt; CompletableFuture&lt;E&gt; put(E newEntity);<br \/>\n<\/code><\/p>\n<p>Ah, yes, Spring, which because of the <a href=\"http:\/\/docs.spring.io\/spring\/docs\/current\/spring-framework-reference\/html\/scheduling.html#scheduling-annotation-support-async\"><code>@Async<\/code><\/a> annotation will proxy this method and cause it to execute in a different thread. The object getting passed in is clearly mutable, and by my reading of the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Java_memory_model\">Java Memory Model<\/a>, there\u00e2\u20ac\u2122s no guarantee that the other thread\u00e2\u20ac\u2122s view of the update that happened in the <code>setName<\/code> method will be visible. In other words, there\u00e2\u20ac\u2122s no guarantee the two threads idea of \u00e2\u20ac\u0153before-ness\u00e2\u20ac\u009d will be the same; who knows what might have been reordered or cached?<\/p>\n<p>I\u00e2\u20ac\u2122m having trouble coming to terms with what seems like a gaping hole. Do you agree with this analysis? If so, how do you deal with it? Immutable object classes seem like an obvious choice, but will introduce lots of overhead in other parts of the code that need to manipulate the entities. A builder pattern gets awkward and verbose quickly, as it hoists all that complexity on to the caller.<\/p>\n<p>How do you handle situations like this? I\u00e2\u20ac\u2122d love to hear it.<\/p>\n<p>If you produce libraries, please include documentation on what is and isn\u00e2\u20ac\u2122t guaranteed w.r.t. async calls. -m<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pop quiz. Why is the following Java 8 code unsafe? UPDATE: this code is fine, see comments. Still good to think about, though. Entity e = new Entity(); e.setName(&#8220;my new entity&#8221;); persistanceLayer.put(e); To provide some\u00c2\u00a0context, Entity is a POJO representing something we want to store in a database. And persistanceLayer is an instance of a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[27,22],"tags":[],"class_list":["post-1158","post","type-post","status-publish","format-standard","hentry","category-languages","category-software"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8eo8l-iG","_links":{"self":[{"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/posts\/1158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/comments?post=1158"}],"version-history":[{"count":3,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/posts\/1158\/revisions"}],"predecessor-version":[{"id":1176,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/posts\/1158\/revisions\/1176"}],"wp:attachment":[{"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/media?parent=1158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/categories?post=1158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/tags?post=1158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}