Themes in Spring

I have a web application using spring and hibernate and struts (it works on Tomcat)

The call sequence looks something like this:

Invokes spring bean service actions, which in turn invokes spring DAO bean. The DAO implementation is an implementation of Hibernate.

Question Will all my spring beans work in one thread? Can I save something in ThreadLocal and get it in another bean?

I am sure that this would not work in a session without a bean state. An EJB container can (or will) spawn a new thread for every bean call

Will spring container do the same? those. run all beans in one thread?

When I tried the JUnit test, I got the same identifier through Thread.currentThread (). getId () in the test case and two beans - which makes me think that there was only one thread in action

Or is the behavior unpredictable? Or will it change when running on a Tomcat server?

Explanation I do not want to exchange data between two streams. I want to put data in ThreadLocal and get it from all beans in the call stack. This will only work if all the beans are in the same thread.

+6
java spring multithreading tomcat thread-local
source share
4 answers

Spring does not spawn threads. Tomcat does. Spring is just creating and connecting objects for you.

Each request from the browser is processed by one request. This request is processed by Tomcat. Tomcat creates a thread to process the request.

Assuming you just created a singleton bean in Spring called "X". Then the same instance of X is used by all requests.

Spring beans do not live on stream. They are simply allocated in a heap.

+15
source share

Will my spring beans be launched in the same thread? Can I store something in ThreadLocal and get it in another bean? AFAIK for the components you mentioned (bean service, DAO bean - I think they are simple spring beans), spring does not spawn a new thread. I do not understand your use case (i.e. data exchange between two streams).

For most web applications, a new stream is created for each new request, and if you want to exchange data between two requests, you usually: - use get / post parameters for data transfer - use a session for data exchange

To answer your question, I am sure that the spring container does not create threads for most components.

+1
source share

Yes you can do it. The same thread will be used to complete your action for ThreadLocal to work. Typically, the same thread is used for a session without a bean if it is running on the same application server instance. I would not depend on this, since it probably depends on the supplier.

We use this technique to access caller ID anywhere in the code. We also use beans and jms, but explicitly pass information between containers and set ThreadLocal at each entry point. Thus, it doesn't matter if the bean (session or mdb) is local or not.

0
source share

In addition to all the other answers, I just add the following:

Typically, the only reason for switching threads is related to some concurrency requirements. Since this usually does not work for free in terms of complexity, you will usually be clearly informed when this will happen.

Switching threads inside what seems to be single-threaded request processing is actually extremely difficult. Usually this happens only in one place in the container, and it is usually handled by tcp / ip socket readers that receive a request from external clients. These read threads usually determine which thread (pool) should process the request and redirect the request to this thread. After that, the request remains with this stream.

Thus, usually the only thing that can / can happen is to create additional threads for concurrency or asynchronous processing (e.g. JMS).

0
source share

All Articles