| 1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 4 | <head> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 6 | <title>Replication Usage Examples</title> |
| 7 | <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> |
| 8 | <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> |
| 9 | <link rel="start" href="index.html" title="Getting Started with the Oracle Berkeley DB SQL APIs" /> |
| 10 | <link rel="up" href="sqlrep.html" title="Chapter 4. Using Replication with the SQL API" /> |
| 11 | <link rel="prev" href="repstatistics.html" title="Displaying Replication Statistics" /> |
| 12 | <link rel="next" href="admin.html" title="Chapter 5. Administrating Berkeley DB SQL Databases" /> |
| 13 | </head> |
| 14 | <body> |
| 15 | <div xmlns="" class="navheader"> |
| 16 | <div class="libver"> |
| 17 | <p>Library Version 11.2.5.3</p> |
| 18 | </div> |
| 19 | <table width="100%" summary="Navigation header"> |
| 20 | <tr> |
| 21 | <th colspan="3" align="center">Replication Usage Examples</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="repstatistics.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 4. Using Replication with the SQL API</th> |
| 26 | <td width="20%" align="right"> <a accesskey="n" href="admin.html">Next</a></td> |
| 27 | </tr> |
| 28 | </table> |
| 29 | <hr /> |
| 30 | </div> |
| 31 | <div class="sect1" lang="en" xml:lang="en"> |
| 32 | <div class="titlepage"> |
| 33 | <div> |
| 34 | <div> |
| 35 | <h2 class="title" style="clear: both"><a id="rep_usageexamples"></a>Replication Usage Examples</h2> |
| 36 | </div> |
| 37 | </div> |
| 38 | </div> |
| 39 | <div class="toc"> |
| 40 | <dl> |
| 41 | <dt> |
| 42 | <span class="sect2"> |
| 43 | <a href="rep_usageexamples.html#rep_ex1">Example 1: Distributed Read at 3 Sites</a> |
| 44 | </span> |
| 45 | </dt> |
| 46 | <dt> |
| 47 | <span class="sect2"> |
| 48 | <a href="rep_usageexamples.html#rep_ex2">Example 2: 2-Site Failover</a> |
| 49 | </span> |
| 50 | </dt> |
| 51 | </dl> |
| 52 | </div> |
| 53 | <p> |
| 54 | In this section we provide two examples of using |
| 55 | replication with BDB SQL. The first example shows a |
| 56 | typical startup process. The second demonstrates master |
| 57 | site failover. |
| 58 | </p> |
| 59 | <div class="sect2" lang="en" xml:lang="en"> |
| 60 | <div class="titlepage"> |
| 61 | <div> |
| 62 | <div> |
| 63 | <h3 class="title"><a id="rep_ex1"></a>Example 1: Distributed Read at 3 Sites</h3> |
| 64 | </div> |
| 65 | </div> |
| 66 | </div> |
| 67 | <p> |
| 68 | This example shows how a typical replication group |
| 69 | startup sequence is performed. It initially populates |
| 70 | the master, then starts two replicas so that read |
| 71 | operations can be distributed. Then it shows what |
| 72 | happens when an attempt is made to write to a replica. |
| 73 | </p> |
| 74 | <p> |
| 75 | Site 1: |
| 76 | </p> |
| 77 | <pre class="programlisting"># Start initial master. |
| 78 | # univ.db does not yet exist. |
| 79 | dbsql univ.db |
| 80 | pragma replication_local_site="site1:7000"; |
| 81 | pragma replication_initial_master=ON; |
| 82 | pragma replication=ON; |
| 83 | |
| 84 | # Create and populate university and country tables. |
| 85 | .read university.sql </pre> |
| 86 | <p> |
| 87 | Site 2: |
| 88 | </p> |
| 89 | <pre class="programlisting"># Start first replica. |
| 90 | # univ.db does not yet exist. |
| 91 | dbsql univ.db |
| 92 | pragma replication_local_site="site2:7001"; |
| 93 | pragma replication_remote_site="site1:7000"; |
| 94 | pragma replication=ON; </pre> |
| 95 | <p> |
| 96 | Site 3: |
| 97 | </p> |
| 98 | <pre class="programlisting"># Start second replica. |
| 99 | # univ.db does not yet exist. |
| 100 | dbsql univ.db |
| 101 | pragma replication_local_site="site3:7002"; |
| 102 | pragma replication_remote_site="site1:7000"; |
| 103 | pragma replication=ON; </pre> |
| 104 | <p> |
| 105 | Site 1: |
| 106 | </p> |
| 107 | <pre class="programlisting"> # Perform some writes and reads on master. |
| 108 | insert into country values ("Greenland","gl", 0, 0, 0, 2); |
| 109 | insert into university values (26, "University College London", |
| 110 | "ucl.edu", "uk", "Europe", 18, 39, 47, 30); |
| 111 | select * from country where abbr = "gl"; |
| 112 | update country set top_1000 = 1 where abbr = "gl"; </pre> |
| 113 | <p> |
| 114 | Site 2: |
| 115 | </p> |
| 116 | <pre class="programlisting"> # Perform some reads on first replica. |
| 117 | select * from university where region = "Europe"; |
| 118 | select count(*) from country where top_100 > 0; |
| 119 | |
| 120 | # Attempt to write on first replica. |
| 121 | insert into country values ("Antarctica","an", 0, 0, 0, 0); |
| 122 | .../univ.db: DBcursor->put: attempt to modify a read-only database |
| 123 | Error: attempt to write to a readonly database</pre> |
| 124 | </div> |
| 125 | <div class="sect2" lang="en" xml:lang="en"> |
| 126 | <div class="titlepage"> |
| 127 | <div> |
| 128 | <div> |
| 129 | <h3 class="title"><a id="rep_ex2"></a>Example 2: 2-Site Failover</h3> |
| 130 | </div> |
| 131 | </div> |
| 132 | </div> |
| 133 | <p> |
| 134 | This example demonstrates failover of the master from |
| 135 | one site to another. It shows how a failed site can |
| 136 | rejoin the replication group, and it shows that there |
| 137 | is a window of time during which write operations |
| 138 | cannot be performed for the replication group. Finally, |
| 139 | it shows how to check the master's location. |
| 140 | </p> |
| 141 | <p> |
| 142 | Site 1: |
| 143 | </p> |
| 144 | <pre class="programlisting"># Start initial master. |
| 145 | # quote.db does not yet exist. |
| 146 | dbsql quote.db |
| 147 | pragma replication_local_site="site1:7000"; |
| 148 | pragma replication_initial_master=ON; |
| 149 | pragma replication=ON; |
| 150 | |
| 151 | # Create stock quote application table. |
| 152 | create table stock_quote (company_name text(40), price real);</pre> |
| 153 | <p> |
| 154 | Site 2: |
| 155 | </p> |
| 156 | <pre class="programlisting"># Start replica. |
| 157 | # quote.db does not yet exist. |
| 158 | dbsql quote.db |
| 159 | pragma replication_local_site="site2:7001"; |
| 160 | pragma replication_remote_site="site1:7000"; |
| 161 | pragma replication=ON; </pre> |
| 162 | <p> |
| 163 | Site 1: |
| 164 | </p> |
| 165 | <pre class="programlisting"> # Perform some writes on master. |
| 166 | insert into stock_quote values ("General Electric", 20.25); |
| 167 | insert into stock_quote values ("Nabisco", 24.75); |
| 168 | insert into stock_quote values ("United Healthcare", 31.00); |
| 169 | update stock_quote set price=25.25 where company_name = "Nabisco";</pre> |
| 170 | <p> |
| 171 | Site 2: |
| 172 | </p> |
| 173 | <pre class="programlisting"> # Perform some reads on replica. |
| 174 | select * from stock_quote where price < 30.00; |
| 175 | select price from stock_quote where |
| 176 | company_name = "General Electric";</pre> |
| 177 | <p> |
| 178 | Site 1: |
| 179 | </p> |
| 180 | <pre class="programlisting"> # Stop the initial master. |
| 181 | .exit </pre> |
| 182 | <p> |
| 183 | Site 2: |
| 184 | </p> |
| 185 | <pre class="programlisting"> ########## |
| 186 | ### Now the remaining site does not accept write operations until |
| 187 | ### the other site rejoins the replication group. |
| 188 | ########## |
| 189 | insert into stock_quote values ("Prudential", 17.25); |
| 190 | .../quote.db: DBcursor->put: attempt to modify a read-only database |
| 191 | Error: attempt to write to a readonly database</pre> |
| 192 | <p> |
| 193 | Site 1: |
| 194 | </p> |
| 195 | <pre class="programlisting"># Restart site, will rejoin replication group. |
| 196 | dbsql quote.db |
| 197 | # The earlier replication=ON causes replication to be |
| 198 | # automatically started. This site may or may not become master |
| 199 | # after rejoining replication group. Check status of site's |
| 200 | # startup and determine whether it is a master or a replica. |
| 201 | .stat :rep: |
| 202 | Replication summary statistics |
| 203 | Environment configured as a replication master |
| 204 | 1/49056 Maximum permanent LSN |
| 205 | 2 Number of environments in the replication group |
| 206 | 0 Number of failed message sends |
| 207 | 0 Number of messages ignored due to pending recovery |
| 208 | 0 Number of log records currently queued |
| 209 | |
| 210 | # Assuming this site became master, perform some writes. |
| 211 | # If this site is not the master, these writes will not |
| 212 | # succeed and must be performed at the other site. |
| 213 | insert into stock_quote values ("Raytheon", 9.25); |
| 214 | insert into stock_quote values ("Cadbury", 7.75); </pre> |
| 215 | <p> |
| 216 | Site 2: |
| 217 | </p> |
| 218 | <pre class="programlisting"> # Read operations can be performed on master or replica |
| 219 | # site. |
| 220 | select * from stock_quote where price < 21.00; </pre> |
| 221 | </div> |
| 222 | </div> |
| 223 | <div class="navfooter"> |
| 224 | <hr /> |
| 225 | <table width="100%" summary="Navigation footer"> |
| 226 | <tr> |
| 227 | <td width="40%" align="left"><a accesskey="p" href="repstatistics.html">Prev</a> </td> |
| 228 | <td width="20%" align="center"> |
| 229 | <a accesskey="u" href="sqlrep.html">Up</a> |
| 230 | </td> |
| 231 | <td width="40%" align="right"> <a accesskey="n" href="admin.html">Next</a></td> |
| 232 | </tr> |
| 233 | <tr> |
| 234 | <td width="40%" align="left" valign="top">Displaying Replication Statistics </td> |
| 235 | <td width="20%" align="center"> |
| 236 | <a accesskey="h" href="index.html">Home</a> |
| 237 | </td> |
| 238 | <td width="40%" align="right" valign="top"> Chapter 5. Administrating Berkeley DB SQL Databases</td> |
| 239 | </tr> |
| 240 | </table> |
| 241 | </div> |
| 242 | </body> |
| 243 | </html> |