|
1 | 1 | ---
|
2 | 2 | title: Version Number
|
3 |
| -category: Concurrency |
| 3 | +category: Data access |
4 | 4 | language: en
|
5 | 5 | tag:
|
6 |
| - - Data access |
7 |
| - - Microservices |
| 6 | + - Compatibility |
| 7 | + - Data access |
| 8 | + - Persistence |
| 9 | + - State tracking |
| 10 | + - Versioning |
8 | 11 | ---
|
9 | 12 |
|
10 |
| -## Name / classification |
11 |
| - |
12 |
| -Version Number. |
13 |
| - |
14 | 13 | ## Also known as
|
15 | 14 |
|
16 |
| -Entity Versioning, Optimistic Locking. |
| 15 | +* Entity Versioning |
| 16 | +* Versioning |
17 | 17 |
|
18 | 18 | ## Intent
|
19 | 19 |
|
20 |
| -Resolve concurrency conflicts when multiple clients are trying to update same entity simultaneously. |
| 20 | +Ensure data consistency and integrity by tracking changes to data with version numbers. |
21 | 21 |
|
22 | 22 | ## Explanation
|
23 | 23 |
|
24 | 24 | Real world example
|
25 | 25 |
|
26 |
| -> Alice and Bob are working on the book, which stored in the database. Our heroes are making |
27 |
| -> changes simultaneously, and we need some mechanism to prevent them from overwriting each other. |
| 26 | +> Consider a library system where multiple librarians can update the details of books simultaneously. Each book entry in the library's database has a version number. When a librarian wants to update a book's details, the system checks the version number of the entry. If the version number matches the current version in the database, the update proceeds, and the version number is incremented. If the version number has changed, it means another librarian has already updated the book details, prompting the system to notify the librarian of the conflict and suggesting a review of the latest changes. This ensures that updates do not overwrite each other unintentionally, maintaining data integrity and consistency. |
28 | 27 |
|
29 | 28 | In plain words
|
30 | 29 |
|
31 | 30 | > Version Number pattern grants protection against concurrent updates to same entity.
|
32 | 31 |
|
33 | 32 | Wikipedia says
|
34 | 33 |
|
35 |
| -> Optimistic concurrency control assumes that multiple transactions can frequently complete |
36 |
| -> without interfering with each other. While running, transactions use data resources without |
37 |
| -> acquiring locks on those resources. Before committing, each transaction verifies that no other |
38 |
| -> transaction has modified the data it has read. If the check reveals conflicting modifications, |
39 |
| -> the committing transaction rolls back and can be restarted. |
| 34 | +> The Version Number pattern is a technique used to manage concurrent access to data in databases and other data stores. It involves associating a version number with each record, which is incremented every time the record is updated. This pattern helps ensure that when multiple users or processes attempt to update the same data simultaneously, conflicts can be detected and resolved. |
40 | 35 |
|
41 | 36 | **Programmatic Example**
|
42 | 37 |
|
| 38 | +Alice and Bob are working on the book, which stored in the database. Our heroes are making changes simultaneously, and we need some mechanism to prevent them from overwriting each other. |
| 39 | + |
43 | 40 | We have a `Book` entity, which is versioned, and has a copy-constructor:
|
44 | 41 |
|
45 | 42 | ```java
|
| 43 | +@Getter |
| 44 | +@Setter |
46 | 45 | public class Book {
|
47 |
| - private long id; |
48 |
| - private String title = ""; |
49 |
| - private String author = ""; |
50 |
| - |
51 |
| - private long version = 0; // version number |
| 46 | + private long id; |
| 47 | + private String title = ""; |
| 48 | + private String author = ""; |
52 | 49 |
|
53 |
| - public Book(Book book) { |
54 |
| - this.id = book.id; |
55 |
| - this.title = book.title; |
56 |
| - this.author = book.author; |
57 |
| - this.version = book.version; |
58 |
| - } |
| 50 | + private long version = 0; // version number |
59 | 51 |
|
60 |
| - // getters and setters are omitted here |
| 52 | + public Book(Book book) { |
| 53 | + this.id = book.id; |
| 54 | + this.title = book.title; |
| 55 | + this.author = book.author; |
| 56 | + this.version = book.version; |
| 57 | + } |
61 | 58 | }
|
62 | 59 | ```
|
63 | 60 |
|
@@ -133,31 +130,50 @@ Exception: Tried to update stale version 0 while actual version is 1
|
133 | 130 |
|
134 | 131 | ## Class diagram
|
135 | 132 |
|
136 |
| - |
| 133 | + |
137 | 134 |
|
138 | 135 | ## Applicability
|
139 | 136 |
|
140 |
| -Use Version Number for: |
141 |
| - |
142 |
| -* resolving concurrent write-access to the data |
143 |
| -* strong data consistency |
| 137 | +* Use when you need to handle concurrent data modifications in a distributed system. |
| 138 | +* Suitable for systems where data consistency and integrity are crucial. |
| 139 | +* Ideal for applications using databases that support versioning or row versioning features. |
144 | 140 |
|
145 | 141 | ## Tutorials
|
146 |
| -* [Version Number Pattern Tutorial](http://www.java2s.com/Tutorial/Java/0355__JPA/VersioningEntity.htm) |
147 | 142 |
|
148 |
| -## Known uses |
149 |
| - * [Hibernate](https://vladmihalcea.com/jpa-entity-version-property-hibernate/) |
150 |
| - * [Elasticsearch](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#index-versioning) |
151 |
| - * [Apache Solr](https://lucene.apache.org/solr/guide/6_6/updating-parts-of-documents.html) |
| 143 | +* [JPA entity versioning - byteslounge.com](https://www.byteslounge.com/tutorials/jpa-entity-versioning-version-and-optimistic-locking) |
| 144 | +* [Optimistic Locking in JPA - Baeldung](https://www.baeldung.com/jpa-optimistic-locking) |
| 145 | +* [Versioning Entity - java2s.com](http://www.java2s.com/Tutorial/Java/0355__JPA/VersioningEntity.htm) |
| 146 | + |
| 147 | +## Known Uses |
| 148 | + |
| 149 | +* Hibernate (Java Persistence API) uses version numbers to implement optimistic locking. |
| 150 | +* Microsoft SQL Server and Oracle databases support version-based concurrency control. |
| 151 | +* Apache CouchDB and other NoSQL databases implement versioning for conflict resolution. |
| 152 | +* [Elasticsearch](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#index-versioning) |
| 153 | +* [Apache Solr](https://lucene.apache.org/solr/guide/6_6/updating-parts-of-documents.html) |
152 | 154 |
|
153 | 155 | ## Consequences
|
154 |
| -Version Number pattern allows to implement a concurrency control, which is usually done |
155 |
| -via Optimistic Offline Lock pattern. |
156 | 156 |
|
157 |
| -## Related patterns |
158 |
| -* [Optimistic Offline Lock](https://martinfowler.com/eaaCatalog/optimisticOfflineLock.html) |
| 157 | +Benefits: |
| 158 | + |
| 159 | +* Improves data consistency and integrity. |
| 160 | +* Reduces the likelihood of lost updates in concurrent environments. |
| 161 | +* Provides a mechanism for conflict detection and resolution. |
| 162 | + |
| 163 | +Trade-offs: |
| 164 | + |
| 165 | +* Requires additional logic for version checking and handling conflicts. |
| 166 | +* Can lead to increased complexity in database schema and application logic. |
| 167 | +* Potential performance overhead due to version checks and conflict resolution. |
| 168 | + |
| 169 | +## Related Patterns |
| 170 | + |
| 171 | +* [Optimistic Offline Lock](https://java-design-patterns.com/patterns/optimistic-offline-lock/): Uses version numbers to detect conflicts rather than preventing them from occurring. |
| 172 | +* Pessimistic Offline Lock: An alternative approach to concurrency control where data is locked for updates to prevent conflicts. |
159 | 173 |
|
160 | 174 | ## Credits
|
161 |
| -* [Optimistic Locking in JPA](https://www.baeldung.com/jpa-optimistic-locking) |
162 |
| -* [JPA entity versioning](https://www.byteslounge.com/tutorials/jpa-entity-versioning-version-and-optimistic-locking) |
163 |
| -* [J2EE Design Patterns](http://ommolketab.ir/aaf-lib/axkwht7wxrhvgs2aqkxse8hihyu9zv.pdf) |
| 175 | + |
| 176 | +* [Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems](https://amzn.to/3y6yv1z) |
| 177 | +* [J2EE Design Patterns](https://amzn.to/4dpzgmx) |
| 178 | +* [Java Persistence with Hibernate](https://amzn.to/44tP1ox) |
| 179 | +* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR) |
0 commit comments