kr.debop4j.data.hibernate.tools.StatelessTool.java Source code

Java tutorial

Introduction

Here is the source code for kr.debop4j.data.hibernate.tools.StatelessTool.java

Source

/*
 * Copyright 2011-2013 the original author or authors.
 *
 * Licensed 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 kr.debop4j.data.hibernate.tools;

import kr.debop4j.core.Action1;
import kr.debop4j.core.Guard;
import kr.debop4j.data.hibernate.unitofwork.UnitOfWorks;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.Session;
import org.hibernate.StatelessSession;
import org.hibernate.Transaction;

import java.sql.Connection;

/**
 * StatelessSession ? ?   ??  ?.
 *
 * @author ? ( sunghyouk.bae@gmail.com )
 * @since 13. 2. 19.
 */
@Slf4j
public class StatelessTool {

    private StatelessTool() {
    }

    /**
     * Execute transactional.
     *
     * @param action the action
     */
    public static void executeTransactional(Action1<StatelessSession> action) {
        executeTransactional(UnitOfWorks.getCurrentSession(), action);
    }

    /**
     *  session StatelessSession? ?  ? , .  @param session the session
     *
     * @param action the action
     */
    public static void executeTransactional(Session session, Action1<StatelessSession> action) {
        if (log.isDebugEnabled())
            log.debug("StatelessSession? ? Transaction ?  ? .");

        StatelessSession stateless = openStatelessSession(session);
        Transaction tx = null;

        try {
            tx = stateless.beginTransaction();
            action.perform(stateless);
            tx.commit();
        } catch (Exception e) {
            log.error("StatelessSession? ? ? . rollback .", e);
            if (tx != null)
                tx.rollback();
            throw new RuntimeException(e);
        } finally {
            stateless.close();
        }
    }

    /**
     * Execute transactional.
     *
     * @param session the session
     * @param actions the actions
     */
    public static void executeTransactional(Session session, Iterable<Action1<StatelessSession>> actions) {
        if (log.isDebugEnabled())
            log.debug("StatelessSession? ? Transaction ?  ? .");

        StatelessSession stateless = openStatelessSession(session);
        Transaction tx = null;

        try {
            tx = stateless.beginTransaction();
            for (Action1<StatelessSession> action : actions) {
                action.perform(stateless);
            }
            tx.commit();
        } catch (Exception e) {
            log.error("StatelessSession? ? . rollback .", e);
            if (tx != null)
                tx.rollback();
            throw new RuntimeException(e);
        } finally {
            stateless.close();
        }
    }

    /**
     * Execute the action
     *
     * @param action the action
     */
    public static void execute(Action1<StatelessSession> action) {
        execute(UnitOfWorks.getCurrentSession(), action);
    }

    /**
     * Execute the action.
     *
     * @param session the session
     * @param action  the action
     */
    public static void execute(Session session, Action1<StatelessSession> action) {
        if (log.isDebugEnabled())
            log.debug("StatelessSession? ?  ? .");

        StatelessSession stateless = openStatelessSession(session);
        try {
            action.perform(stateless);
        } catch (Exception e) {
            log.error("StatelessSession? ? .", e);
            throw new RuntimeException(e);
        } finally {
            stateless.close();
        }
    }

    /**
     * Execute the actions.
     *
     * @param session the session
     * @param actions the actions
     */
    public static void execute(Session session, Iterable<Action1<StatelessSession>> actions) {
        if (log.isDebugEnabled())
            log.debug("StatelessSession? ?  ? .");

        StatelessSession stateless = openStatelessSession(session);
        try {
            for (Action1<StatelessSession> action : actions)
                action.perform(stateless);
        } catch (Exception e) {
            log.error("StatelessSession? ? .", e);
            throw new RuntimeException(e);
        } finally {
            stateless.close();
        }
    }

    /**
     * Open stateless session.
     *
     * @param session the session
     * @return the stateless session
     */
    public static StatelessSession openStatelessSession(Session session) {
        Guard.shouldNotBeNull(session, "session");
        return session.getSessionFactory().openStatelessSession();
    }

    /**
     * Open stateless session.
     *
     * @param connection the connection
     * @return the stateless session
     */
    public static StatelessSession openStatelessSession(Connection connection) {
        Guard.shouldNotBeNull(connection, "connection");
        return UnitOfWorks.getCurrentSessionFactory().openStatelessSession(connection);
    }
}