@@ -66,6 +66,32 @@ <h1 id="breadcrumb">
6666 </ li >
6767 < li class ="nav1 active ">
6868 < a class ="active " href ="/docs/read-replicas "> Read Replicas</ a >
69+ < ul class ="nav nav-scroll ">
70+ < li >
71+ < a href ="#overview "> Overview</ a >
72+ </ li >
73+ < li >
74+ < a href ="#readOnlyUrl "> ReadOnlyUrl</ a >
75+ </ li >
76+ < li >
77+ < a href ="#readOnlyDataSource "> ReadOnlyDataSource</ a >
78+ </ li >
79+ < li >
80+ < a href ="#aurora "> AWS Aurora</ a >
81+ </ li >
82+ < li >
83+ < a href ="#usingMaster "> Using Master</ a >
84+ </ li >
85+ < li >
86+ < a href ="#queryUseReadReplica "> Using Replica</ a >
87+ </ li >
88+ < li >
89+ < a href ="#transactionReadOnly "> @Transaction(readOnly=true)</ a >
90+ </ li >
91+ < li >
92+ < a href ="#readOnlyDatabase "> ReadOnly Database</ a >
93+ </ li >
94+ </ ul >
6995 </ li >
7096 < li class ="nav1 ">
7197 < a href ="/docs/database "> Database platforms</ a >
@@ -117,21 +143,26 @@ <h1 id="breadcrumb">
117143 </ div >
118144</ form >
119145
120- < h2 id ="transactional " > Read Replicas </ h2 >
146+ < h2 id ="overview " > Overview </ h2 >
121147< p >
122- Ebean supports configuring a second DataSource [connection pool] that points to
123- a "Read replica". The DataSource is < em > readOnly = true</ em > and < em > autoCommit=true</ em > .
148+ Ebean supports configuring a second DataSource (connection pool) that points to
149+ a "Read replica". The "Read Replica" DataSource is set to be < em > readOnly = true</ em >
150+ and < em > autoCommit=true</ em > .
124151</ p >
125-
126- < h2 > DataSourceBuilder readOnlyUrl</ h2 >
127152< p >
128- When using DataSourceBuilder we can specify an additional < code > readOnlyUrl</ code > which
129- can point to a read replica (e.g. AWS Aurora reader endpoint).
153+ When Ebean is configured with 2 DataSources it will use the "Main" DataSource for explicit
154+ transactions and writing data, and will use the "Read Only" DataSource for
155+ queries that are executed without an explicit transaction - see
156+ < a href ="#queryUseReadReplica "> Using the Read Replica.</ a >
130157</ p >
158+
159+ < h2 id ="readOnlyUrl "> DataSourceBuilder readOnlyUrl</ h2 >
131160< p >
132- Ebean DataSourceBuilder can support configuration for both a "Master/Writer" DataSource and a
133- "Read Only" DataSource where the difference between these is the connection url. This is
134- the case for AWS Aurora where there are 2 endpoints available (Writer and Reader endpoints).
161+ Using DataSourceBuilder we can specify an additional < code > readOnlyUrl</ code > which
162+ can point to a read replica (e.g. AWS Aurora reader endpoint). When readOnlyUrl is set,
163+ the DataSourceBuilder can effectively build 2 DataSources. The difference between these
164+ DataSources is the connection url and that the ReadOnly DataSource will have
165+ < em > readOnly=true</ em > and < em > autoCommit=true</ em > .
135166</ p >
136167< div class ="syntax java "> < div class ="highlight "> < pre > < span > </ span > < span class ="k "> var</ span > < span class ="n "> dataSourceBuilder</ span > < span class ="o "> =</ span >
137168 < span class ="n "> DataSourceBuilder</ span > < span class ="o "> .</ span > < span class ="na "> create</ span > < span class ="o "> ()</ span >
@@ -143,7 +174,7 @@ <h2>DataSourceBuilder readOnlyUrl</h2>
143174</ pre > </ div >
144175</ div >
145176
146- < h2 > DatabaseBuilder readOnlyDataSource</ h2 >
177+ < h2 id =" readOnlyDataSource " > DatabaseBuilder readOnlyDataSource</ h2 >
147178< p >
148179 If we are not using DataSourceBuilder, we can instead specify a "Read Only" DataSource
149180 to use. We can create a DataSource (using say Hikari) and specify it as the < em > readOnlyDataSource</ em > .
@@ -177,22 +208,19 @@ <h2 id="aurora">Amazon AWS Aurora</h2>
177208</ ul >
178209< img alt ="AWS Aurora Endpoints " src ="/images/aws-aurora-endpoints.png " class ="img-responsive " style ="max-width: 700px ">
179210
180- < p >
181-
182- </ p >
183-
184211
185212< h2 id ="usingMaster "> Using the "Master"</ h2 >
186213< p >
187214 When we have an explicit transaction via < em > @Transactional</ em > on a method or using
188215 explicitly < code > database.beginTransaction()</ code > - then this transaction will use
189- the "Master".
216+ the "Master" DataSource .
190217</ p >
191218< p >
192219 When we use database.save() without an explicit transaction, then a transaction will be
193- used implicitly and this will use the "Master".
220+ used implicitly and this will use the "Master" DataSource .
194221</ p >
195222
223+
196224< h2 id ="queryUseMaster "> Query.usingMaster()</ h2 >
197225< p >
198226 If we have a query that is executed outside any explicit transaction, and it will fetch data
@@ -204,7 +232,7 @@ <h2 id="queryUseMaster">Query.usingMaster()</h2>
204232< div class ="syntax java "> < div class ="highlight "> < pre > < span > </ span > < span class ="k "> var</ span > < span class ="n "> customers</ span > < span class ="o "> =</ span >
205233 < span class ="k "> new</ span > < span class ="n "> QCustomer</ span > < span class ="o "> ()</ span >
206234 < 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 >
207- < span class ="o "> .</ span > < span class ="na "> usingMaster</ span > < span class ="o "> ()</ span > < span class ="c1 "> // Use the "Master/Writer" database </ span >
235+ < span class ="o "> .</ span > < span class ="na "> usingMaster</ span > < span class ="o "> ()</ span > < span class ="c1 "> // Use the "Master/Writer" dataSource </ span >
208236 < span class ="o "> .</ span > < span class ="na "> findList</ span > < span class ="o "> ();</ span >
209237</ pre > </ div >
210238</ div >
@@ -227,18 +255,38 @@ <h2 id="queryUseReadReplica">Using the Read Replica</h2>
227255< h2 id ="transactionReadOnly "> @Transaction(readOnly=true)</ h2 >
228256< p >
229257 When a method is annotated with < code > @Transaction(readOnly=true)</ code > , then this
230- will create a read only transaction using the "Read Replica".
258+ will create a read only transaction using the read only DataSource (which will be
259+ pointing to the "Read Replica" instance).
231260</ p >
232261
233262< 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 >
234263< span class ="kt "> void</ span > < span class ="nf "> myMethod</ span > < span class ="o "> ()</ span > < span class ="o "> {</ span >
235264
236- < span class ="c1 "> // do stuff with a transaction using the Read replica</ span >
265+ < span class ="c1 "> // do stuff with a transaction using the</ span >
266+ < span class ="c1 "> // Read replica / read only dataSource</ span >
237267
238268< span class ="o "> }</ span >
239269</ pre > </ div >
240270</ div >
241271
272+ < h2 id ="readOnlyDatabase "> ReadOnly Database</ h2 >
273+ < p >
274+ An Ebean Database can be specified as < em > read-only</ em > using
275+ < code > DatabaseBuilder.readOnlyDatabase(true)</ code > to indicate that
276+ this Database can only be used in a read only way.
277+ </ p >
278+ < p >
279+ This means that the DataSource and read-only DataSource are expected to be the
280+ same and use < em > readOnly=true</ em > and < em > autoCommit=true</ em > .
281+ </ p >
282+ < div class ="syntax java "> < div class ="highlight "> < pre > < span > </ span > < span class ="k "> var</ span > < span class ="n "> database</ span > < span class ="o "> =</ span > < span class ="n "> Database</ span > < span class ="o "> .</ span > < span class ="na "> builder</ span > < span class ="o "> ()</ span >
283+ < span class ="o "> .</ span > < span class ="na "> name</ span > < span class ="o "> (</ span > < span class ="s "> "mydb"</ span > < span class ="o "> )</ span >
284+ < span class ="o "> .</ span > < span class ="na "> readOnlyDatabase</ span > < span class ="o "> (</ span > < span class ="kc "> true</ span > < span class ="o "> )</ span >
285+ < span class ="o "> ...</ span >
286+ < span class ="o "> .</ span > < span class ="na "> build</ span > < span class ="o "> ();</ span >
287+ </ pre > </ div >
288+ </ div >
289+
242290
243291 < nav class ="next ">
244292 < p class ="edit-page ">
0 commit comments