views:

338

answers:

2

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?

A: 

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.

Marcelo Morales
I thought DTO were annoying as well, but it was what was chosen for me.
Martlark
this SO question is already the second result on google (for me, at least) for "DTO evil"!
matt b
+2  A: 

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.

Julien Chastang
are you saying that if my bean is backing a page and the page is opened twice then the bean is shared between the pages or is a new bean created for each page session? I'll have to experiment when i get back to work
Martlark
Speaking broadly, you need to reason about whether your bean is shared and how it is shared. I realize this sometimes is easier said than done.
Julien Chastang
@Julien Chastang. thanks ... i think.
Martlark