View Full Version : Singletons or prototypes
vijaynairis
Feb 14th, 2008, 11:11 PM
Hi All,
Just a very stupid simple question.....
Are all steps and tasklets prototypes by default ?
Thanks...Vijay
robert.kasanicky
Feb 18th, 2008, 03:09 AM
no, they are just ordinary spring beans => singletons by default
adrianshum
Feb 18th, 2008, 04:07 AM
I have asked similar question before and here is what I understood, wish it can help:
Normally, tasklet is singleton by default. Therefore, if the tasklet's logic is stateless, it will run well.
However, if you want to have a stateful, or want to be StepContextAware, you may make the scope of the tasklet being "step" (you should register the scope first, refer to the unit test), and put <aop:scoped-proxy/> in the tasklet's bean config in app context. It should make your tasklet instantiated for every new step.
lucasward
Feb 18th, 2008, 11:25 AM
For a little bit of background around the issue:
When using steps, lots of very stateful resources are generally created. For example, if reading from the database you may open a cursor that stays open for the life of the batch job. This should be closed as soon as the step finishes, which is why we have step scope, to ensure resources are free. So, in theory, you could make your steps 'prototype scoped', but that doesn't necessarily guarantee that the resources created will be released without using something like step scope or closing the application context.
As a side note, there's an issue to move the responsibilities of step scope to the step itself, but the overall effect is the same:
http://jira.springframework.org/browse/BATCH-364
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.