当前位置: 首页 > 面试题库 >

UnknownServiceException:请求了未知服务(Hibernate/Spring)

向实
2023-03-14
问题内容

这个问题困扰我好几天了..

我试图从我的当前会话,sessionFactor但收到org.hibernate.service.UnknownServiceException: Unknown service requested异常。

我不知道是什么原因引起的,或者这个异常的含义是什么。异常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)这样

@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

并没有明显地思考就关闭了它。



 类似资料:
  • 问题内容: 我有一个简单的类,它开始3个线程,并在每个线程中保存一个新对象。但是我遇到了我无法理解的例外。谁能帮我理解为什么会例外? 这是显示异常的日志文件的相关部分: 编辑1 问题答案: Hibernate中的会话对象不是线程安全的,除非您同步访问会话对象,否则不应在不同线程中使用同一会话。

  • 问题内容: 我正在为AbstractHibernateRepository保存方法编写单元测试。我正在使用弹簧测试运行器,但运行时出现以下异常: 我的测试: 仓库: 弹簧配置: 是什么导致此问题,如何解决? 问题答案: 我可能完全不在这里,但是对我来说,这似乎是会话处理异常。在打开和关闭会话中,然后在当前会话中,这可能是您刚刚关闭的会话,从而导致异常。如果您不关闭它,请尝试一下它是否起作用(我知道

  • 我试图使用更新多个项目,但出现以下错误: 我的代码如下: 任何帮助都是非常可观的。提前道谢。

  • 问题内容: 我在页面上有几个更新面板和jquery选项卡。另外,我正在更新面板上加载几个用户控件。用户等待几分钟后(未检查的时间约为40分钟)。当用户从“提交”按钮发送请求时,出现以下错误? 我无法解决此问题。但我相信。这是由Ajax引起的。大师,如果您知道解决方案。请告诉我。 问题答案: 当您有一个控件注册为多个更新面板中时,有时会出现此问题。 如果这不是问题,请尝试在脚本管理器声明之后立即添加

  • Python 的标准库 urllib 提供了大部分 HTTP 功能,但使用起来较繁琐。通常,我们会使用另外一个优秀的第三方库:Requests,它的标语是:Requests: HTTP for Humans。 Requests 提供了很多功能特性,几乎涵盖了当今 Web 服务的需求,比如: 浏览器式的 SSL 验证 身份认证 Keep-Alive & 连接池 带持久 Cookie 的会话 流下载

  • 我想使用cli复制此Spring初始化设置。 然而,每当我尝试将“rest”添加为依赖项时,该命令就会返回以下错误消息,即rest存储库在rest之上公开Spring数据存储库。 下面是我正在运行的命令: 如果rest不是正确的依赖项名称,那么是什么?此外,如何从命令行列出可用的依赖项以获得正确的名称。