Package org.sprimaudi.zkspring.util

Source Code of org.sprimaudi.zkspring.util.HibernateExtendedJpaDialect

package org.sprimaudi.zkspring.util;

import org.hibernate.Session;
import org.hibernate.TransactionException;
import org.hibernate.jdbc.Work;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import java.sql.Connection;
import java.sql.SQLException;

/**
* Created with IntelliJ IDEA.
* UserUser: UserUser
* Date: 8/3/12
* Time: 3:29 PM
* To change this template use File | Settings | File Templates.
*/
public class HibernateExtendedJpaDialect extends HibernateJpaDialect {

    private Logger logger = LoggerFactory.getLogger(HibernateExtendedJpaDialect.class);

    /**
     * This method is overridden to set custom isolation levels on the connection
     *
     * @param entityManager
     * @param definition
     * @return
     * @throws javax.persistence.PersistenceException
     *
     * @throws java.sql.SQLException
     * @throws org.hibernate.TransactionException
     *
     */
    @Override
    public Object beginTransaction(final EntityManager entityManager,
                                   final TransactionDefinition definition) throws PersistenceException,
            SQLException, TransactionException {
        Session session = (Session) entityManager.getDelegate();
        if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
            getSession(entityManager).getTransaction().setTimeout(definition.getTimeout());
        }

        entityManager.getTransaction().begin();
        logger.debug("Transaction started");

        session.doWork(new Work() {

            public void execute(Connection connection) throws SQLException {
                logger.debug("The connection instance is {}", connection);
                logger.debug("The isolation level of the connection is {} and the isolation level set on the transaction is {}",
                        connection.getTransactionIsolation(), definition.getIsolationLevel());
                DataSourceUtils.prepareConnectionForTransaction(connection, definition);
            }
        });

        return prepareTransaction(entityManager, definition.isReadOnly(), definition.getName());
    }

}
TOP

Related Classes of org.sprimaudi.zkspring.util.HibernateExtendedJpaDialect

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.