Org.hibernate.service.UnknownServiceException: requested unknown service

I am writing unit test for my AbstractHibernateRepository persistence method. I use spring test runner, but when I get it, I get the following exception:

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 Configuration:

<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 causes this problem and how to fix it?

+5
source share
8 answers

, . @Before , save() , , , , , . , , @Before ( , , ). , ( ). , , , @Before , @Transactional, .

+5

, , [org.hibernate.cache.spi.RegionFactory], spring . - beanFactory org.springframework.transaction.interceptor.TransactionAspectSupport. org.springframework.transaction.interceptor.TransactionAspectSupport # clearTransactionManagerCache.

+4

. . - .

ServiceRegistryBuilder.destroy() sessionFactoryCreated, sessionFactoryClosed.

, , , Hibernate .

, , , , .

+1

Google:

, ​​sql script, . , , , UnknownServiceException - .

+1

, . . . .

0

Hibernate. : Session session = XmlSessionUtil.getSessionFactory().openSession(); session.close();. , . .

0

Hibernate Framework. , . .

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

. , .

0

. , kill -9 tomcat. . , , , .

0

All Articles