Results 1 to 4 of 4

Thread: Is it possible for beans to have their data changed inbetween requests?

  1. #1
    Join Date
    May 2012
    Posts
    11

    Default Is it possible for beans to have their data changed inbetween requests?

    Hi,

    I am very new to spring so still learning daily but whilst writing a small test application I have noticed some behaviour that made me wonder whether or not it would be possible for a beans data to be changed by another request whilst in use, on a very busy system of course.

    What I noticed is that I wrote a small class has an array list as a private parameter and this array list is returned by a get method. Multiple requests increase the array list, ie it holds its value as I guess only one instance exists.

    So what would happen if request A sets a value in a bean and before it reads it request B changes those values?

    Thanks

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    I strongly suggest a read of the spring reference guide which explains bean scopes.

    First question why are you holding state in singletons? State is something that should belong to the current request/session/user and not in the application beans. Also not everything needs to be a spring bean .

    However to solve your problem you probably want to use a request scoped bean (instead of a singleton). Again as suggested read the reference guide about scopes.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    May 2012
    Posts
    11

    Default

    Quote Originally Posted by Marten Deinum View Post
    I strongly suggest a read of the spring reference guide which explains bean scopes.

    First question why are you holding state in singletons? State is something that should belong to the current request/session/user and not in the application beans. Also not everything needs to be a spring bean .

    However to solve your problem you probably want to use a request scoped bean (instead of a singleton). Again as suggested read the reference guide about scopes.
    I will read through the reference material .re scopes again today This is just a very simple application with 1 jsp and 1 controller. The controller has a service injected into it and the service has a bean which needs to be returned to the jsp, that service also has a DAO injected. The DAO returns some data to the service which returns an object (the bean in question) to the controller. The controller then returns this object to the jsp to render the screen. Now on a subsequent page refresh the ArrayList in that bean has the first requests data, plus the next requests data and so on.

    I didnt choose or want to hold the state in a singleton I just wanted a bean to return some data to the jsp and I noticed that the ArrayList was still full of the previous request\return cycles content, I only used an annotation based example to build this application but figured that in production this could cause some issues. I am just using the defaults that come with the annotations tutorial I was following.

    You said not everything needs to be a spring bean, so is it then ok to just create objects in the normal way saying something like Bean myBean = new DataContainerBean(); etc? Someone at work (who already uses spring on their team) told me that we shouldnt be creating beans to return to a jsp in this way with spring which is the only reason I had used autowire to create the bean via the container.

    Thanks
    Last edited by paul_; Aug 17th, 2012 at 02:11 AM.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You should use spring to bootstrap and wire your application beans (services, dao's etc.) however for other things that hold state you should create new beans yourself...

    As stated you should never hold state in a singleton (at least if that state can be changed and isn't the same for each and every user). So simply return the values you want and don't store them in instance variables they should be contained to the method.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •