Srini
Srini

Reputation: 450

Maintaining singletons in Session Spring MVC

I have 2 session scoped beans

1)sessionholder1 2)sessionholder2

Both sessionholders have object UserVO in them

class SessionHolder1{
private UserVO user = new UserVO();
}

class SessionHolder2{
private UserVO user = new UserVO();
}

Now if i change some variable of the userVO in sesionholder1 it gets changed in sessionholder2 too.Is this behavior normal or is this due to bad design?

Is there any work around for this other than using clone() ?Kindly help. Thanks in advance.

Upvotes: 0

Views: 308

Answers (1)

JB Nizet
JB Nizet

Reputation: 691933

If you store the same UserVO object in both, then of course changing some field in one will change the field in the other. If you want two distinct objects, then create two distinct objects. If one is a copy of the other, the best way is to use a copy constructor:

/**
 * Constructs a copy of the given userVO
 */
public UserVO(UserVO userVO) {
    ...
}

Upvotes: 2

Related Questions