How to prevent simultaneous access to a file/cell in the database?


There is a controller method:

public class NoteController {

 private NoteService noteService;

 private UserService userService;

 private NoteFileWriter noteFileWriter = new NoteFileWriter();

 @RequestMapping(value = "admin/editnote/{id}", method = RequestMethod.POST)
 public String editNotePost(@PathVariable("id")Long id,
 @ModelAttribute("note")String text){
 //get from DB the note by ID
 Note note = noteService.getById(id);
 //change the notes field to the new
 //update the note in the database. 
 //update the comment in the file
 noteFileWriter.writeNoteToFileWithFOS(note.getId().toString(), text);

 return "redirect:/admin/notelist";

and the page view:

 <title>Add Notee</title>
 <link rel="stylesheet" href="">
 <script src=""></script>
 <script src=""></script>

<div class="container">
 <div class="row">
 <form autocomplete="off" action="#" th:action="@{/admin/editnote/{id}(id=${id})}" th:object="${note}" method="post" class="form-horizontal" role="form">
 <div style="margin-top: auto">
 <h2>Write your changes</h2>
the <div>
 <textarea class="form-control" rows="12" style="margin-top: 10px;" placeholder="Note" name="note" th:inline="text" th:text="${note.text}"> </textarea>
 <div class="form-group">
 <button type="submit" class="btn btn-primary btn-block">Save changes</button>
<br / >
the <div>
 <form th:action="@{/admin/home}" method="get">
 <button class="btn btn-md btn-warning btn-block" type="Submit">Home page</button>

How best to organize simultaneous access to a file/cell database by means of multi-threading?
For example, that if one user started editing the annotation. The second could only view the most recent version, but can also violate the editing?

Thanks in advance!
June 10th 19 at 16:04
0 answer

Find more questions by tags multithreadingJava