Example usage for org.hibernate.engine.jdbc.connections.spi ConnectionProvider unwrap

List of usage examples for org.hibernate.engine.jdbc.connections.spi ConnectionProvider unwrap

Introduction

In this page you can find the example usage for org.hibernate.engine.jdbc.connections.spi ConnectionProvider unwrap.

Prototype

public <T> T unwrap(Class<T> unwrapType);

Source Link

Document

Unproxy the service proxy

Usage

From source file:org.springframework.orm.hibernate4.fix.SessionFactoryUtils.java

License:Apache License

/**
 * Determine the DataSource of the given SessionFactory.
 * @param sessionFactory the SessionFactory to check
 * @return the DataSource, or {@code null} if none found
 * @see org.hibernate.engine.spi.SessionFactoryImplementor#getConnectionProvider
 *///from w w w. java 2s .c o  m
public static DataSource getDataSource(SessionFactory sessionFactory) {
    if (sessionFactory instanceof SessionFactoryImplementor) {
        ConnectionProvider cp = ((SessionFactoryImplementor) sessionFactory).getConnectionProvider();
        return cp.unwrap(DataSource.class);
    }
    return null;
}

From source file:org.springframework.orm.hibernate5.SessionFactoryUtils.java

License:Apache License

/**
 * Determine the DataSource of the given SessionFactory.
 * @param sessionFactory the SessionFactory to check
 * @return the DataSource, or {@code null} if none found
 * @see ConnectionProvider/*from w  ww .  java  2  s.c  o m*/
 */
public static DataSource getDataSource(SessionFactory sessionFactory) {
    if (sessionFactory instanceof SessionFactoryImplementor) {
        try {
            ConnectionProvider cp = ((SessionFactoryImplementor) sessionFactory).getServiceRegistry()
                    .getService(ConnectionProvider.class);
            if (cp != null) {
                return cp.unwrap(DataSource.class);
            }
        } catch (UnknownServiceException ex) {
            if (logger.isDebugEnabled()) {
                logger.debug(
                        "No ConnectionProvider found - cannot determine DataSource for SessionFactory: " + ex);
            }
        }
    }
    return null;
}