Martlark
Martlark

Reputation: 14591

Do I need to worry about concurrency with tomcat spring beans?

Not quite understanding enough about java, do I need to worry about concurancy issues when listing, and changing DTO objects in my spring java beans in a single server tomcat application?

Upvotes: 4

Views: 1098

Answers (2)

Julien Chastang
Julien Chastang

Reputation: 17784

In short, yes. Spring Beans can often be shared by multiple threads. Pay special attention to the member variables in your Spring Bean. If they are mutable, either make them immutable or coordinate access with a lock (e.g. with synchronization), ThreadLocal, etc.

Upvotes: 4

HMM
HMM

Reputation: 3013

This is the question you need to ask yourself. Is there a way for two threads to access the same DTO?. I guess in any sane architecture there is not.

Spring beans themselves are usually singletons (when not configured otherwise), and should be thread-safe.

If all beans receive DTOs as parameters, return newly created DTOs, and all clients of those beans don't keep the references hanging around, DTOs should not be a concern. At least from a high level standpoint.

However, you should read about java concurrency. I would recommend Goetz's book if you got the chance.

Finally, back in the day, I remember Rod Johnson (THE springsource mastermind) saying DTOs were EVIL. Please take some time to google "DTO evil" and make your mind.

Upvotes: 0

Related Questions