• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronize ArrayList access in business objects?

 
Carl Trapani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carl Trapani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!