Question

My question is : let's assume that i have a spring bean : a DAO one (it's a singleton of course)

So, when we have lot of users who want to use this bean at the same time : what happens?

or,

for every user, spring instantiate a DAO bean for him?

==> we have a singleton by application Context : did spring create a context for every user?

Was it helpful?

Solution

(it's a singleton of course)

Then it's a singleton. The ApplicationContext will only create one instance and pass that same instance around wherever it's requested.

for every user, spring [instantiate] a DAO bean for him?

No, it will retrieve and give them the same DAO instance.

==> we have a singleton by application Context : did spring create a context for every user?

I'm not exactly sure what you mean for every user. A Java application has no knowledge of users. If you mean in the context of a multithreaded application, it's still irrelevant. The injected DAO bean will still be that single instance.

As stated in the comments, it's your responsibility to handle the state of your DAO, if it's mutable, handle concurrent access. Handle transactions (possibly with @Transactional) with the datasource.

OTHER TIPS

You dont use DAOs as Spring beans. Spring beans (singleton scope) are more like a service.

Lets say you have a Pizza Dao, and a PizzaService, your Service is a spring bean, while pizza isnt.

class Pizza {
    Set ingredients = ... ; 

    public Pizza(Set s) {
         ...
    }

    private addIngredient(Object o ) {
        set.add... 
    }
}

@Service
class PizzaService {
    private Pizza createPizza(..){
        Pizza p = new Pizza(); 
        ....
        return pizza;
    }

    private void persistPizza(Pizza pizza){
        ..save to db..
    }
} 

Test your service:

class JunitFoo{
    @Autowired private PizzaService service; 

    @Test
    someMethod(){
        Pizza p = service.createPizza(); 
        p.addIngredient(cheese)
        service.persistPizza(p);
    }
}

You can also implement Runnable in JunitFoo and launch someMethod a lot of time with differents threads (your User), every user will get its own Pizza. But all users use the same pizzaService.

Due to this reason singleton is default scope of a spring bean. You can also create a Pizza by fetching it from your application context, but in this case, you would need prototype otherwise all users share the same pizza

<bean id="pizza" class="foo.Pizza" scope="prototype" > 
<default set of pizza stuff> 
</bean>

If you do it that way, your PizzaService could look like this:

@Service
class PizzaService {
    @Autowired private ApplicationContext context;
    private Pizza createPizza(..){
        return (Pizza) context.getBean("pizza");
    }

    private void persistPizza(Pizza pizza){
        ..save to db..
    }
} 

This topic is way to big to cover it in a single post, i hope i could help and improve your understanding. Please also check the spring documantation about bean scopes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top