-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatabase.html
255 lines (237 loc) · 17.4 KB
/
database.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Database Configuration — Slick 3.1.0-M1 documentation</title>
<link rel="stylesheet" href="_static/slick.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '3.1.0-M1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="shortcut icon" href="_static/favicon.ico"/>
<link rel="top" title="Slick 3.1.0-M1 documentation" href="index.html" />
<link rel="next" title="Database I/O Actions" href="dbio.html" />
<link rel="prev" title="Getting Started" href="gettingstarted.html" />
<script type="text/javascript">
if(window.location.host == 'slick.typesafe.com'){
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-23127719-3']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
}
</script>
</head>
<body>
<div class="header-wrapper">
<div class="header">
<div class="rel">
<span class="github"><a href="https://github.com/slick/slick/edit/master/slick/src/sphinx/database.rst">edit this page on github</a><span>
|
<a href="gettingstarted.html" title="Getting Started"
accesskey="P">previous</a> |
<a href="dbio.html" title="Database I/O Actions"
accesskey="N">next</a> |
<a href="genindex.html" title="General Index"
accesskey="I">index</a>
</div>
<div class="headertitle"><a href="index.html">
<span class="logo"><img class="logo" src="_static/slick-logo.png" alt="Slick"/></span>
3.1.0-M1 manual
</a></div>
</div>
</div>
<div class="content-wrapper">
<div class="content">
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="database-configuration">
<h1>Database Configuration<a class="headerlink" href="#database-configuration" title="Permalink to this headline">¶</a></h1>
<p>You can tell Slick how to connect to the JDBC database of your choice by
creating a <a class="reference external" href="api/index.html#slick.jdbc.JdbcBackend@Database:Database">Database</a> object,
which encapsulates the information. There are several
<a class="reference external" href="api/index.html#slick.jdbc.JdbcBackend$DatabaseFactoryDef">factory methods</a>
on <cite>slick.jdbc.JdbcBackend.Database</cite> that you can use depending on what
connection data you have available.</p>
<div class="section" id="using-typesafe-config">
<h2>Using Typesafe Config<a class="headerlink" href="#using-typesafe-config" title="Permalink to this headline">¶</a></h2>
<p>The prefered way to configure database connections is through <a class="reference external" href="https://github.com/typesafehub/config">Typesafe Config</a> in your
<tt class="docutils literal"><span class="pre">application.conf</span></tt>, which is also used by <a class="reference external" href="https://playframework.com/">Play</a> and <a class="reference external" href="http://akka.io/">Akka</a> for their configuration.</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">mydb</span> <span class="k">=</span> <span class="o">{</span>
<span class="n">dataSourceClass</span> <span class="k">=</span> <span class="s">"org.postgresql.ds.PGSimpleDataSource"</span>
<span class="n">properties</span> <span class="k">=</span> <span class="o">{</span>
<span class="n">databaseName</span> <span class="k">=</span> <span class="s">"mydb"</span>
<span class="n">user</span> <span class="k">=</span> <span class="s">"myuser"</span>
<span class="n">password</span> <span class="k">=</span> <span class="s">"secret"</span>
<span class="o">}</span>
<span class="n">numThreads</span> <span class="k">=</span> <span class="mi">10</span>
<span class="o">}</span>
</pre></div>
</div>
<p>Such a configuration can be loaded with <cite>Database.forConfig</cite> (see the
<a class="reference external" href="api/index.html#slick.jdbc.JdbcBackend$DatabaseFactoryDef@forConfig(String,Config,Driver,ClassLoader):Database">API documentation</a>
of this method for details on the configuration parameters).</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">val</span> <span class="n">db</span> <span class="k">=</span> <span class="nc">Database</span><span class="o">.</span><span class="n">forConfig</span><span class="o">(</span><span class="s">"mydb"</span><span class="o">)</span>
</pre></div>
</div>
</div>
<div class="section" id="using-a-jdbc-url">
<span id="index-0"></span><h2>Using a JDBC URL<a class="headerlink" href="#using-a-jdbc-url" title="Permalink to this headline">¶</a></h2>
<p>You can pass a JDBC URL to
<a class="reference external" href="api/index.html#slick.jdbc.JdbcBackend$DatabaseFactoryDef@forURL(String,String,String,Properties,String,AsyncExecutor,Boolean,ClassLoader):DatabaseDef">forURL</a>.
(see your database’s JDBC driver’s documentation for the correct URL syntax).</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">val</span> <span class="n">db</span> <span class="k">=</span> <span class="nc">Database</span><span class="o">.</span><span class="n">forURL</span><span class="o">(</span><span class="s">"jdbc:h2:mem:test1;DB_CLOSE_DELAY=-1"</span><span class="o">,</span> <span class="n">driver</span><span class="o">=</span><span class="s">"org.h2.Driver"</span><span class="o">)</span>
</pre></div>
</div>
<p>Here we are connecting to a new, empty, in-memory H2 database called <tt class="docutils literal"><span class="pre">test1</span></tt> and keep it resident
until the JVM ends (<tt class="docutils literal"><span class="pre">DB_CLOSE_DELAY=-1</span></tt>, which is H2 specific).</p>
</div>
<div class="section" id="using-a-datasource">
<span id="index-1"></span><h2>Using a DataSource<a class="headerlink" href="#using-a-datasource" title="Permalink to this headline">¶</a></h2>
<p>You can pass a <a class="reference external" href="http://docs.oracle.com/javase/7/docs/api/javax/sql/DataSource.html">DataSource</a> object to
<a class="reference external" href="api/index.html#slick.jdbc.JdbcBackend$DatabaseFactoryDef@forDataSource(DataSource,AsyncExecutor,Boolean):DatabaseDef">forDataSource</a>.
If you got it from the connection pool of your application framework, this plugs the pool into Slick.</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">val</span> <span class="n">db</span> <span class="k">=</span> <span class="nc">Database</span><span class="o">.</span><span class="n">forDataSource</span><span class="o">(</span><span class="n">dataSource</span><span class="k">:</span> <span class="kt">javax.sql.DataSource</span><span class="o">)</span>
</pre></div>
</div>
</div>
<div class="section" id="using-a-jndi-name">
<span id="index-2"></span><h2>Using a JNDI Name<a class="headerlink" href="#using-a-jndi-name" title="Permalink to this headline">¶</a></h2>
<p>If you are using <a class="reference external" href="http://en.wikipedia.org/wiki/JNDI">JNDI</a> you can pass a JNDI name to
<a class="reference external" href="api/index.html#slick.jdbc.JdbcBackend$DatabaseFactoryDef@forName(String,AsyncExecutor):DatabaseDef">forName</a>
under which a <a class="reference external" href="http://docs.oracle.com/javase/7/docs/api/javax/sql/DataSource.html">DataSource</a> object can be looked up.</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">val</span> <span class="n">db</span> <span class="k">=</span> <span class="nc">Database</span><span class="o">.</span><span class="n">forName</span><span class="o">(</span><span class="n">jndiName</span><span class="k">:</span> <span class="kt">String</span><span class="o">)</span>
</pre></div>
</div>
</div>
<div class="section" id="database-thread-pool">
<span id="index-3"></span><h2>Database thread pool<a class="headerlink" href="#database-thread-pool" title="Permalink to this headline">¶</a></h2>
<p>Every <tt class="docutils literal"><span class="pre">Database</span></tt> contains an <a class="reference external" href="api/index.html#slick.util.AsyncExecutor">AsyncExecutor</a> that manages the thread pool
for asynchronous execution of Database I/O Actions. Its size is the main parameter to tune for the best
performance of the <tt class="docutils literal"><span class="pre">Database</span></tt> object. It should be set to the value that you would use for the
size of the <em>connection pool</em> in a traditional, blocking application (see <a class="reference external" href="https://github.com/brettwooldridge/HikariCP/wiki/About-Pool-Sizing">About Pool Sizing</a>
in the <a class="reference external" href="http://brettwooldridge.github.io/HikariCP/">HikariCP</a> documentation for further information). When using
<a class="reference external" href="api/index.html#slick.jdbc.JdbcBackend$DatabaseFactoryDef@forConfig(String,Config,Driver,ClassLoader):Database">Database.forConfig</a>,
the thread pool is configured directly in the external configuration file together with the connection
parameters. If you use any other factory method to get a <tt class="docutils literal"><span class="pre">Database</span></tt>, you can either use a default
configuration or specify a custom AsyncExecutor:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">val</span> <span class="n">db</span> <span class="k">=</span> <span class="nc">Database</span><span class="o">.</span><span class="n">forURL</span><span class="o">(</span><span class="s">"jdbc:h2:mem:test1;DB_CLOSE_DELAY=-1"</span><span class="o">,</span> <span class="n">driver</span><span class="o">=</span><span class="s">"org.h2.Driver"</span><span class="o">,</span>
<span class="n">executor</span> <span class="k">=</span> <span class="nc">AsyncExecutor</span><span class="o">(</span><span class="s">"test1"</span><span class="o">,</span> <span class="n">numThreads</span><span class="k">=</span><span class="mi">10</span><span class="o">,</span> <span class="n">queueSize</span><span class="k">=</span><span class="mi">1000</span><span class="o">))</span>
</pre></div>
</div>
</div>
<div class="section" id="connection-pools">
<span id="index-4"></span><h2>Connection pools<a class="headerlink" href="#connection-pools" title="Permalink to this headline">¶</a></h2>
<p>When using a connection pool (which is always recommended in production environments) the <em>minimum</em>
size of the <em>connection pool</em> should also be set to at least the same size. The <em>maximum</em> size of
the <em>connection pool</em> can be set much higher than in a blocking application. Any connections beyond
the size of the <em>thread pool</em> will only be used when other connections are required to keep a
database session open (e.g. while waiting for the result from an asynchronous computation in the
middle of a transaction) but are not actively doing any work on the database.</p>
<p>Note that reasonable defaults for the connection pool sizes are calculated from the thread pool
size when using
<a class="reference external" href="api/index.html#slick.jdbc.JdbcBackend$DatabaseFactoryDef@forConfig(String,Config,Driver,ClassLoader):Database">Database.forConfig</a>.</p>
<p>Slick uses <em>prepared</em> statements wherever possible but it does not cache them on its own. You
should therefore enable prepared statement caching in the connection pool’s configuration.</p>
</div>
<div class="section" id="databaseconfig">
<h2>DatabaseConfig<a class="headerlink" href="#databaseconfig" title="Permalink to this headline">¶</a></h2>
<p>On top of the configuration syntax for <tt class="docutils literal"><span class="pre">Database</span></tt>, there is another layer in the form of
<a class="reference external" href="api/index.html#slick.backend.DatabaseConfig">DatabaseConfig</a> which allows you to configure a Slick driver plus a
matching <tt class="docutils literal"><span class="pre">Database</span></tt> together. This makes it easy to abstract over different kinds of
database systems by simply changing a configuration file.</p>
<p>Here is a typical <tt class="docutils literal"><span class="pre">DatabaseConfig</span></tt> with a Slick driver object in <tt class="docutils literal"><span class="pre">driver</span></tt> and the database
configuration in <tt class="docutils literal"><span class="pre">db</span></tt>:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">tsql</span> <span class="o">{</span>
<span class="n">driver</span> <span class="k">=</span> <span class="s">"slick.driver.H2Driver$"</span>
<span class="n">db</span> <span class="o">{</span>
<span class="n">connectionPool</span> <span class="k">=</span> <span class="n">disabled</span>
<span class="n">driver</span> <span class="k">=</span> <span class="s">"org.h2.Driver"</span>
<span class="n">url</span> <span class="k">=</span> <span class="s">"jdbc:h2:mem:tsql1;INIT=runscript from 'src/main/resources/create-schema.sql'"</span>
<span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sidebar">
<h3>Search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<h3 style="margin-top: 1.5em;">Table Of Contents</h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="introduction.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="supported-databases.html">Supported Databases</a></li>
<li class="toctree-l1"><a class="reference internal" href="gettingstarted.html">Getting Started</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="">Database Configuration</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#using-typesafe-config">Using Typesafe Config</a></li>
<li class="toctree-l2"><a class="reference internal" href="#using-a-jdbc-url">Using a JDBC URL</a></li>
<li class="toctree-l2"><a class="reference internal" href="#using-a-datasource">Using a DataSource</a></li>
<li class="toctree-l2"><a class="reference internal" href="#using-a-jndi-name">Using a JNDI Name</a></li>
<li class="toctree-l2"><a class="reference internal" href="#database-thread-pool">Database thread pool</a></li>
<li class="toctree-l2"><a class="reference internal" href="#connection-pools">Connection pools</a></li>
<li class="toctree-l2"><a class="reference internal" href="#databaseconfig">DatabaseConfig</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="dbio.html">Database I/O Actions</a></li>
<li class="toctree-l1"><a class="reference internal" href="schemas.html">Schemas</a></li>
<li class="toctree-l1"><a class="reference internal" href="queries.html">Queries</a></li>
<li class="toctree-l1"><a class="reference internal" href="code-generation.html">Schema Code Generation</a></li>
<li class="toctree-l1"><a class="reference internal" href="userdefined.html">User-Defined Features</a></li>
<li class="toctree-l1"><a class="reference internal" href="sql.html">Plain SQL Queries</a></li>
<li class="toctree-l1"><a class="reference internal" href="orm-to-slick.html">Coming from ORM to Slick</a></li>
<li class="toctree-l1"><a class="reference internal" href="sql-to-slick.html">Coming from SQL to Slick</a></li>
<li class="toctree-l1"><a class="reference internal" href="upgrade.html">Upgrade Guides</a></li>
<li class="toctree-l1"><a class="reference internal" href="extensions.html">Slick Extensions</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="testkit.html">Slick TestKit</a></li>
</ul>
</div>
<div class="clearer"></div>
</div>
</div>
<div class="footer-wrapper">
<div class="footer">
<div class="left">
<a href="gettingstarted.html" title="Getting Started"
>previous</a> |
<a href="dbio.html" title="Database I/O Actions"
>next</a> |
<a href="genindex.html" title="General Index"
>index</a>
</div>
<div class="right">
<div class="footer">
© Copyright 2011-2015 Typesafe, Inc.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b3.
</div>
</div>
<div class="clearer"></div>
</div>
</div>
</body>
</html>