这个问题困扰我好几天了..
我试图从我的当前会话,sessionFactor但收到org.hibernate.service.UnknownServiceException: Unknown service requested异常。
sessionFactor
org.hibernate.service.UnknownServiceException: Unknown service requested
我不知道是什么原因引起的,或者这个异常的含义是什么。异常getCurrentSession()被调用时引发。
getCurrentSession()
@Component public class RestaurantOwnerRepository implements RestauranOwnerDAO { private SessionFactory sessionFactory; public RestaurantOwnerRepository(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override @Transactional public List<Restaurant> getAvailableRestaurants(String sessionId) { Session session = sessionFactory.getCurrentSession(); // .. } }
我在servlet中调用的代码:
@Configurable public class RestaurantInformationServiceImpl extends XsrfProtectedServiceServlet implements RestaurantInformationService { private static final long serialVersionUID = -4088840947018614411L; private final static Logger logger = Logger.getLogger(RestaurantInformationServiceImpl.class); @Autowired public RestauranOwnerDAO restaurantOwnerRepository; @Override public void init(ServletConfig config) throws ServletException { super.init(config); ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/appServlet/servlet-context.xml"); this.restaurantOwnerRepository = (RestauranOwnerDAO)applicationContext.getBean("restaurantOwnerRepository"); ((ConfigurableApplicationContext)applicationContext).close(); } @Override public List<RestaurantDTO> getAvailableRestaurants() { List<Restaurant> availableRestaurants = restaurantOwnerRepository.getAvailableRestaurants(getSessionId()); // .. } }
这是我的 servlet-context.xml 文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <mvc:annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <mvc:resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <context:component-scan base-package="com.mahlzeit.web.server" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mahlzeit_db" /> <property name="username" value="root" /> <property name="password" value="" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:hibernate-webserver.cfg.xml" /> </bean> <!-- <tx:annotation-driven transaction-manager="transactionManager"/> --> <tx:annotation-driven/> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="restaurantOwnerRepository" class="com.mahlzeit.web.server.dao.RestaurantOwnerRepository"> <constructor-arg> <ref bean="sessionFactory" /> </constructor-arg> </bean> </beans>
这是我的 hibernate-webserver.cfg.xml 文件:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.url">jdbc:mysql://localhost:3306/mahlzeit_db</property> <property name="connection.username">root</property> <property name="connection.password"></property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Batch size --> <!-- <property name="hibernate.jdbc.batch_size">20</property> --> <!-- Echo all executed SQL to stdout --> <property name="show_sql">false</property> <property name="format_sql">false</property> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hibernate.hbm2ddl.auto">create-drop</property> --> <!-- Run Hibernate in update mode --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- Bind the getCurrentSession() method to the thread --> <!-- <property name="current_session_context_class">thread</property> --> <!-- Mappings ... --> </session-factory> </hibernate-configuration>
错误消息/堆栈跟踪:
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract java.util.List com.mahlzeit.web.shared.service.restaurant.RestaurantInformationService.getAvailableRestaurants()' threw an unexpected exception: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.jdbc.connections.spi.ConnectionProvider] at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:415) at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:605) at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:333) at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:303) at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:373) at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62) at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745) Caused by: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.jdbc.connections.spi.ConnectionProvider] at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:126) at org.hibernate.internal.AbstractSessionImpl.getJdbcConnectionAccess(AbstractSessionImpl.java:261) at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.<init>(JdbcCoordinatorImpl.java:97) at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.<init>(TransactionCoordinatorImpl.java:87) at org.hibernate.internal.SessionImpl.<init>(SessionImpl.java:254) at org.hibernate.internal.SessionFactoryImpl$SessionBuilderImpl.openSession(SessionFactoryImpl.java:1609) at org.hibernate.internal.SessionFactoryImpl.openSession(SessionFactoryImpl.java:973) at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:122) at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988) at com.mahlzeit.web.server.dao.RestaurantOwnerRepository.getAvailableRestaurants(RestaurantOwnerRepository.java:62) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:266) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy.$Proxy50.getAvailableRestaurants(Unknown Source) at com.mahlzeit.web.server.service.restaurant.RestaurantInformationServiceImpl.getAvailableRestaurants(RestaurantInformationServiceImpl.java:50) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:587) ... 25 more
我只是不知道如何解决这个问题。任何帮助,将不胜感激!
所以,我不知道为什么要对我发表评论
((ConfigurableApplicationContext)applicationContext).close();
在init(ServletConfig config)这样
init(ServletConfig config)
@Override public void init(ServletConfig config) throws ServletException { super.init(config); ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/appServlet/servlet-context.xml"); this.restaurantOwnerRepository = (RestauranOwnerDAO)applicationContext.getBean("restaurantOwnerRepository"); //((ConfigurableApplicationContext)applicationContext).close(); }
但 没有关闭 应用程序上下文肯定在这里有所帮助。
我没想到 我只是遵循Eclipse中的警告
Resource leak: 'applicationContext' is never closed
并没有明显地思考就关闭了它。