我有followig服務:HibernateException的:無法獲得交易同步會話當前線程的,雖然我調用事務方法
@Service
public class CompanyServiceImpl implements CompanyService {
@PostConstruct
public void init() {
this.refreshStopJobs();
}
@Transactional(readOnly = true)
@Override
public void refreshStopJobs() {
companyDao.getCompanysByStatus(CampaignStatus.START).forEach(this::refreshStopJob);
}
}
並按照道:
@SuppressWarnings("unchecked")
@Override
public List<Campaign> getCompanysByStatus(CampaignStatus campaignStatus) {
Criteria criteria = createCriteriaForGettingList(null, campaignStatus);
return criteria.list();
}
如果我跑我的應用程序見以下日誌:
2015-11-08 17:54:04.601:WARN:oejw.WebAppContext:main: Failed startup of context [email protected]{/,file:/D:/freelance/marcproject/src/main/webapp/,STARTING}{file:/D:/freelance/marcproject/src/main/webapp/}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'companyServiceImpl': Invocation of init method failed; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
.....
Caused by:
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at com.terminal.dao.impl.CompanyDaoImpl.createCriteriaForGettingList(CompanyDaoImpl.java:77)
at com.terminal.dao.impl.CompanyDaoImpl.getCompanysByStatus(CompanyDaoImpl.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201)
at com.sun.proxy.$Proxy80.getCompanysByStatus(Unknown Source)
at com.terminal.service.impl.CompanyServiceImpl.refreshStopJobs(CompanyServiceImpl.java:319)
at com.terminal.service.impl.CompanyServiceImpl.init(CompanyServiceImpl.java:313)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
if mark dao method getCompanysByStatus
as @Transactional
- 應用程序啓動正常。 但我不明白爲什麼。因爲我已經開始了服務方式的交易refreshStopJobs
[Spring @Transaction方法調用可能出現同一個類中的方法調用,不起作用?](http://stackoverflow.com/questions/3423972/spring-transaction-method-call-by-the-方法在同一類別不禾窩) – gstackoverflow