{"action":"created","comment":{"url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/pulls/comments/31727784","id":31727784,"diff_hunk":"@@ -0,0 +1,123 @@\n+# 3. Connection settings and management\n+\n+## Using and configuring a connection pool\n+\n+To use a connection pool, pass a set of JDBC settings to an object called `ConnectionPool`. `Class.forName(String)` needs to be called because the JDBC driver is not loaded automatically.\n+\n+    import scalikejdbc._\n+    Class.forName(\"org.h2.Driver\")\n+    ConnectionPool.singleton(\"jdbc:h2:mem:db\", \"username\", \"password\")\n+\n+This is all you need for the DB connection configuration. Once the above has been run, you can connect to the DB:\n+\n+    val names: List[String] = DB readOnly { implicit session =>\n+      sql\"select name from members\".map(rs => rs.string(\"name\")).list.apply()\n+    }\n+\n+The reason why the connection settings are applied here is because the `DB.readOnly` internally behaves like this:\n+\n+    val names: List[String] = using(DB(ConnectionPool.borrow())) { db => \n+      db.readOnly { implicit session => \n+        sql\"select name from members\".map(rs => rs.string(\"name\")).list.apply()\n+      }\n+    }\n+\n+As you can see, `DB.readOnly` can save you from writing such a verbose code every time.\n+\n+## Connecting multiple data sources\n+\n+Below is an example of how to cope with the needs for connecting multiple data sources from a single application using ScalikeJDBC. The name of a data source must be specified in the first argument of the `add` method, whose type is actually `Any` but it is highly recommended to use a `Symbol`.\n+\n+    ConnectionPool.add('db1, \"jdbc:xxx:db1\", \"user\", \"pass\")\n+    ConnectionPool.add('db2, \"jdbc:xxx:db2\", \"user\", \"pass\")\n+\n+To use them, write as `NamedDB ('db1) readOnly {implicit session =>}` instead of `DB readOnly {implicit session =>}`.\n+\n+    NamedDB('db1) readOnly { implicit session =>\n+      // ...\n+    }\n+    \n+    NamedDB('db2) readOnly { implicit session =>\n+      // ...\n+    }\n+\n+The data source created by the `ConnectionPool.singleton(...)` method earlier has the name `'default`. This name can not be used for other data sources.\n+\n+\n+## Optional settings\n+\n+Settings other than JDBC url, user name and password are customizable using the `ConnectionPoolSettings`.\n+\n+    ConnectionPool.singleton(\"jdbc:h2:mem:db\", \"\", \"\", \n+      new ConnectionPoolSettings(initialSize = 20, maxSize = 50))\n+\n+Here are the list of those settings:\n+\n+<table>\n+<tr>\n+<td>Key</td><td>Content</td>\n+</tr>\n+<tr>\n+<td>initialSize</td><td>Minimum number of connections to be pooled</td>\n+</tr>\n+<tr>\n+<td>maxSize</td><td>Maximum number of connections to be pooled</td>\n+</tr>\n+<tr>\n+<td>validationQuery</td><td>SQL query to check to be connected</td>\n+</tr>\n+</table>\n+\n+\n+## Using a connection pool other than Commons DBCP\n+\n+The `ConnectionPool` method chooses [Commons DBCP](http://commons.apache.org/dbcp/) as the default implementation of the connection pool. Implementations that version 2.2.0 of ScalikeJDBC offers are: commons-dbcp, commons-dbcp2 and BoneCP.\n+\n+If you prefer other connection pool implementations, you can do so like this:\n+\n+    class MyConnectionPoolFactory extends ConnectionPoolFactory {\n+      def apply(url: String, user: String, password: String, settings: ConnectionPoolSettings) = {\n+        new MyConnectionPool(url, user, password)\n+      }\n+    }\n+    \n+    ConnectionPool.add('xxxx, url, user, password)(new MyConnectionPoolFactory)\n+\n+You can also register the connection pool to be used through `DataSource`. The following is an example of using HikariCP.\n+\n+\n+http://brettwooldridge.github.io/HikariCP/\n+\n+    val dataSource: DataSource = {\n+      val ds = new HikariDataSource()\n+      ds.setDataSourceClassName(dataSourceClassName)\n+      ds.addDataSourceProperty(\"url\", url)\n+      ds.addDataSourceProperty(\"user\", user)\n+      ds.addDataSourceProperty(\"password\", password)\n+      ds\n+    }\n+    ConnectionPool.singleton(new DataSourceConnectionPool(dataSource))\n+\n+## A thread-local connection\n+\n+You can reuse an instance of the DB class, which holds `java.sql.Connection`, as a thread local value. As long as you use a single DB instance, the same connection will be reused.\n+\n+    def init() = {\n+      val newDB = ThreadLocalDB.create(conn)\n+      newDB.begin()\n+    }\n+    // after `init` is called\n+    def action() = {\n+      val db = ThreadLocalDB.load()\n+      db readOnly { implicit session =>\n+        // ...\n+      }\n+    }\n+    def finalize() = {\n+      try { ThreadLocalDB.load().close() } catch { case e: Exception => }\n+    }\n+\n+That's it for the connection management. We will explain in the next section for transaction management.","path":"en/03_connection.md","position":120,"original_position":120,"commit_id":"6d381c47ced2ca39714e8ad553ae4d44d1e86171","original_commit_id":"6d381c47ced2ca39714e8ad553ae4d44d1e86171","user":{"login":"seratch","id":19658,"avatar_url":"https://avatars.githubusercontent.com/u/19658?v=3","gravatar_id":"","url":"https://api.github.com/users/seratch","html_url":"https://github.com/seratch","followers_url":"https://api.github.com/users/seratch/followers","following_url":"https://api.github.com/users/seratch/following{/other_user}","gists_url":"https://api.github.com/users/seratch/gists{/gist_id}","starred_url":"https://api.github.com/users/seratch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/seratch/subscriptions","organizations_url":"https://api.github.com/users/seratch/orgs","repos_url":"https://api.github.com/users/seratch/repos","events_url":"https://api.github.com/users/seratch/events{/privacy}","received_events_url":"https://api.github.com/users/seratch/received_events","type":"User","site_admin":false},"body":"explain about transaction management in the next section ですかね。explain for だと explain for だと説明する理由になってしまうと思います。","created_at":"2015-06-04T14:52:49Z","updated_at":"2015-06-04T14:52:49Z","html_url":"https://github.com/scalikejdbc/scalikejdbc-cookbook/pull/17#discussion_r31727784","pull_request_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/pulls/17","_links":{"self":{"href":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/pulls/comments/31727784"},"html":{"href":"https://github.com/scalikejdbc/scalikejdbc-cookbook/pull/17#discussion_r31727784"},"pull_request":{"href":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/pulls/17"}}},"pull_request":{"url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/pulls/17","id":36934272,"html_url":"https://github.com/scalikejdbc/scalikejdbc-cookbook/pull/17","diff_url":"https://github.com/scalikejdbc/scalikejdbc-cookbook/pull/17.diff","patch_url":"https://github.com/scalikejdbc/scalikejdbc-cookbook/pull/17.patch","issue_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/issues/17","number":17,"state":"open","locked":false,"title":"Translate 03_connection.md","user":{"login":"edvakf","id":28143,"avatar_url":"https://avatars.githubusercontent.com/u/28143?v=3","gravatar_id":"","url":"https://api.github.com/users/edvakf","html_url":"https://github.com/edvakf","followers_url":"https://api.github.com/users/edvakf/followers","following_url":"https://api.github.com/users/edvakf/following{/other_user}","gists_url":"https://api.github.com/users/edvakf/gists{/gist_id}","starred_url":"https://api.github.com/users/edvakf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edvakf/subscriptions","organizations_url":"https://api.github.com/users/edvakf/orgs","repos_url":"https://api.github.com/users/edvakf/repos","events_url":"https://api.github.com/users/edvakf/events{/privacy}","received_events_url":"https://api.github.com/users/edvakf/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-06-04T13:33:59Z","updated_at":"2015-06-04T14:52:49Z","closed_at":null,"merged_at":null,"merge_commit_sha":"2b47763fbd105164bf696841ef73270eeddece49","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/pulls/17/commits","review_comments_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/pulls/17/comments","review_comment_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/pulls/comments{/number}","comments_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/issues/17/comments","statuses_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/statuses/6d381c47ced2ca39714e8ad553ae4d44d1e86171","head":{"label":"edvakf:feature/03_connection","ref":"feature/03_connection","sha":"6d381c47ced2ca39714e8ad553ae4d44d1e86171","user":{"login":"edvakf","id":28143,"avatar_url":"https://avatars.githubusercontent.com/u/28143?v=3","gravatar_id":"","url":"https://api.github.com/users/edvakf","html_url":"https://github.com/edvakf","followers_url":"https://api.github.com/users/edvakf/followers","following_url":"https://api.github.com/users/edvakf/following{/other_user}","gists_url":"https://api.github.com/users/edvakf/gists{/gist_id}","starred_url":"https://api.github.com/users/edvakf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edvakf/subscriptions","organizations_url":"https://api.github.com/users/edvakf/orgs","repos_url":"https://api.github.com/users/edvakf/repos","events_url":"https://api.github.com/users/edvakf/events{/privacy}","received_events_url":"https://api.github.com/users/edvakf/received_events","type":"User","site_admin":false},"repo":{"id":35896988,"name":"scalikejdbc-cookbook","full_name":"edvakf/scalikejdbc-cookbook","owner":{"login":"edvakf","id":28143,"avatar_url":"https://avatars.githubusercontent.com/u/28143?v=3","gravatar_id":"","url":"https://api.github.com/users/edvakf","html_url":"https://github.com/edvakf","followers_url":"https://api.github.com/users/edvakf/followers","following_url":"https://api.github.com/users/edvakf/following{/other_user}","gists_url":"https://api.github.com/users/edvakf/gists{/gist_id}","starred_url":"https://api.github.com/users/edvakf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edvakf/subscriptions","organizations_url":"https://api.github.com/users/edvakf/orgs","repos_url":"https://api.github.com/users/edvakf/repos","events_url":"https://api.github.com/users/edvakf/events{/privacy}","received_events_url":"https://api.github.com/users/edvakf/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/edvakf/scalikejdbc-cookbook","description":"ScalikeJDBC Cookbook","fork":true,"url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook","forks_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/forks","keys_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/teams","hooks_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/hooks","issue_events_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/issues/events{/number}","events_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/events","assignees_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/assignees{/user}","branches_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/branches{/branch}","tags_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/tags","blobs_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/git/refs{/sha}","trees_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/statuses/{sha}","languages_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/languages","stargazers_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/stargazers","contributors_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/contributors","subscribers_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/subscribers","subscription_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/subscription","commits_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/commits{/sha}","git_commits_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/git/commits{/sha}","comments_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/comments{/number}","issue_comment_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/issues/comments{/number}","contents_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/contents/{+path}","compare_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/merges","archive_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/downloads","issues_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/issues{/number}","pulls_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/pulls{/number}","milestones_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/milestones{/number}","notifications_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/labels{/name}","releases_url":"https://api.github.com/repos/edvakf/scalikejdbc-cookbook/releases{/id}","created_at":"2015-05-19T17:16:56Z","updated_at":"2015-05-19T17:16:57Z","pushed_at":"2015-06-04T13:33:29Z","git_url":"git://github.com/edvakf/scalikejdbc-cookbook.git","ssh_url":"git@github.com:edvakf/scalikejdbc-cookbook.git","clone_url":"https://github.com/edvakf/scalikejdbc-cookbook.git","svn_url":"https://github.com/edvakf/scalikejdbc-cookbook","homepage":null,"size":232,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"scalikejdbc:master","ref":"master","sha":"a410e9776f5f8378244077c44be0082d64c75a21","user":{"login":"scalikejdbc","id":5978675,"avatar_url":"https://avatars.githubusercontent.com/u/5978675?v=3","gravatar_id":"","url":"https://api.github.com/users/scalikejdbc","html_url":"https://github.com/scalikejdbc","followers_url":"https://api.github.com/users/scalikejdbc/followers","following_url":"https://api.github.com/users/scalikejdbc/following{/other_user}","gists_url":"https://api.github.com/users/scalikejdbc/gists{/gist_id}","starred_url":"https://api.github.com/users/scalikejdbc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scalikejdbc/subscriptions","organizations_url":"https://api.github.com/users/scalikejdbc/orgs","repos_url":"https://api.github.com/users/scalikejdbc/repos","events_url":"https://api.github.com/users/scalikejdbc/events{/privacy}","received_events_url":"https://api.github.com/users/scalikejdbc/received_events","type":"Organization","site_admin":false},"repo":{"id":7403439,"name":"scalikejdbc-cookbook","full_name":"scalikejdbc/scalikejdbc-cookbook","owner":{"login":"scalikejdbc","id":5978675,"avatar_url":"https://avatars.githubusercontent.com/u/5978675?v=3","gravatar_id":"","url":"https://api.github.com/users/scalikejdbc","html_url":"https://github.com/scalikejdbc","followers_url":"https://api.github.com/users/scalikejdbc/followers","following_url":"https://api.github.com/users/scalikejdbc/following{/other_user}","gists_url":"https://api.github.com/users/scalikejdbc/gists{/gist_id}","starred_url":"https://api.github.com/users/scalikejdbc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scalikejdbc/subscriptions","organizations_url":"https://api.github.com/users/scalikejdbc/orgs","repos_url":"https://api.github.com/users/scalikejdbc/repos","events_url":"https://api.github.com/users/scalikejdbc/events{/privacy}","received_events_url":"https://api.github.com/users/scalikejdbc/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/scalikejdbc/scalikejdbc-cookbook","description":"ScalikeJDBC Cookbook","fork":false,"url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook","forks_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/forks","keys_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/keys{/key_id}","collaborators_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/teams","hooks_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/hooks","issue_events_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/issues/events{/number}","events_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/events","assignees_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/assignees{/user}","branches_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/branches{/branch}","tags_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/tags","blobs_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/git/refs{/sha}","trees_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/git/trees{/sha}","statuses_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/statuses/{sha}","languages_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/languages","stargazers_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/stargazers","contributors_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/contributors","subscribers_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/subscribers","subscription_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/subscription","commits_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/commits{/sha}","git_commits_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/git/commits{/sha}","comments_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/comments{/number}","issue_comment_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/issues/comments{/number}","contents_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/contents/{+path}","compare_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/compare/{base}...{head}","merges_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/merges","archive_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/downloads","issues_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/issues{/number}","pulls_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/pulls{/number}","milestones_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/milestones{/number}","notifications_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/labels{/name}","releases_url":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/releases{/id}","created_at":"2013-01-02T07:14:58Z","updated_at":"2015-05-16T14:59:15Z","pushed_at":"2015-06-04T13:33:59Z","git_url":"git://github.com/scalikejdbc/scalikejdbc-cookbook.git","ssh_url":"git@github.com:scalikejdbc/scalikejdbc-cookbook.git","clone_url":"https://github.com/scalikejdbc/scalikejdbc-cookbook.git","svn_url":"https://github.com/scalikejdbc/scalikejdbc-cookbook","homepage":null,"size":516,"stargazers_count":35,"watchers_count":35,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":11,"mirror_url":null,"open_issues_count":2,"forks":11,"open_issues":2,"watchers":35,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/pulls/17"},"html":{"href":"https://github.com/scalikejdbc/scalikejdbc-cookbook/pull/17"},"issue":{"href":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/issues/17"},"comments":{"href":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/issues/17/comments"},"review_comments":{"href":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/pulls/17/comments"},"review_comment":{"href":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/pulls/17/commits"},"statuses":{"href":"https://api.github.com/repos/scalikejdbc/scalikejdbc-cookbook/statuses/6d381c47ced2ca39714e8ad553ae4d44d1e86171"}}}}