Skip to content

Commit c9c7349

Browse files
committed
docs: update version number
1 parent d6d2a76 commit c9c7349

File tree

2 files changed

+67
-81
lines changed

2 files changed

+67
-81
lines changed

version-number/README.md

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,60 @@
11
---
22
title: Version Number
3-
category: Concurrency
3+
category: Data access
44
language: en
55
tag:
6-
- Data access
7-
- Microservices
6+
- Compatibility
7+
- Data access
8+
- Persistence
9+
- State tracking
10+
- Versioning
811
---
912

10-
## Name / classification
11-
12-
Version Number.
13-
1413
## Also known as
1514

16-
Entity Versioning, Optimistic Locking.
15+
* Entity Versioning
16+
* Versioning
1717

1818
## Intent
1919

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.
2121

2222
## Explanation
2323

2424
Real world example
2525

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.
2827
2928
In plain words
3029

3130
> Version Number pattern grants protection against concurrent updates to same entity.
3231
3332
Wikipedia says
3433

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.
4035
4136
**Programmatic Example**
4237

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+
4340
We have a `Book` entity, which is versioned, and has a copy-constructor:
4441

4542
```java
43+
@Getter
44+
@Setter
4645
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 = "";
5249

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
5951

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+
}
6158
}
6259
```
6360

@@ -133,31 +130,50 @@ Exception: Tried to update stale version 0 while actual version is 1
133130

134131
## Class diagram
135132

136-
![alt text](./etc/version-number.urm.png "Version Number pattern class diagram")
133+
![Version Number](./etc/version-number.urm.png "Version Number pattern class diagram")
137134

138135
## Applicability
139136

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.
144140

145141
## Tutorials
146-
* [Version Number Pattern Tutorial](http://www.java2s.com/Tutorial/Java/0355__JPA/VersioningEntity.htm)
147142

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)
152154

153155
## Consequences
154-
Version Number pattern allows to implement a concurrency control, which is usually done
155-
via Optimistic Offline Lock pattern.
156156

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.
159173

160174
## 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)

version-number/src/main/java/com/iluwatar/versionnumber/Book.java

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@
2424
*/
2525
package com.iluwatar.versionnumber;
2626

27+
import lombok.Getter;
28+
import lombok.Setter;
29+
2730
/**
2831
* Model class for Book entity.
2932
*/
33+
@Getter
34+
@Setter
3035
public class Book {
3136
private long id;
3237
private String title = "";
3338
private String author = "";
34-
3539
private long version = 0; // version number
3640

37-
public Book() {
38-
39-
}
41+
public Book() {}
4042

4143
/**
4244
* We need this copy constructor to copy book representation in {@link BookRepository}.
@@ -47,36 +49,4 @@ public Book(Book book) {
4749
this.author = book.author;
4850
this.version = book.version;
4951
}
50-
51-
public long getId() {
52-
return id;
53-
}
54-
55-
public void setId(long id) {
56-
this.id = id;
57-
}
58-
59-
public String getTitle() {
60-
return title;
61-
}
62-
63-
public void setTitle(String title) {
64-
this.title = title;
65-
}
66-
67-
public String getAuthor() {
68-
return author;
69-
}
70-
71-
public void setAuthor(String author) {
72-
this.author = author;
73-
}
74-
75-
public long getVersion() {
76-
return version;
77-
}
78-
79-
public void setVersion(long version) {
80-
this.version = version;
81-
}
8252
}

0 commit comments

Comments
 (0)