br.com.gauge.hibernate.impl.HibernatePersistenceContext.java Source code

Java tutorial

Introduction

Here is the source code for br.com.gauge.hibernate.impl.HibernatePersistenceContext.java

Source

/**
 *   Este codigo  software livre voc e pode resdistribuir e/ou modificar ele seguindo os termos da
 *   Creative Commons Attribution 4.0 International Pare visualizar uma copia desta 
 *   licensa em ingles visite http://creativecommons.org/licenses/by/4.0/.
 *   
 *   This code is free software; you can redistribute it and/or modify it
 *   under the terms of Creative Commons Attribution 4.0 International License. 
 *   To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/.
 */
package br.com.gauge.hibernate.impl;

import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import br.com.gauge.hibernate.PersistenceContext;

/**
 *  Implementacao de PersistenceContext que encapsula uma Session do hibernate.
 *
 * @author Rogerio Lima dos Santos
 * @version 1.00
 * @since Release 1 da aplicao
 */

public class HibernatePersistenceContext implements PersistenceContext {

    //private static final Logger logger = LoggerFactory.getLogger(HibernatePersistenceContext.class);

    /**
     * Timeout para as transacoes do Hibernate.
     */

    // TODO Subir este item para configuracao global
    private static final int TRANSACTION_TIMEOUT = 15;

    private static SessionFactory factory;

    private Session session;
    private Transaction trans;

    static {
        factory = new AnnotationConfiguration().configure().buildSessionFactory();
    }

    public HibernatePersistenceContext() {
        session = factory.openSession();
        session.setFlushMode(FlushMode.COMMIT);
        trans = session.beginTransaction();
        trans.setTimeout(TRANSACTION_TIMEOUT);
    }

    public Object getContext() {
        return session;
    }

    public void commitAndClose() {
        trans.commit();
        session.close();
    }

    public void rollbackAndClose() {
        try {
            trans.rollback();
        } catch (Exception e) {
            // Usualmente o rollback sao chamado em caso de erro. Nao vamos mascara-lo
            // com uma nova excecao, por isso logamos e ignoramos.

        }
        session.close();
    }

    public void detach(Object o) {
        session.evict(o);
    }

}