Search in sources :

Example 1 with ConnectionProvider

use of org.hibernate.connection.ConnectionProvider in project xwiki-platform by xwiki.

the class HibernateStore method shutdownHibernate.

/**
 * Allows to shut down the hibernate configuration Closing all pools and connections.
 */
public void shutdownHibernate() {
    Session session = getCurrentSession();
    closeSession(session);
    // Close all connections
    if (getSessionFactory() != null) {
        // Note that we need to do the cast because this is how Hibernate suggests to get the Connection Provider.
        // See http://bit.ly/QAJXlr
        ConnectionProvider connectionProvider = ((SessionFactoryImplementor) getSessionFactory()).getConnectionProvider();
        connectionProvider.close();
    }
}
Also used : SessionFactoryImplementor(org.hibernate.engine.SessionFactoryImplementor) Session(org.hibernate.Session) ConnectionProvider(org.hibernate.connection.ConnectionProvider)

Example 2 with ConnectionProvider

use of org.hibernate.connection.ConnectionProvider in project xwiki-platform by xwiki.

the class HibernateStore method getDatabaseMetaData.

/**
 * Retrieve metadata about the database used (name, version, etc).
 * <p>
 * Note that the database metadata is not cached and it's retrieved at each call. If all you need is the database
 * product name you should use {@link #getDatabaseProductName()} instead, which is cached.
 * </p>
 *
 * @return the database meta data or null if an error occurred
 * @since 6.1M1
 */
public DatabaseMetaData getDatabaseMetaData() {
    DatabaseMetaData result;
    Connection connection = null;
    // Note that we need to do the cast because this is how Hibernate suggests to get the Connection Provider.
    // See http://bit.ly/QAJXlr
    ConnectionProvider connectionProvider = ((SessionFactoryImplementor) getSessionFactory()).getConnectionProvider();
    try {
        connection = connectionProvider.getConnection();
        result = connection.getMetaData();
    } catch (SQLException ignored) {
        result = null;
    } finally {
        if (connection != null) {
            try {
                connectionProvider.closeConnection(connection);
            } catch (SQLException ignored) {
            // Ignore
            }
        }
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.SessionFactoryImplementor) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) ConnectionProvider(org.hibernate.connection.ConnectionProvider)

Example 3 with ConnectionProvider

use of org.hibernate.connection.ConnectionProvider in project xwiki-platform by xwiki.

the class XWikiHibernateStore method injectInSessionFactory.

private SessionFactory injectInSessionFactory(Configuration config) throws XWikiException {
    SessionFactoryImpl sfactory = (SessionFactoryImpl) config.buildSessionFactory();
    Settings settings = sfactory.getSettings();
    ConnectionProvider provider = ((SessionFactoryImpl) getSessionFactory()).getSettings().getConnectionProvider();
    Field field = null;
    try {
        field = settings.getClass().getDeclaredField("connectionProvider");
        field.setAccessible(true);
        field.set(settings, provider);
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_MAPPING_INJECTION_FAILED, "Mapping injection failed", e);
    }
    return sfactory;
}
Also used : Field(java.lang.reflect.Field) SessionFactoryImpl(org.hibernate.impl.SessionFactoryImpl) Settings(org.hibernate.cfg.Settings) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) XWikiException(com.xpn.xwiki.XWikiException) ConnectionProvider(org.hibernate.connection.ConnectionProvider)

Example 4 with ConnectionProvider

use of org.hibernate.connection.ConnectionProvider in project xwiki-platform by xwiki.

the class XWikiHibernateStore method injectCustomMappingsInSessionFactory.

private SessionFactory injectCustomMappingsInSessionFactory(XWikiDocument doc, XWikiContext context) throws XWikiException {
    // If we haven't turned of dynamic custom mappings we should not inject them
    if (!context.getWiki().hasDynamicCustomMappings()) {
        return getSessionFactory();
    }
    boolean result = injectCustomMappings(doc, context);
    if (!result) {
        return getSessionFactory();
    }
    Configuration config = getConfiguration();
    SessionFactoryImpl sfactory = (SessionFactoryImpl) config.buildSessionFactory();
    Settings settings = sfactory.getSettings();
    ConnectionProvider provider = ((SessionFactoryImpl) getSessionFactory()).getSettings().getConnectionProvider();
    Field field = null;
    try {
        field = settings.getClass().getDeclaredField("connectionProvider");
        field.setAccessible(true);
        field.set(settings, provider);
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_MAPPING_INJECTION_FAILED, "Mapping injection failed", e);
    }
    return sfactory;
}
Also used : Field(java.lang.reflect.Field) Configuration(org.hibernate.cfg.Configuration) SessionFactoryImpl(org.hibernate.impl.SessionFactoryImpl) Settings(org.hibernate.cfg.Settings) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) XWikiException(com.xpn.xwiki.XWikiException) ConnectionProvider(org.hibernate.connection.ConnectionProvider)

Aggregations

ConnectionProvider (org.hibernate.connection.ConnectionProvider)4 SQLException (java.sql.SQLException)3 XWikiException (com.xpn.xwiki.XWikiException)2 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)2 Field (java.lang.reflect.Field)2 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)2 Settings (org.hibernate.cfg.Settings)2 SessionFactoryImplementor (org.hibernate.engine.SessionFactoryImplementor)2 SessionFactoryImpl (org.hibernate.impl.SessionFactoryImpl)2 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)2 InitializationException (org.xwiki.component.phase.InitializationException)2 QueryException (org.xwiki.query.QueryException)2 UnexpectedException (org.xwiki.store.UnexpectedException)2 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 Session (org.hibernate.Session)1 Configuration (org.hibernate.cfg.Configuration)1