File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Synchronize ArrayList access in business objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Synchronize ArrayList access in business objects?" Watch "Synchronize ArrayList access in business objects?" New topic
Author

Synchronize ArrayList access in business objects?

Carl Trapani
Greenhorn

Joined: Apr 21, 2005
Posts: 4
Hi,

I have a value object, SubordinateUserDTO (DTO == Data Transfer Object), that contains a collection (ArrayList) of another value object, OrgRoleDTO.

In addition to the usual getter/setters, I would like to add methods to add and remove OrgRoleDTO items from the collection. So the SubordinateUserDTO looks like this:



The value object is used to pass data back and forth between a business service object (Usermgmt) and a servlet that support a Flash client. Usermgmt has a method that returns an ArrayList of SubordinateUserDTO, like this:



My servlet creates a Usermgmt object and calls getSubordinateUsers() locally (not an instance variable) in a method that handles all requests. It seems to me this should work fine without synchronizing access to the two collections, but I'm not sure

1) Do I have it right in thinking that I don't need to synchronize access since my servlet creates and accesses the objects in a method and method calls get done on the stack?

2) Of course, the business object should support other callers besides the servlet. Should I make the SubordinateUserDTO.addOrgRole() and removeOrgRole() methods thread safe? If so, how? Also, what about making Usermgmt.getSubordinateUsers() thread safe? Should I do this, or at least warn folks in documentation?

Thanks for any input/feedback that you might offer,
Carl
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
Can you use Vector instead of the ArrayList class? Access to the elements inside a Vector is synchronized. Another tip, use List as the type as in
List orgRoleList;
that way you can change implementation any time.

Is the business service object an EJB? If not, you may have to worry about threads in the service layer.

Calls from within the servlet are thread safe because the container manages them.


ChintanRajyaguru.com
SOADevelopment.com - Coming soon!
Carl Trapani
Greenhorn

Joined: Apr 21, 2005
Posts: 4
Hi Chintan,

NICE!! Thanks much for your feedback! I changed the type to java.util.List and then changed the constructors to use Vector like this:

Is the business service object an EJB? If not, you may have to worry about threads in the service layer.
No, just a plain old java object. If I've implemented using Vector, than I should be ok?

At this point I'm wondering about the need for removeOrgRole(). I think I'll add a getOrgRole(int index) in it's place.

Thanks again!! Let me know if you see anything else.
Carl
Carl
[ September 02, 2005: Message edited by: Carl Trapani ]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Synchronize ArrayList access in business objects?
 
Similar Threads
Locking Schemes: Tactical View 01
Expected output is incorrect!
how to do submit the Dynamic produced element in Struts
Iterator Pattern