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;
}
Aggregations