All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.catools.etl.dao.CEtlBaseDao Maven / Gradle / Ivy

There is a newer version: 0.1.25
Show newest version
package org.catools.etl.dao;

import org.catools.common.collections.interfaces.CCollection;
import org.catools.common.json.CJsonUtil;
import org.hibernate.SessionFactory;
import org.hibernate.stat.Statistics;
import org.slf4j.Logger;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

public class CEtlBaseDao {
    private static EntityManagerFactory entityManagerFactory = null;

    static {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            closeEntityManagerFactory();
        }));
    }

    protected static  T find(Logger logger, Class entityClass, Object primaryKey) {
        return getTransactionResult(logger, session -> session.find(entityClass, primaryKey));
    }

    protected static  void persist(Logger logger, T record) {
        doTransaction(logger, session -> session.persist(record));
    }

    protected static  void merge(Logger logger, T record) {
        doTransaction(logger, session -> session.merge(record));
    }

    protected static  void bulkMerge(Logger logger, CCollection records) {
        bulk(logger, records, (session, record) -> session.merge(record));
    }

    protected static  void bulk(Logger logger, CCollection records, BiConsumer action) {
        doTransaction(logger, session -> {
            int counter = 0;
            for (T record : records) {
                action.accept(session, record);
                if (++counter % 100 == 0) {
                    session.flush();
                    session.clear();
                }
            }
            session.flush();
            session.clear();
        });
    }

    protected static  T getTransactionResult(Logger logger, Function action) {
        EntityManager session = getEntityManager();
        EntityTransaction tx = null;
        try {
            tx = session.getTransaction();
            tx.begin();
            T result = action.apply(session);
            tx.commit();
            return result;
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            logger.error("Failed To Perform Transaction.", e);
            throw e;
        } finally {
            session.close();
        }
    }

    protected static void doTransaction(Logger logger, Consumer action) {
        EntityManager session = getEntityManager();
        EntityTransaction tx = null;
        try {
            tx = session.getTransaction();
            tx.begin();
            action.accept(session);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            logger.error("Failed To Perform Transaction.", e);
            throw e;
        } finally {
            session.close();
        }
    }

    protected static synchronized EntityManagerFactory getEntityManagerFactory() {
        if (entityManagerFactory == null) {
            entityManagerFactory = Persistence.createEntityManagerFactory("CEtlPersistence");
        }
        return entityManagerFactory;
    }

    protected static synchronized void closeEntityManagerFactory() {
        if (entityManagerFactory != null && entityManagerFactory.isOpen()) {
            entityManagerFactory.close();
        }
    }

    protected static EntityManager getEntityManager() {
        EntityManager entityManager = getEntityManagerFactory().createEntityManager();
        return entityManager;
    }

    // We save this method for debugging propose
    protected static void printStats(Logger logger) {
        Statistics stats = entityManagerFactory.unwrap(SessionFactory.class).getStatistics();
        stats.setStatisticsEnabled(true);
        logger.info("==============================================================");
        logger.info("Fetch=%d, 2L Hit=%d, 2L Miss=%d, 2L Put=%d",
                stats.getEntityFetchCount(),
                stats.getSecondLevelCacheHitCount(),
                stats.getSecondLevelCacheMissCount(),
                stats.getSecondLevelCachePutCount());
        for (String entityName : stats.getEntityNames()) {
            logger.info(entityName + " ::> " + CJsonUtil.toString(stats.getEntityStatistics(entityName)));
        }
//
//        logger.info("Query Hit Count=" + stats.getQueryCacheHitCount());
//        logger.info("Query Miss Count=" + stats .getQueryCacheMissCount());
//        logger.info("Query Put Count=" + stats.getQueryCachePutCount());
//
//        logger.info("NaturalId Hit Count=" + stats.getNaturalIdCacheHitCount());
//        logger.info("NaturalId Miss Count=" + stats .getNaturalIdCacheMissCount());
//        logger.info("NaturalId Put Count=" + stats.getNaturalIdCachePutCount());
//
        logger.info("==============================================================");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy