Skip to content

Commit 3eb5813

Browse files
committed
docs: update version number
1 parent 5d44152 commit 3eb5813

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

version-number/README.md

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Ensure data consistency and integrity by tracking changes to data with version n
2121

2222
## Explanation
2323

24-
Real world example
24+
Real-world example
2525

2626
> 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.
2727
@@ -43,10 +43,10 @@ We have a `Book` entity, which is versioned, and has a copy-constructor:
4343
@Getter
4444
@Setter
4545
public class Book {
46+
4647
private long id;
4748
private String title = "";
4849
private String author = "";
49-
5050
private long version = 0; // version number
5151

5252
public Book(Book book) {
@@ -62,6 +62,7 @@ We also have `BookRepository`, which implements concurrency control:
6262

6363
```java
6464
public class BookRepository {
65+
6566
private final Map<Long, Book> collection = new HashMap<>();
6667

6768
public void update(Book book) throws BookNotFoundException, VersionMismatchException {
@@ -95,42 +96,56 @@ public class BookRepository {
9596
}
9697
```
9798

98-
Here's the concurrency control in action:
99+
Here's the version number pattern in action:
99100

100101
```java
101-
var bookId = 1;
102-
// Alice and Bob took the book concurrently
103-
final var aliceBook = bookRepository.get(bookId);
104-
final var bobBook = bookRepository.get(bookId);
105-
106-
aliceBook.setTitle("Kama Sutra"); // Alice has updated book title
107-
bookRepository.update(aliceBook); // and successfully saved book in database
108-
LOGGER.info("Alice updates the book with new version {}", aliceBook.getVersion());
109-
110-
// now Bob has the stale version of the book with empty title and version = 0
111-
// while actual book in database has filled title and version = 1
112-
bobBook.setAuthor("Vatsyayana Mallanaga"); // Bob updates the author
113-
try {
114-
LOGGER.info("Bob tries to update the book with his version {}", bobBook.getVersion());
115-
bookRepository.update(bobBook); // Bob tries to save his book to database
116-
} catch (VersionMismatchException e) {
117-
// Bob update fails, and book in repository remained untouchable
118-
LOGGER.info("Exception: {}", e.getMessage());
119-
// Now Bob should reread actual book from repository, do his changes again and save again
102+
public class App {
103+
104+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
105+
106+
public static void main(String[] args) throws
107+
BookDuplicateException,
108+
BookNotFoundException,
109+
VersionMismatchException {
110+
var bookId = 1;
111+
112+
var bookRepository = new BookRepository();
113+
var book = new Book();
114+
book.setId(bookId);
115+
bookRepository.add(book); // adding a book with empty title and author
116+
LOGGER.info("An empty book with version {} was added to repository", book.getVersion());
117+
118+
// Alice and Bob took the book concurrently
119+
final var aliceBook = bookRepository.get(bookId);
120+
final var bobBook = bookRepository.get(bookId);
121+
122+
aliceBook.setTitle("Kama Sutra"); // Alice has updated book title
123+
bookRepository.update(aliceBook); // and successfully saved book in database
124+
LOGGER.info("Alice updates the book with new version {}", aliceBook.getVersion());
125+
126+
// now Bob has the stale version of the book with empty title and version = 0
127+
// while actual book in database has filled title and version = 1
128+
bobBook.setAuthor("Vatsyayana Mallanaga"); // Bob updates the author
129+
try {
130+
LOGGER.info("Bob tries to update the book with his version {}", bobBook.getVersion());
131+
bookRepository.update(bobBook); // Bob tries to save his book to database
132+
} catch (VersionMismatchException e) {
133+
// Bob update fails, and book in repository remained untouchable
134+
LOGGER.info("Exception: {}", e.getMessage());
135+
// Now Bob should reread actual book from repository, do his changes again and save again
136+
}
137+
}
120138
}
121139
```
122140

123141
Program output:
124142

125-
```java
126-
Alice updates the book with new version 1
127-
Bob tries to update the book with his version 0
128-
Exception: Tried to update stale version 0 while actual version is 1
129143
```
130-
131-
## Class diagram
132-
133-
![Version Number](./etc/version-number.urm.png "Version Number pattern class diagram")
144+
14:51:04.119 [main] INFO com.iluwatar.versionnumber.App -- An empty book with version 0 was added to repository
145+
14:51:04.122 [main] INFO com.iluwatar.versionnumber.App -- Alice updates the book with new version 1
146+
14:51:04.122 [main] INFO com.iluwatar.versionnumber.App -- Bob tries to update the book with his version 0
147+
14:51:04.123 [main] INFO com.iluwatar.versionnumber.App -- Exception: Tried to update stale version 0 while actual version is 1
148+
```
134149

135150
## Applicability
136151

@@ -140,9 +155,9 @@ Exception: Tried to update stale version 0 while actual version is 1
140155

141156
## Tutorials
142157

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)
158+
* [JPA entity versioning (byteslounge.com)](https://www.byteslounge.com/tutorials/jpa-entity-versioning-version-and-optimistic-locking)
159+
* [Optimistic Locking in JPA (Baeldung)](https://www.baeldung.com/jpa-optimistic-locking)
160+
* [Versioning Entity (java2s.com)](http://www.java2s.com/Tutorial/Java/0355__JPA/VersioningEntity.htm)
146161

147162
## Known Uses
148163

0 commit comments

Comments
 (0)