Sercat concurrency / synchronization in Tomcat?

Is there a recommended way to synchronize instances of Tomcat Servlet that are competing for the same resource (for example, a file or database, for example MongoDB, which is not an ACID)?

I am familiar with thread synchronization so that two Java threads do not access the same Java object at the same time, but not to objects that exist outside the JRE.

edit: I have only 1 Tomcat server. Regardless of whether this means different JVMs or not, I'm not sure (I assume this is the same JVM, but potentially different threads).


edit: specific use case (but I ask the question in general):

The Tomcat server acts as a file vault, places raw files in a directory, and uses MongoDB to store metadata. This is a fairly simple concept, with the exception of the concurrency problem. If there are two simultaneous requests for storing the same file or for managing metadata on the same object at the same time, I need a way to fix it, and I'm not sure how to do it. I believe that the simplest approach would be to serialize / queues somehow. Is there a way to implement queuing in Tomcat?

+3
source share
5 answers

, JVM, , , . , .

gubbinry, , Semaphore ( /, , ), "" .

+3

tomcat , Java, . , " " - , , . "" tomcat 6.0 , " ", .

+3

2 . JVM, "" Java. JVM Java , ,

, concurrency.

+2

- Java (, java.io.File). , .

, , .

+1

IMO, you are asking for trouble. There are reasons why things like databases and shared file systems were created. Trying to write your own using some kind of Singleton class or semaphores will be very ugly. Find a storage solution that does this for you and saves you a ton of headaches.

+1
source

Source: https://habr.com/ru/post/1719452/


All Articles