【问题标题】:Spring test: UnknownServiceException: ConnectionProvider春季测试:UnknownServiceException:ConnectionProvider
【发布时间】:2013-12-19 13:28:54
【问题描述】:

我有许多启动 spring 上下文的测试类,每个测试类都包含 hibernate(和 EntityManagerFactory)。

一个自定义的 TestExecutionListener 确保 spring 上下文总是被标记为脏的,所以一个上下文不应该被重用。

在 spring 上下文的后续启动期间(不是第一次启动),我得到:

org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.jdbc.connections.spi.ConnectionProvider]

什么会导致这个异常?之前的 spring 上下文是否没有正确关闭?

编辑/添加:在启动期间尝试在 bean 中启动事务时,实际上会引发上述异常。 (Lifecycle.start())。所以问题是为什么在应该有的时候没有可用的hibernate SessionFactory - 并且对于创建的第一个spring测试上下文(对于第一个测试类)。

【问题讨论】:

    标签: spring hibernate testing


    【解决方案1】:

    该异常可能是由于在 SessionFactory 关闭时尝试创建新会话引起的

    确保您没有在非事务性测试中调用事务性方法。

    test context:

    In the TestContext framework, transactions are managed by the TransactionalTestExecutionListener, which is configured through the @TestExecutionListeners annotation by default, even if you do not explicitly declare @TestExecutionListeners on your test class. To enable support for transactions, however, you must provide a PlatformTransactionManager bean in the application context loaded by @ContextConfiguration semantics. In addition, you must declare @Transactional either at the class or method level.
    

    TransactionalTestExecutionListener:

    ... test methods that are not annotated with either @Transactional (at the class or method level) or @NotTransactional will not be run within a transaction.
    

    【讨论】:

    • 你的意思是@Transactional 应该放在所有可能使用数据库的测试上。这样的注释已经在 bean 的适当位置。我不明白为什么在加载 Spring Context 并且当前正在刷新(启动)时应该关闭 SessionFactory。
    • 我说的是 AbstractTransactionalJUnit4SpringContextTests。
    • 谢谢。但是测试本身不应该是事务性的。事务在某些 bean 中的“更深”级别开始。此外,使用 TransactionalTestExecutionListener 和 @Transactional 运行我的测试不会改变任何东西。
    【解决方案2】:

    原来这是由于对旧 spring 上下文(在自定义代码中)的引用泄漏造成的。

    【讨论】:

      猜你喜欢
      • 2011-09-26
      • 2012-09-12
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 2015-02-23
      • 2018-07-29
      • 2020-12-15
      • 1970-01-01
      相关资源
      最近更新 更多