![](/img/trans.png)
[英]Spring + Hibernate with annotations: No Hibernate Session bound to thread
[英]Spring + Hibernate: No Hibernate Session bound to thread
我正在嘗試實現以下內容:從SpringSecurity注銷處理程序上的數據庫中清除一些詳細信息。 嘗試從數據庫獲取用戶詳細信息后出現的主要問題是此錯誤。 其余的代碼,甚至相同的方法在其他情況下也可以正常工作。
public class CurrentUserLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {
/**
*
*/
@Autowired
private RequestsService requestsService;
/**
*
*/
@Autowired
private OffersService offersService;
/**
*
*/
@Autowired
private UsersService usersService;
/**
*
*/
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
if (authentication != null) {
UserDetailsExtended details = (UserDetailsExtended) authentication.getPrincipal();
User user = usersService.get(details.getId()); // fails here
requestsService.unlockAllByBackoffice(user);
offersService.unlockAllByBackoffice(user);
}
setDefaultTargetUrl("/");
super.onLogoutSuccess(request, response, authentication);
}
}
配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.ejl.butler.object.data" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
DAO:
public User get(final Long id) {
Session session = SessionFactoryUtils.getSession(sessionFactory, false);
return (User) session.get(User.class, id);
}
春季安全配置:
<logout invalidate-session="true" logout-url="/logout" success-handler-ref="logoutSuccessHandler"/>
例外:
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:356)
at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:202)
@Transactional
解決了問題,但我不明白為什么? 我的意思是,它只會在此處理程序的所有其他調用中失敗,如果沒有此注釋,此方法將正常工作!
先感謝您!
UPD:我的臨時解決方案是將@Transactional
添加到整個onLogoutSuccess
方法中。
如果您在Spring上下文中定義了TransactionManager
,則必須在堆棧中的某處指定@Transactional
。 否則,您將遇到遇到的異常,因為您試圖在事務外部運行查詢。
有一些解決方法,例如在休眠配置中將current_session_context_class
指定為thread
或
<property name="current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
但這並不安全。 。
current_session_context_class
的可能值為jta
, thread
和managed
。 除此之外,休眠狀態還支持jta
和thread
。 thread
上下文用於大多數獨立的休眠應用程序中,或者基於輕量級框架(如Spring)的應用程序,而jta
用於Java EE環境。
還可以嘗試使用sessionFactory.getCurrentSession()
而不是SessionFactoryUtils.getSession()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.