Example usage for org.hibernate.internal StatelessSessionImpl close

List of usage examples for org.hibernate.internal StatelessSessionImpl close

Introduction

In this page you can find the example usage for org.hibernate.internal StatelessSessionImpl close.

Prototype

@Override
    public void close() 

Source Link

Usage

From source file:debop4k.data.orm.hibernate.StatelessEx.java

License:Apache License

public static <T> T withTransaction(@NonNull SessionFactory factory,
        @NonNull final Function1<StatelessSessionImpl, T> func) {
    StatelessSessionImpl stateless = of(factory);
    try {//from www . j a v  a2s .c  om
        Transaction tx = stateless.beginTransaction();
        try {
            T result = func.invoke(stateless);
            tx.commit();
            return result;
        } catch (HibernateException e) {
            log.error("Hibernate StatelessSession  ?  ?.", e);
            tx.rollback();
            return null;
        }
    } finally {
        if (stateless != null && !stateless.isClosed())
            stateless.close();
    }
}

From source file:debop4k.data.orm.hibernate.StatelessEx.java

License:Apache License

/**
 * DB?  ?  ? ./*from   ww  w.j  a  v a 2  s .  c o  m*/
 *
 * @param factory hibernate session factory
 * @param func    ? 
 * @param <T>     ?? ? 
 * @return ?? 
 * @deprecated {@literal @}Transactional(readOnly=true)  .
 */
@Deprecated
@Transactional(readOnly = true)
public static <T> T withReadOnly(@NonNull SessionFactory factory,
        @NonNull Function1<StatelessSessionImpl, T> func) {
    StatelessSessionImpl stateless = of(factory);
    try {
        Connection conn = stateless.connection();
        conn.setReadOnly(true);
        conn.setAutoCommit(false);
        Transaction tx = stateless.beginTransaction();
        try {
            T result = func.invoke(stateless);
            tx.commit();
            return result;
        } catch (Exception e) {
            log.error("Hibernate StatelessSession  ?  ?.", e);
            tx.rollback();
            return null;
        }
    } catch (SQLException e) {
        log.error("Connection ? ?  ?.", e);
        throw new RuntimeException(e);
    } finally {
        if (stateless != null && !stateless.isClosed())
            stateless.close();
    }
}