Java org.hibernate.service.UnknownServiceException:请求未知服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26469263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.hibernate.service.UnknownServiceException: Unknown service requested
提问by user2054833
I am writing a unit test to for my AbstractHibernateRepository save method. I'm using spring test runner but I get the following exception when it runs:
我正在为我的 AbstractHibernateRepository 保存方法编写单元测试。我正在使用 spring 测试运行器,但在运行时出现以下异常:
org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:201)
My Test:
我的测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/spring-hibernate.xml")
public class AbstractHibernateRepoTest extends AbstractHibernateRepo<Video> {
@Autowired private SessionFactory sessionFactory;
private Video video;
public AbstractHibernateRepoTest()
{
super(Video.class);
}
@Before
public void setUp ()
{
video = new Video();
video.setId("xyz");
video.setName("Video Name");
video.setSrc("Source");
video.setThumbnail("Thumbnail");
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(video) ;
session.close();
}
@Test
public void testSaveMethod ()
{
video.setId("asa");
String id = (String) save(video);
Assert.assertEquals(video.getId(), id);
}
@After
public void breakDown ()
{
sessionFactory.close();
}
}
Repository:
存储库:
@Autowired private SessionFactory sessionFactory;
private final Class<T> clazz;
public AbstractHibernateRepo(Class<T> clazz)
{
this.clazz = clazz;
}
@SuppressWarnings("unchecked")
@Transactional(rollbackFor = HibernateException.class)
@Override
public T findById(Serializable id)
{
if (id == null)
throw new NullPointerException();
return (T) getSessionFactory().getCurrentSession().get(getClazz(), id);
}
@Override
@Transactional(rollbackFor = HibernateException.class)
public Serializable save(T entity)
{
if (entity == null)
throw new NullPointerException();
return getSessionFactory().getCurrentSession().save(entity);
}
@Override
@Transactional(rollbackFor = HibernateException.class)
public void delete(T entity)
{
if (entity == null)
throw new NullPointerException();
getSessionFactory().getCurrentSession().delete(entity);
}
@Override
@Transactional(rollbackFor = HibernateException.class)
public void update(T entity)
{
if (entity == null)
throw new NullPointerException();
getSessionFactory().getCurrentSession().update(entity);
}
}
Spring Config:
弹簧配置:
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE"/>
<property name="username" value="someuser"/>
<property name="password" value="somepassword"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.package.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>
What's causing this problem and how can I fix it?
是什么导致了这个问题,我该如何解决?
采纳答案by Predrag Maric
I might be totally off here, but to me this seems to be a session handling exception. In @Before
you open and close session, then in save()
you get the current session, which is maybe the one you just closed, leading to an exception. Try if it works if you don't close it in @Before
(I know it's not the solution, just to test the theory). You can also try opening a new session in repository instead of getting the current one (also not the solution). The only difference I see compared with our working test setup is that in @Before
we also call our repository methods, marked as @Transactional
, instead of creating a session directly.
我可能完全不在这儿,但对我来说这似乎是一个会话处理异常。在@Before
您打开和关闭会话时,save()
您将获得当前会话,这可能是您刚刚关闭的会话,从而导致异常。如果您不关闭它,请尝试它是否有效@Before
(我知道这不是解决方案,只是为了测试理论)。您还可以尝试在存储库中打开一个新会话,而不是获取当前会话(也不是解决方案)。与我们的工作测试设置相比,我看到的唯一区别是,@Before
我们还调用了我们的存储库方法,标记为@Transactional
,而不是直接创建会话。
回答by Steve Single
I ran into this same error. I discovered the cause in my case. My experience may help someone else.
我遇到了同样的错误。我在我的案例中发现了原因。我的经验可能会帮助别人。
I was calling ServiceRegistryBuilder.destroy()
in my sessionFactoryCreated
method rather than my sessionFactoryClosed
method.
我正在调用ServiceRegistryBuilder.destroy()
我的sessionFactoryCreated
方法而不是我的sessionFactoryClosed
方法。
Basically, I destroyed my service registry then tried to get a new session, and this makes Hibernate produce the misleading error message.
基本上,我销毁了我的服务注册表,然后尝试获取一个新会话,这使得 Hibernate 产生了误导性的错误消息。
Therefore, I suggest if people get this error, check they are not closing their session or registry and then trying to get it again.
因此,我建议如果人们收到此错误,请检查他们是否关闭了会话或注册表,然后再次尝试获取它。
回答by Charlie Hurrell
For any future Google searchers:
对于任何未来的 Google 搜索者:
When I saw this problem it was caused by an error in the sql script that was being run before the tests started. Scrolling back far enough through the logs revealed the error, so it's worth looking back to check that an UnknownServiceException
isn't just a side effect of another problem.
当我看到这个问题时,它是由测试开始前正在运行的 sql 脚本中的错误引起的。回滚到足够远的日志会发现错误,因此值得回顾一下以检查它UnknownServiceException
是否不仅仅是另一个问题的副作用。
回答by user6780416
I ran into a similar error except the unknown service was [org.hibernate.cache.spi.RegionFactory] which only occurred when the spring context was started a second time. The problem was due to a partially destroyed beanFactory and transaction manager cache in org.springframework.transaction.interceptor.TransactionAspectSupport. The solution was to call org.springframework.transaction.interceptor.TransactionAspectSupport#clearTransactionManagerCache.
我遇到了类似的错误,除了未知服务是 [org.hibernate.cache.spi.RegionFactory] 仅在第二次启动 spring 上下文时发生。问题是由于 org.springframework.transaction.interceptor.TransactionAspectSupport 中 beanFactory 和事务管理器缓存部分被破坏。解决方案是调用 org.springframework.transaction.interceptor.TransactionAspectSupport#clearTransactionManagerCache。
回答by Ramanpreet Singh
Its Nothing , its cache problem . just close your server and clean your tomcat . then restart . I solved it like this.
它没有,它的缓存问题。只需关闭您的服务器并清理您的 tomcat 。然后重新启动。我是这样解决的。
回答by Sir Montes
I found out this error during Hibernate unit test creating. I created session by my util class: Session session = XmlSessionUtil.getSessionFactory().openSession();
In one test was session closed by session.close();
When I removed statement closing session all tests passed. So in my case was exception reason closed session.
我在 Hibernate 单元测试创建过程中发现了这个错误。我通过我的 util 类创建了会话:Session session = XmlSessionUtil.getSessionFactory().openSession();
在一个测试中,会话被关闭,session.close();
当我删除语句关闭会话时,所有测试都通过了。所以在我的情况下是异常原因关闭会话。
回答by akash ingle
I got this error during working with hibernate framework. So getting rid off from this error I change the sequence of closing session and session factory. like this.
我在使用 hibernate 框架时遇到了这个错误。所以摆脱这个错误我改变了关闭会话和会话工厂的顺序。像这样。
session.close();
sessionFactory.close();
It worked for me. Hope it will work for you.
它对我有用。希望它对你有用。
回答by Spencer Stewart
I'm adding onto another answer here from Ramanpreet Singh. I experienced this issue when I used kill -9
on tomcat. I can reliably re-create the problem this way. I have to start up my server, gracefully close it, then start it up again to clear up the problem.
我在这里添加了来自 Ramanpreet Singh 的另一个答案。我kill -9
在tomcat上使用时遇到过这个问题。我可以通过这种方式可靠地重新创建问题。我必须启动我的服务器,优雅地关闭它,然后再次启动它以解决问题。