de.decidr.model.testing.LowLevelDatabaseTest.java Source code

Java tutorial

Introduction

Here is the source code for de.decidr.model.testing.LowLevelDatabaseTest.java

Source

/*
 * The DecidR Development Team licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package de.decidr.model.testing;

import java.sql.Connection;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.junit.AfterClass;
import org.junit.BeforeClass;

/**
 * Creates a session for low-level database access.
 * 
 * @author Reinhold
 */
public abstract class LowLevelDatabaseTest extends DecidrDatabaseTest {
    protected static Session session = null;

    @BeforeClass
    public static final void setUpClass() {
        Configuration config = new Configuration();
        config.configure("/hibernate.cfg.xml");
        /*
         * Minimize concurrency issues.
         */
        config.setProperty("hibernate.connection.isolation",
                Integer.toString(Connection.TRANSACTION_READ_UNCOMMITTED));
        config.setProperty("hibernate.connection.autocommit", "true");
        session = config.buildSessionFactory().openSession();
    }

    @AfterClass
    public static final void tearDownClass() {
        if (session != null) {
            try {
                session.getTransaction().commit();
            } catch (HibernateException e) {
                // ignore if transaction not started.
            }
            session.flush();
            session.close();
        }
    }
}