Pale Blue Dot
Pale Blue Dot

Reputation: 581

Spring Scheduler incrementing values wrongly

I am using spring scheduler.I am calling m1() every 5 seconds .Why the value of variable count getting incremented with every call? I am expecting each call to m1() is independent of previous one.So ineach call to m1 i should get value of count to be 1

How can i get value of count =1 in each call?

  @Component
    @EnableScheduling
    public class MySchedulers {

        @Autowired
        private DaoService daoService;

        @Scheduled(cron = "*/5 * * * * ?")
        public void process() {
            daoService.serviceMethod();

        }

@Repository
public class DbDao {

    private int count = 0;

    @Value("${test}")
    private String str;

    public void m1() {

        System.out.println(" in dao layer --" + ++count);

    }

This is the O/P i am getting.

in dao layer --0
 in dao layer --1
 in dao layer --2
 in dao layer --3
 in dao layer --4

Upvotes: 0

Views: 205

Answers (1)

Mark Bramnik
Mark Bramnik

Reputation: 42431

Why the value of variable count getting incremented with every call?

I believe it gets incremented because DbDao is defined to be singleton, which means that spring maintains one object of type DbDao in the application context and this object gets injected (autowired) into MySchedulers.

In the m1 method of DbDao you always increase a count data field (++count) so its right that it will be increased upon every call - that's the expected behaviour.

How can i get value of count =1 in each call?

Well, it depends on what do you really want to do. For example, don't use count as a data field, but it really depends on logic behind your scenario.

I could say that you can use prototype scope for DbDao - but it doesn't really make sense because repositories usually are defined as singletons (I don't see a reason to create a new repository object upon every call to it).

Since repositories are supposed to be called simultaneously from different threads (or at least support this model of execution), usually, repositories should not keep state.

Update 1

Based on OP's comment:

When the scheduler is used by default spring allocates 1 thread only (pool size = 1) so, there won't be any thread related issues as all invocations will be done in the same thread. This is true of course as long as the code you call from "scheduler" is not called simultaneously by other flows (like originated from rest controller, message listeners and so forth).

You can configure the pool size though, and then it's possible that two threads that belong to the scheduler will call the same method (like the one of MyDao for instance).

In this case, it up to you to decide what exactly do you want to protect and how, spring won't help you here. As I've explained above, indeed, since MyDao is a singleton, many threads will access its shared state, so in this case, you'll probably want to do something with it.

Upvotes: 1

Related Questions