关于Hibernate之Unknown service requested [org.hibernate.cache.spi.RegionFactory]这个坑!
程序员文章站
2022-04-15 23:15:17
...
这几天频繁被一个Hibernate的坑困扰。明明这个Dao被很多个Service调用,为什么新写的一个Service调用总是出现如下问题:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.cache.spi.RegionFactory]
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.orm.hibernate5.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:151)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
root cause
org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.cache.spi.RegionFactory]
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:184)
org.hibernate.cfg.Settings.getRegionFactory(Settings.java:300)
org.hibernate.internal.SessionFactoryImpl$SessionBuilderImpl.openSession(SessionFactoryImpl.java:1322)
org.hibernate.internal.SessionFactoryImpl.openSession(SessionFactoryImpl.java:677)
org.springframework.orm.hibernate5.SpringSessionContext.currentSession(SpringSessionContext.java:121)
org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:692)
com.supermap.zzsubcorp.iFacility.FacilityMetaDataEngine.dao.impl.FacilityServiceDaoImpl.getFacilityServices(FacilityServiceDaoImpl.java:82)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
进一步测试后发现,凡是这个Service调用任何Dao都会出现上面这个问题。更奇怪的是无论是在Service的Class头部加不加@Service都一样会报这个错。而国内的网站都说是因为Hibernate版本或配置的问题,纯属胡扯。。。。。。!
后来万般无奈查看Hibernate官网,才发现有外国大神跟我遇到过一样的问题。原因是我在Controller中注入Service时选择的是手动注入:
public AccessControlCardController() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext*.xml");
iIAccessControlCardService = (IAccessControlCardService)ctx.getBean("accessControlCardService");
ctx.close(); //原因出在这里了,执行这句就会导致上述Hibernate错误
}
看到了吧?把上面那行ctx.close();注释掉之后,一切问题都没有了,所有的Dao层调用都没有问题了。OMG!
现在翻回头来想想为什么其他Controller调用Service,Service再调用Dao就没问题?是因为其他的Controller里都是通过@Autowired的方式自动注入接口的,所以就避免了该问题。So dis nei!
老师说了,让我们在一个对象使用完毕后一定要记着释放,这到底是好习惯还是坏习惯!!!