Transactions, security and thanks

Practice for Week 11

In the practical activities for this week, you used JDBC with pessimistic concurrency control.

When you are using JPA, you can use pessimistic concurrency control. However, with JPA it is more common to make use of optimistic concurrency control using @Version.

For this challenge, you should create a simple application that lets users edit a notes field. If two users modify the notes at the same time, then this should be detected when they attempt to save the changes.

For example, the user might see something like this when they visit the application:

Initial view

Then, suppose they make some changes (but haven't yet clicked "Save Changes"):


Suppose that, at the same time, another user working in another window has changed and saved the notes (to "I've made a change in another window"). Then, when the current user clicks "Save Changes", they should see a warning:

Concurrent modification warning

If there aren't any concurrent modifications, then the changes should just be saved directly when the user clicks "Save Changes".

Challenge: Can you build an application that does this?


Create an @Entity for storing your notes and ensure there is an @Version field/property.

In this challenge problem, we have just one notes field so a primary key doesn't make sense. However, JPA requires a primary key. You could create a primary key field/property but only show/edit the Entity with id = 1 (i.e., your table has just one row). Alternatively, you could have multiple notes in your application, and allow users to choose which note they are editing.

If there is a concurrent modification, then the version field/property in the database and the application will be mismatched. This mismatch will be detected if you merge and then flush an updated record with an incorrect version.

If your backing bean is request scoped, then you may need to store your version information in a hidden field.

Think back over the course so far:

  • What have you learnt?
  • What was the hardest part?
  • What did you find the easiest?
  • What did you think would be hard but turned out to be easy?
  • What did you think would be easy but turned out to be hard?
  • How do all of the technologies that we've covered relate to each other?
  • What's missing from this course? What's missing from the Java EE platform?

Next, think about what you'll do next:

  • How will you use what you've learnt?
  • How will you improve your skills?
  • What weaknesses do you think you need to improve?
  • What strengths have you gained that you think you can exploit?