CINXE.COM
Read Replicas | Ebean
<!doctype html> <html lang="en"> <head> <title>Read Replicas | Ebean</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="shortcut icon" href="/images/favicon.ico"> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Source+Sans+Pro|Ubuntu&display=swap"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous"> <link rel="stylesheet" href="/css/reset3.css"> <link rel="stylesheet" href="/css/site3.css"> <link rel="stylesheet" href="/css/pygments3.css"> </head> <body> <div id="main"> <div id="banner"> <header> <nav id="top"> <h1 id="breadcrumb"> <a class="nav-logo" href="/"><img src="/images/logo-200.png" height="35"></a> <a href="/docs">Documentation</a><span class="sep"> / </span><span class="last">Read Replicas</span> </h1> <ul> <li><a onclick="toggleTheme();" title="switch dark light theme"><i class="fas fa-adjust"></i></a></li> </ul> </nav> </header> </div> <div class="grid grid-docs"> <aside> <nav class="side"> <ul> <li class="nav0 "> <a href="/docs/getting-started">Getting started</a> </li> <li class="nav0 "> <a href="/docs/intro">Introduction</a> </li> <li class="nav0 active"> <a class="active" href="/docs">Documentation</a> <ul> <li class="nav1 "> <a href="/docs/best-practice">Best practice</a> </li> <li class="nav1 "> <a href="/docs/query">Query</a> </li> <li class="nav1 "> <a href="/docs/persist">Persist</a> </li> <li class="nav1 "> <a href="/docs/transactions">Transactions</a> </li> <li class="nav1 "> <a href="/docs/mapping">Mapping</a> </li> <li class="nav1 "> <a href="/docs/ddl-generation">DDL & Migrations</a> </li> <li class="nav1 "> <a href="/docs/logging">Logging</a> </li> <li class="nav1 "> <a href="/docs/testing">Testing</a> </li> <li class="nav1 active"> <a class="active" href="/docs/read-replicas">Read Replicas</a> </li> <li class="nav1 "> <a href="/docs/database">Database platforms</a> </li> <li class="nav1 "> <a href="/docs/multi-database">Multiple databases</a> </li> <li class="nav1 "> <a href="/docs/kotlin">Kotlin</a> </li> <li><a href="/docs/tuning">Tuning</a></li> <li class="nav1 "> <a href="/docs/features">Features</a> </li> </ul> </li> <li class="nav0 "> <a href="/support">Getting help</a> </li> <li class="nav0 "> <a target="_blank" href="/apidoc/13">API Javadoc</a> </li> <li class="nav0 "> <a href="/videos">Videos</a> </li> <li class="nav0 "> <a href="/docs/upgrading">Upgrading</a> </li> <li class="nav0 "> <a href="/releases">Releases</a> </li> </ul> </nav> </aside> <article> <form action="https://www.google.com/search" method="get" class="inline-form"> <input type="hidden" name="as_sitesearch" value="ebean.io"> <div id="page-search"> <div class="input-group"> <input class="frm" name="q" id="searchinput" type="text" placeholder="Search... (press 's' to focus)" data-placeholder-focus="Search... (use '↑', '↓' and '⏎' to select results)" data-placeholder-blur="Search... (press 's' to focus)" autocomplete="off"> <div class="input-group-btn"> <button class="frm" type="submit"><i class="fas fa-search"></i></button> </div> </div> <div id="page-search-results" style="display: none;"> <ul id="search-results-container" class="search-results"><li class=" active"><a href="/docs" title="Docs"><span style="color:#777;">Docs</span> Documentation </a></li><li class=""><small style="color:#999;">And 101 more...</small></li></ul> </div> </div> </form> <h2 id="transactional">Read Replicas</h2> <p> Ebean supports configuring a second DataSource [connect pool] that points to a "Read replica". The DataSource is <em>readOnly = true</em> and <em>autoCommit=true</em>. </p> <div class="syntax java"><div class="highlight"><pre><span></span> </pre></div> </div> <h2 id="aurora">Amazon AWS Aurora</h2> <p> Amazon AWS Aurora provides 2 connection endpoints with one endpoint for the "Master" [writer] and a second endpoint for the "Read replica" [read only] instance(s). </p> <div class="syntax java"><div class="highlight"><pre><span></span> </pre></div> </div> <h2 id="usingMaster">Using the "Master"</h2> <p> When we have an explicit transaction via <em>@Transactional</em> on a method or using explicitly <code>database.beginTransaction()</code> - then this transaction will use the "Master". </p> <p> When we use database.save() without an explicit transaction, then a transaction will be used implicitly and this will use the "Master". </p> <h2 id="queryUseMaster">Query.useMaster()</h2> <p> If we have a query that is executed outside any explicit transaction, and it will fetch data (not a bulk update query or bulk delete query) then by default it will use the "Read Replica". </p> <p> To force a query to use the "Master" we use <code>Query.useMaster()</code> like: </p> <div class="syntax java"><div class="highlight"><pre><span></span><span class="k">var</span> <span class="n">customers</span> <span class="o">=</span> <span class="k">new</span> <span class="n">QCustomer</span><span class="o">()</span> <span class="o">.</span><span class="na field">status</span><span class="o">.</span><span class="na">eq</span><span class="o">(</span><span class="n">Status</span><span class="o">.</span><span class="na">NEW</span><span class="o">)</span> <span class="o">.</span><span class="na">useMaster</span><span class="o">()</span> <span class="c1">// Use the "Master" database</span> <span class="o">.</span><span class="na">findList</span><span class="o">();</span> </pre></div> </div> <h2 id="queryUseReadReplica">Using the Read Replica</h2> <p> If we have a query that is executed outside any explicit transaction, and it will fetch data (it is not a bulk update query or bulk delete query) then by default it will use the "Read Replica". </p> <div class="syntax java"><div class="highlight"><pre><span></span><span class="c1">// a query executed outside of any explicit transaction</span> <span class="c1">// ... will use the replica</span> <span class="k">var</span> <span class="n">customers</span> <span class="o">=</span> <span class="k">new</span> <span class="n">QCustomer</span><span class="o">()</span> <span class="o">.</span><span class="na field">status</span><span class="o">.</span><span class="na">eq</span><span class="o">(</span><span class="n">Status</span><span class="o">.</span><span class="na">NEW</span><span class="o">)</span> <span class="o">.</span><span class="na">findList</span><span class="o">();</span> </pre></div> </div> <h2 id="transactionReadOnly">@Transaction(readOnly=true)</h2> <p> When a method is annotated with <code>@Transaction(readOnly=true)</code>, then this will create a read only transaction using the "Read Replica". </p> <div class="syntax java"><div class="highlight"><pre><span></span><span class="nd">@Transaction</span><span class="o">(</span><span class="nx">readOnly</span><span class="o">=</span><span class="kc">true</span><span class="o">)</span> <span class="kt">void</span> <span class="nf">myMethod</span><span class="o">()</span> <span class="o">{</span> <span class="c1">// do stuff with a transaction using the Read replica</span> <span class="o">}</span> </pre></div> </div> <nav class="next"> <p class="edit-page"> <a href="https://github.com/ebean-orm/website-source/blob/master/docs/read-replicas/index.html"><i class="fab fa-github"></i> Edit Page</a> </p> <p class="next"> <a href="/docs/monitoring" class="btn btn-info">Next: Batch</a> </p> </nav> </article> </div> </div> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script> <script src="/js/site3.js"></script> <script src="/js/search3.js"></script> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-75181644-1"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-75181644-1'); </script> </body> </html>