My service bean example
@Service("officeService")
@Transactional
public class OfficeService {
@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;
@Transactional(readOnly=true)
public List<Office> getAllOffices(){
Session session = sessionFactory.getCurrentSession();
Criteria crit = session.createCriteria(Office.class);
crit.addOrder(Order.desc("name"));
return crit.list();
}
}
What would it look like if it were not thread safe (or isn’t it already) ?
And if my all my controllers and services are singletons, does this mean that each request simply acts on the singletons concurrently when required ?
If you managed any state within the class as instance variables then it wouldn’t be thread safe. Since the only state you have is the sessionFactory, as long as its getCurrentSession() method is thread safe (which it is), then so is your method