{"id":353,"date":"2012-04-11T13:34:29","date_gmt":"2012-04-11T20:34:29","guid":{"rendered":"http:\/\/www.dragishak.com\/?p=353"},"modified":"2012-04-11T13:40:46","modified_gmt":"2012-04-11T20:40:46","slug":"setting-mysql-load-balancing-in-play-and-scala","status":"publish","type":"post","link":"https:\/\/www.dragishak.com\/?p=353","title":{"rendered":"Setting MySQL Load Balancing in Play and Scala"},"content":{"rendered":"<p>In my <a title=\"MySQL Master\/Slave Load Balancing with JPA and Spring\" href=\"http:\/\/www.dragishak.com\/?p=307\">previous post<\/a> I showed a way to set MySQL load balancing with Java and Spring.<\/p>\n<p>To make your queries go to the slave database, you need to set connection to read-only. In <a title=\"Play Framework\" href=\"http:\/\/www.playframework.org\/\">Play Framework 2.0<\/a> with <a title=\"Scala\" href=\"http:\/\/www.scala-lang.org\/\">Scala<\/a>, this can&#8217;t be simpler:<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\ndef withReadOnlyConnection&#x5B;A](block: Connection =&gt; A)(implicit app: Application): A = {\r\n    DB.withConnection {\r\n      connection =&gt;\r\n        try {\r\n          connection.setAutoCommit(false)\r\n          connection.setReadOnly(true)\r\n          block(connection)\r\n        } finally {\r\n          connection.setReadOnly(false)\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Now you can just wrap your database code as:<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\nwithReadOnlyConnection&#x5B;Option&#x5B;User]] {\r\n   implicit connection: Connection =&gt;\r\n\r\n      SQL(....)\r\n      etc, etc.\r\n}\r\n<\/pre>\n<p>For additional info on Play and Anorm see: <a title=\"Anorm, simple SQL data access\" href=\"http:\/\/www.playframework.org\/documentation\/2.0\/ScalaAnorm\">Anorm, simple SQL data access<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my previous post I showed a way to set MySQL load balancing with Java and Spring. To make your queries go to the slave database, you need to set connection to read-only. In Play Framework 2.0 with Scala, this can&#8217;t be simpler: def withReadOnlyConnection&#x5B;A](block: Connection =&gt; A)(implicit app: Application): A = { DB.withConnection { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[52,58,59],"class_list":["post-353","post","type-post","status-publish","format-standard","hentry","category-software","tag-mysql","tag-playframework","tag-scala"],"_links":{"self":[{"href":"https:\/\/www.dragishak.com\/index.php?rest_route=\/wp\/v2\/posts\/353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dragishak.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dragishak.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dragishak.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dragishak.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=353"}],"version-history":[{"count":14,"href":"https:\/\/www.dragishak.com\/index.php?rest_route=\/wp\/v2\/posts\/353\/revisions"}],"predecessor-version":[{"id":368,"href":"https:\/\/www.dragishak.com\/index.php?rest_route=\/wp\/v2\/posts\/353\/revisions\/368"}],"wp:attachment":[{"href":"https:\/\/www.dragishak.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dragishak.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dragishak.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}