{"id":828,"date":"2013-12-11T09:51:46","date_gmt":"2013-12-11T16:51:46","guid":{"rendered":"http:\/\/10kdev.net\/?p=828"},"modified":"2013-12-11T09:58:36","modified_gmt":"2013-12-11T16:58:36","slug":"threading-some-queries","status":"publish","type":"post","link":"http:\/\/10kdev.net\/?p=828","title":{"rendered":"Threading some queries"},"content":{"rendered":"<p>I set up a quick multi-thread query test for a complex model entity made of several hibernate table entities. What I was trying to accomplish was a faster retrieval by running the queries in parallel. Spring JPARepository is the underlying querying mechanism. Here&#8217;s the higher level entity:<\/p>\n<pre class=\"brush: java; tab-size: 4; title: ; notranslate\" title=\"\">\r\n\/*\r\n* LookupEntity is a composite pojo of TableX instances,\r\n* TableX instances are just any jpa\/hibernate entities\r\n* that share the same id (for simplicity)\r\n*\/\r\npublic LookupEntity {\r\nprivate Integer id;\r\nprivate Table1 table1;\r\nprivate Table2 table2;\r\nprivate Table3 table3;\r\n\r\npublic LookupEntity(){}\r\n\r\npublic Table1 getTable1() {return table1;}\r\npublic void setTable1(Table1 table1) {this.table1 = table1;}\r\n\r\npublic Table2 getTable2() {return table2;}\r\npublic void setTable2(Table2 table1) {this.table2 = table2;}\r\n\r\npublic Table3 getTable3() {return table3;}\r\npublic void setTable3(Table3 table3) {this.table3 = table3;}\r\n}\r\n<\/pre>\n<p>In my service method is the threading test. I wrote a quick @TimeMethod annotation and point cut to time the method:<\/p>\n<pre class=\"brush: java; smart-tabs: true; tab-size: 4; title: ; notranslate\" title=\"\">\r\n\/*\r\n* getLookupThreaded will run TableX repository methods to populate\r\n* the TableX objects in a LookupEntity\r\n*\/\r\n@TimeMethod\r\npublic LookupEntity getLookupThreaded(final Integer id) {\r\nfinal LookupEntity lookupEntity = new LookupEntity(id);\r\n\r\nRunnable r1 = new Runnable() {\r\npublic void run() {\r\nlookupEntity.setTable1(table1Repository.findOne(id));\r\n};\r\n};\r\nRunnable r2 = new Runnable() {\r\npublic void run() {\r\nlookupEntity.setTable2(table2Repository.findOne(id));\r\n};\r\n};\r\nRunnable r3 = new Runnable() {\r\npublic void run() {\r\nlookupEntity.setTable3(table2Repository.findOne(id));\r\n};\r\n};\r\n\r\nThread t1 = new Thread(r1);\r\nThread t2 = new Thread(r2);\r\nThread t3 = new Thread(r3);\r\n\r\nt1.start();\r\nt2.start();\r\nt3.start();\r\n\r\nreturn lookupEntity;\r\n}\r\n<\/pre>\n<p>This worked pretty well but would still need more scaling management with the threads if a server was going to hit the method 10k times. But what I found was more weaknesses in my table designs and indexing as opposed to coding deficiencies after turning on jpa and hibernate logging. Personally, I avoid threading and non injection container singletons like the plague unless absolutely necessary because they introduce, many times, unnecessary complexity. But if I were to write some low connection count analysis procedures; maybe doing big data style calculations in a job, I might consider threading to save time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I set up a quick multi-thread query test for a complex model entity made of several hibernate table entities. What I was trying to accomplish was a faster retrieval by running the queries in parallel. Spring JPARepository is the underlying querying mechanism. Here&#8217;s the higher level entity: In my service method is the threading test. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/828"}],"collection":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=828"}],"version-history":[{"count":5,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/828\/revisions"}],"predecessor-version":[{"id":833,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/828\/revisions\/833"}],"wp:attachment":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=828"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}