Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy Hibernate session fetching fails in this code piece?
    primarykey
    data
    text
    <p>I am experimenting with Hibernate 4.1.2. </p> <p>Wrote the following class to help me get a session using new ServiceRegistry approach</p> <p>=====================================================</p> <pre><code>package com.debaself.samples; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class SessionFetch { private static SessionFactory sessionFactory = null; public SessionFactory getSessionFactory(){ if(sessionFactory == null){ Configuration cfg = new AnnotationConfiguration().addResource("hibernate.cfg.xml").configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); sessionFactory = cfg.buildSessionFactory(serviceRegistry); } return sessionFactory ; } public Session getSession(){ return getSessionFactory().openSession(); } } </code></pre> <p>=============================================== </p> <p>Now I wrote a test</p> <p>===============================================</p> <pre><code>public class SessionFetchTest { @Test public void getSessionFactoryTest(){ SessionFetch fetch = new SessionFetch(); SessionFactory factory = fetch.getSessionFactory(); assertNotNull(factory); assert(!factory.isClosed()); factory.close(); } @Test public void getSessionTest(){ SessionFetch fetch = new SessionFetch(); Session session = fetch.getSession(); assert(session.isOpen()); assert(session.isConnected()); } } </code></pre> <p>==================================================</p> <p>The weirdness is </p> <p>when I run the test methods individually both tests succeed. But when I run them in one go the getSessionTest() invariably fails and throws an UnknownServiceException. </p> <p>Can anyone explain this behaviour please?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload