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

com.github.fluent.hibernate.cfg.SessionControlHibernate4 Maven / Gradle / Ivy

Go to download

A library to work with Hibernate by fluent API. This library hasn't dependencies except Hibernate dependencies. It requires Java 1.6 and above.

The newest version!
package com.github.fluent.hibernate.cfg;

import java.lang.reflect.Method;

import org.hibernate.Session;
import org.hibernate.StatelessSession;

import com.github.fluent.hibernate.internal.util.InternalUtils;
import com.github.fluent.hibernate.internal.util.reflection.ReflectionUtils;

/**
 *
 * @author V.Ladynev
 */
class SessionControlHibernate4 implements ISessionControl {

    private static Method sessionClose = getCloseMethod(Session.class);

    private static Method statelessSessionClose = getCloseMethod(StatelessSession.class);

    @Override
    public void close(Session session) {
        if (session == null || !session.isOpen()) {
            return;
        }

        try {
            ReflectionUtils.invoke(session, sessionClose);
        } catch (Exception ex) {
            throw InternalUtils.toRuntimeException("Can't close Session using a reflection.", ex);
        }
    }

    @Override
    public void close(StatelessSession session) {
        if (session == null) {
            return;
        }

        try {
            ReflectionUtils.invoke(session, statelessSessionClose);
        } catch (Exception ex) {
            throw InternalUtils
                    .toRuntimeException("Can't close StatelessSession using a reflection.", ex);
        }

    }

    private static Method getCloseMethod(Class classFrom) {
        try {
            return ReflectionUtils.extractMethod(classFrom, "close");
        } catch (Exception ex) {
            throw InternalUtils.toRuntimeException(String.format(
                    "Can't get %s close() method by a reflection.", classFrom.getSimpleName()), ex);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy