• Fat Cats Boardgame
  • Wicket UI Library
  • About Me

Java and Wicket web development thoughts, tutorials, and tips

roman@coderdreams.com
Coder DreamsCoder Dreams
  • Fat Cats Boardgame
  • Wicket UI Library
  • About Me

How to implement record locking in Wicket

May 28, 2020 Posted by Roman Sery wicket No Comments

The concept of record locking is useful when you want to allow only one user to edit a data record at a time. A data record or, record for short, can be any collection of data. For example, a user profile, a purchase order, or a data-entry form. In a multi-user environment, you want to avoid the case where multiple users are simultaneously editing the same record and possibly overwriting each other’s changes.

So what we want to do is build a mechanism for controlling access to a record. It’s helpful to see it in action first:

The general flow looks like this:

  • A user comes to record A and obtains an editing lock on the record.
  • Another user comes to record A but will be unable to make changes until the first user leaves. The first user will be informed to finish their changes and leave the record so that another user can make their changes.
  • Once the original user leaves, the second user will be notified that they can begin with their changes.

The main components

The main libraries we will use are:

  • wicket-native-websocket-javax – See the official Wicket documentation for adding WebSocket support to your application.
  • Redis – Using the awesome Redisson library for Java. We will use Redis topics, one for each record, to notify all listeners of the record when other users enter or leave the record.

Now let’s take a high-level look at the components involved:

  • LockablePage – All web pages that want to be lockable will implement this interface which gives them gives them the functionality via composition, without having to write much code.
  • LockingService – Contains methods for obtaining and removing locks on records.
  • EditRecordPage – Sample page for editing a record that implements LocakablePage and shows simple messaging about the editability and current locks for the record.
  • LockingWebSocketBehavior – A WebSockets behavior that is responsible for notifying the specific Redis topic that a new user has started viewing the record or has left the record. It will also update parts of the UI when it receives events.
  • RemoveLockPage – Will be invoked on window.onbeforeunload to remove the users’ lock on the record when they leave. This is a safety measure in case the lock is not removed upon closing the web socket connection.

Flowchart

About the demo

There is too much code to go thru all of it here. However I want to point out some notable things.

To simplify the demo, we get the user ID from the query string in EditRecordPage. However, in a real application, you would use the currently logged in user’s ID from your authentication provider.

In a real application, instead of EditRecordPage, you might have an edit user profile page or edit the purchase order page. In each respective case, the ‘record ID’ would be the user ID and the purchase order ID. The implementation will remain the same, both pages will implement LockablePage.

Hopefully the code is understandable enough! Find the full source code on GitHub.

No Comments
Share
4

About Roman Sery

I've been a software developer for over 10 years and still loving Java!

You also might be interested in

Customizing your codebase for each customer using Enumerations

Jul 26, 2019

Use enums and properties files to define all the behaviors of your application that are available for customization to your clients.

Wicket quick tip #2: Print to PDF

Apr 11, 2020

A series of quick tips aimed at improving your productivity. Generating PDF's from Wicket pages.

Happy holidays

Dec 24, 2019

Just a short note

Categories

  • aws
  • customization
  • database
  • debugging
  • enum
  • java
  • models
  • performance
  • projects
  • react
  • software design
  • Spring
  • tool
  • Uncategorized
  • wicket

Recent Posts

  • Rent Day
  • Self-contained Wicket Fragments
  • Pros and cons of unit testing
  • Themeable React Monopoly board
  • Please dont use client-specific release branches

Recent Comments

  • TCI Express Thanks for sharing such insightful information. TCI Express truly stands out as the best air logistics company, offering fast, secure, and efficient air express and cold chain transportation services....

    Tracking down a bug in production Wicket application ·  March 25, 2025

  • Tom Error: A zip file cannot include itself Can you please correct the plugin part so it doesn't use the same folder as input?

    Deploying Spring Boot app to AWS Beanstalk with Nginx customization ·  September 3, 2021

  • Golfman: Reality always wins I've used both Wicket and front-end JS frameworks and, having worked extensively on both, I can tell you that "Speed of development" is definitely NOT with the JS frameworks. You basically end up...

    Five reasons you should use Apache Wicket ·  August 29, 2021

  • Kiriller Sorry can not agree with you, wicket might be a well built technical framework. But the advantages of using a front-end framework like react.js and vue.js can not be beaten by Wicket nowadays. - Speed...

    Five reasons you should use Apache Wicket ·  August 23, 2021

  • Bernd Lauert Sorry but i have to refute your claims with the following arguments: 1. the Wicket community may be small but it is also very responsive, you usually get a helpful answer from the core devs on the...

    Five reasons you should use Apache Wicket ·  July 1, 2021

Archives

  • May 2021
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019

Contact Me

Send Message
Prev Next