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

Welcome!

There is a controller method:

@Controller
public class NoteController {

@Autowired
 private NoteService noteService;

@Autowired
 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
note.setText(text);
 //update the note in the database. 
noteService.saveNote(note);
 //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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></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>
</div>
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>
 <div class="form-group">
 <button type="submit" class="btn btn-primary btn-block">Save changes</button>
</div>
</form>
</div>
</div>
<br / >
the <div>
 <form th:action="@{/admin/home}" method="get">
 <button class="btn btn-md btn-warning btn-block" type="Submit">Home page</button>
</form>
</div>



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