[英]Handling Concurrent request with SpringBoot and Hibernate
我正在使用SpringBoot創建一個Web應用程序。在我的應用程序中,我有RestController層,它將調用服務層,而服務層又將調用DAO層。 我的服務層用@Transactional注釋(Spring事務)
我已經使用通用DAO休眠實現了DAO層
public class Dao{
@Autowired(name = "sessionFactory")
private SessionFactory sessionFactory;
public <T> T save(final T o){
return (T) sessionFactory.getCurrentSession().save(o);
}
public void delete(final Object object){
sessionFactory.getCurrentSession().delete(object);
}
/***/
public <T> T get(final Class<T> type, final Long id){
return (T) sessionFactory.getCurrentSession().get(type, id);
}
/***/
public <T> T merge(final T o) {
return (T) sessionFactory.getCurrentSession().merge(o);
}
/***/
public <T> void saveOrUpdate(final T o){
sessionFactory.getCurrentSession().saveOrUpdate(o);
}
}
在application.properties中,我有spring.jpa.properties.hibernate.current_session_context_class = org.springframework.orm.hibernate4.SpringSessionContext
所以我擔心的是,當多個並發請求到達我的REST控制器時,我是否需要將服務層bean / DAO層bean的范圍作為原型? 另外,是否需要同步DAO層中的方法? 或getCurrentSession()將始終返回新會話,因為休眠會話不是線程安全的。
PS由於某些特定原因,我不想使用Spring Data JPA
所以我擔心的是,當多個並發請求到達我的REST控制器時,我是否需要將服務層bean / DAO層bean的范圍作為原型?
另外,是否需要同步DAO層中的方法? 或getCurrentSession()將始終返回新會話,因為休眠會話不是線程安全的。
您作為Spring Bean進行會話,默認情況下為Singleton,因此所有請求都只有一個實例。
您是否考慮過使用spring-rest-data項目?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.