Search in sources :

Example 1 with SqlRepositoryServiceImpl

use of com.evolveum.midpoint.repo.sql.SqlRepositoryServiceImpl in project midpoint by Evolveum.

the class TestSqlRepositoryBeanPostProcessor method postProcessAfterInitialization.

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if ((bean instanceof SqlRepositoryFactory) || (bean instanceof SessionFactory) || (bean instanceof SqlRepositoryServiceImpl)) {
        LOGGER.info("Post process: " + bean.getClass().getName());
    }
    if (!(bean instanceof SessionFactory)) {
        return bean;
    }
    LOGGER.info("Postprocessing session factory - removing everything from database if necessary.");
    TestSqlRepositoryFactory factory = context.getBean(TestSqlRepositoryFactory.class);
    //we'll attempt to drop database objects if configuration contains dropIfExists=true and embedded=false
    SqlRepositoryConfiguration config = factory.getSqlConfiguration();
    if (!config.isDropIfExists() || config.isEmbedded()) {
        LOGGER.info("We're not deleting objects from DB, drop if exists=false or embedded=true.");
        return bean;
    }
    LOGGER.info("Deleting objects from database.");
    SessionFactory sessionFactory = (SessionFactory) bean;
    Session session = sessionFactory.openSession();
    try {
        session.beginTransaction();
        Query query;
        if (useProcedure(factory.getSqlConfiguration())) {
            LOGGER.info("Using truncate procedure.");
            query = session.createSQLQuery("{ call " + TRUNCATE_PROCEDURE + "() }");
            query.executeUpdate();
        } else {
            LOGGER.info("Using truncate function.");
            query = session.createSQLQuery("select " + TRUNCATE_FUNCTION + "();");
            query.uniqueResult();
        }
        session.getTransaction().commit();
    } catch (Exception ex) {
        LOGGER.error("Couldn't cleanup database, reason: " + ex.getMessage(), ex);
        if (session != null && session.isOpen()) {
            Transaction transaction = session.getTransaction();
            if (transaction != null && transaction.isActive()) {
                transaction.rollback();
            }
        }
        throw new BeanInitializationException("Couldn't cleanup database, reason: " + ex.getMessage(), ex);
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }
    return bean;
}
Also used : SessionFactory(org.hibernate.SessionFactory) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Query(org.hibernate.Query) Transaction(org.hibernate.Transaction) SqlRepositoryFactory(com.evolveum.midpoint.repo.sql.SqlRepositoryFactory) SqlRepositoryServiceImpl(com.evolveum.midpoint.repo.sql.SqlRepositoryServiceImpl) BeansException(org.springframework.beans.BeansException) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) SqlRepositoryConfiguration(com.evolveum.midpoint.repo.sql.SqlRepositoryConfiguration) Session(org.hibernate.Session)

Aggregations

SqlRepositoryConfiguration (com.evolveum.midpoint.repo.sql.SqlRepositoryConfiguration)1 SqlRepositoryFactory (com.evolveum.midpoint.repo.sql.SqlRepositoryFactory)1 SqlRepositoryServiceImpl (com.evolveum.midpoint.repo.sql.SqlRepositoryServiceImpl)1 Query (org.hibernate.Query)1 Session (org.hibernate.Session)1 SessionFactory (org.hibernate.SessionFactory)1 Transaction (org.hibernate.Transaction)1 BeansException (org.springframework.beans.BeansException)1 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)1