Thanks guys for your input.

Lance, I tried the following configuration without success, but then tried
the second configuration and succeeded. Could you take a quick peak to be
sure I'm doing it correctly.

-- cleanup in @AfterMethod = This failed with the following exception
Failed: org.hibernate.SessionException: Session is closed
-- cleanup in @AfterTest = Failed:java.lang.IllegalStateException: Method
org.apache.tapestry5.ioc.internal.RegisteryImpl.cleanupThread(RegistryImpl.java.519)
may no longer be involked

-- I found success moving before to @BeforeMethod and moving registry
shutdown into @AfterMethod.

public class RegistryBuilderTest {

    protected final Registry buildRegistry(Class... moduleClasses) {
        RegistryBuilder builder = new RegistryBuilder();
        builder.add(moduleClasses);
        return builder.build();
    }

}

public class SampleTest extends RegistryBuilderTest {

    private Registry registry;
    private HibernateSessionManager sessionManager;
    private Session session;

    @BeforeClass
    protected void before() {
        registry = buildRegistry(AppModuleTest.class,
HibernateCoreModule.class);
        sessionManager = registry.getService(HibernateSessionManager.class);
        session = sessionManager.getSession();
    }

    @Test
    public void test1() {
        System.out.println("test1");
        UserProfile userProfile = new UserProfile();
        userProfile.setShortname("test1");
        session.save(userProfile);

        session.flush();
        sessionManager.commit();

        assertEquals(getResultSize(UserProfile.class), 1);

    }

    @Test
    public void test2() {
        System.out.println("test2");
        UserProfile userProfile = new UserProfile();
        userProfile.setShortname("test2");
        session.save(userProfile);

        session.flush();
        sessionManager.commit();

        assertEquals(getResultSize(UserProfile.class), 1);

    }

    public int getResultSize(Class<?> clazz) {
        return session.createCriteria(clazz).list().size();
    }

    @AfterClass
    public void shutDown() {
        System.out.println("shutDown");
        registry.shutdown();
    }

    @AfterMethod
    public void cleanupThread() {
        System.out.println("cleanupThread");
        registry.cleanupThread();
    }

}





**** Working configuration ****


public class SampleTest extends RegistryBuilderTest {


    private Registry registry;
    private HibernateSessionManager sessionManager;
    private Session session;

    @BeforeMethod
    protected void before() {
        registry = buildRegistry(AppModuleTest.class,
HibernateCoreModule.class);
        sessionManager = registry.getService(HibernateSessionManager.class);
        session = sessionManager.getSession();
    }

    @Test
    public void test1() {
        System.out.println("test1");
        UserProfile userProfile = new UserProfile();
        userProfile.setShortname("test1");
        session.save(userProfile);

        session.flush();
        sessionManager.commit();

        assertEquals(getResultSize(UserProfile.class), 1);
    }

    @Test
    public void test2() {
        System.out.println("test2");
        UserProfile userProfile = new UserProfile();
        userProfile.setShortname("test2");
        session.save(userProfile);

        session.flush();
        sessionManager.commit();

        assertEquals(getResultSize(UserProfile.class), 1);
    }

    public int getResultSize(Class<?> clazz) {
        return session.createCriteria(clazz).list().size();
    }

    @AfterMethod
    public void cleanupThread() {
        System.out.println("cleanupThread");
        registry.cleanupThread();
        registry.shutdown();
    }

}



On Fri, Aug 29, 2014 at 11:33 AM, Lance Java <lance.j...@googlemail.com>
wrote:

> From the h2 docs here:
> http://www.h2database.com/html/features.html#in_memory_databases
>
> In some cases, only one connection to a in-memory database is required.
> This means the database to be opened is private. In this case, the database
> URL is jdbc:h2:mem: Opening two connections within the same virtual machine
> means opening two different (private) databases.
>
> Sometimes multiple connections to the same in-memory database are required.
> In this case, the database URL must include a name. Example:
> jdbc:h2:mem:db1. Accessing the same database using this URL only works
> within the same virtual machine and class loader environment.
>
> So, if you use a connection url of "jdbc:h2:mem:" AND you make sure to call
> registry.cleanupThread() in the @After of each test. You will get a new
> database for every test.
>



-- 
George Christman
www.CarDaddy.com
P.O. Box 735
Johnstown, New York

Reply via email to