org.hibernate.service.UnknownServiceException: Unknown service requested
Asked Answered
I

8

7

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:

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?

Implosive answered 20/10, 2014 at 15:25 Comment(0)
O
6

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.

Onshore answered 20/10, 2014 at 15:49 Comment(2)
i am also facing same issue at tomcat server startUp while upgrading jsf version from 2.1.25 to 2.2.0 any suggestions.Impresa
Do you have any idea how to solve this problem #31694747 ?Berstine
K
4

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.

Karalee answered 31/8, 2016 at 19:48 Comment(2)
How do you call it if it is protected?Anderegg
@GeorgeVinokhodov probably he extended that class and called itWartburg
S
2

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 sessionFactoryCreatedmethod rather than my sessionFactoryClosed method.

Basically, I destroyed my service registry then tried to get a new session, and this makes Hibernate produce the misleading error message.

Therefore, I suggest if people get this error, check they are not closing their session or registry and then trying to get it again.

Sinh answered 28/5, 2015 at 16:3 Comment(0)
T
2

For any future Google searchers:

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.

Tribrach answered 13/10, 2015 at 14:42 Comment(0)
S
1

Its Nothing , its cache problem . just close your server and clean your tomcat . then restart . I solved it like this.

Stuart answered 15/3, 2018 at 16:16 Comment(0)
T
1

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.

Typist answered 20/9, 2019 at 14:0 Comment(0)
A
0

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.

Actomyosin answered 8/6, 2018 at 17:24 Comment(1)
but we have to close the session.Windhoek
W
0

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.

session.close();
sessionFactory.close();

It worked for me. Hope it will work for you.

Windhoek answered 8/8, 2018 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.