0

I'm working with struts2, hibernate and spring and using model driven pattern. It seems that there is a serious issue when trying to fetch an object with 2 different users and sessions (also different computers) at the same time exactly.

More info... Let's say we have a Project object which has 2 members - user and name. Both users will try to fetch their Project object (which is a different object for different user of course). So User A would have a project with id 498 and User B would have a project with ID 499.

The struts action would recognize that they're trying to fetch an object with different ID but it seems that both of the users have the same Project object instance and therefore they see the same result. You could see in the log provided here:

2011-12-08 14:07:21 LoginInterceptor [INFO] User 17 is invoking populateProject, params: id=499 2011-12-08 14:07:21 LoginInterceptor [INFO] User 4 is invoking populateProject, params: id=498 2011-12-08 14:07:21 ProjectAction [INFO] Obj: hbn.Project@e2df60d, Session User Id is 17, obj.user.id is 4 2011-12-08 14:07:21 ProjectAction [INFO] Obj: hbn.Project@e2df60d, Session User Id is 4, obj.user.id is 4

How could I solve it?

Thanks, Ron.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Ron
  • 393
  • 1
  • 4
  • 13
  • how you are creating instance of struts2 action?? are you using struts2-spring plugin and if yes, show your spring file where you are creating action instance? – Umesh Awasthi Dec 08 '11 at 12:23
  • Yes I do it with struts2-spring plugin. I have a separate spring config file for each actions (or most of them). This is from the ProjectBeans.xml – Ron Dec 08 '11 at 14:45
  • can you show any of the spring config file and additionally what scope of you are defining for the actions? – Umesh Awasthi Dec 08 '11 at 14:48
  • That's the part of the spring config file that initiate the action...I'm not sure which scope, where should I look? That's part of the struts.xml jsp/Project/form.jsp jsp/Project/list.jsp jsp/Project/details.jsp jsp/Project/showReport.jsp – Ron Dec 08 '11 at 14:50
  • ok.in your struts.xml file you have defined `class="projectAction` and this `projectAction` class must be initialized by spring in some other file can you show that entry where you have created a bean like `` – Umesh Awasthi Dec 08 '11 at 14:55
  • I've just added this scope="prototype" - it wasn't there before and i think it seems to solve it...could be? – Ron Dec 08 '11 at 15:02
  • yup since in struts2 for each request there should be a new action object, see my detail answer below :) – Umesh Awasthi Dec 08 '11 at 15:09

1 Answers1

3

As per discussion we have i am posting the cause of the problem and solution. The scope was not set for the action being created by spring plugin and by default they have a scope of singleton. In struts2 each action also work as a domain object so Struts2 always create a new instance of an action per request and place it on value stack.

in above case scope was singleton and which was the cause of problem since both user have same action object being passed by the spring due to singleton scope .Setting scope=prototype solved problem

for more details refer the official plugin page

Struts2-Spring plugin

Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204
  • By the way, is it better to store the object fetched from DB in the session? because from what i understand here, every tag like would create a new instance of the action (needs to access the getObj method) – Ron Dec 08 '11 at 15:22
  • i suggest you to create a new question for this.but no need to put every object in to session as struts2 out of the box create value stack and put request object there so can access from there – Umesh Awasthi Dec 08 '11 at 15:24