Search in sources :

Example 1 with XWikiHibernateBaseStore

use of com.xpn.xwiki.store.XWikiHibernateBaseStore in project xwiki-platform by xwiki.

the class DefaultInstanceIdManager method initialize.

@Override
public void initialize() {
    // Load it from the database
    XWikiContext context = getXWikiContext();
    XWikiHibernateBaseStore store = (XWikiHibernateBaseStore) this.hibernateStoreProvider.get();
    // Try retrieving the UUID from the database
    // First ensure that we're on the main wiki since we store the unique id only on the main wiki
    String originalDatabase = context.getWikiId();
    context.setWikiId(context.getMainXWiki());
    try {
        InstanceId id = store.failSafeExecuteRead(context, new XWikiHibernateBaseStore.HibernateCallback<InstanceId>() {

            @Override
            public InstanceId doInHibernate(Session session) throws HibernateException {
                // Retrieve the version from the database
                return (InstanceId) session.createCriteria(InstanceId.class).uniqueResult();
            }
        });
        // If the database doesn't hold the UUID then compute one and save it
        if (id == null) {
            // Compute UUID
            final InstanceId newId = new InstanceId(UUID.randomUUID().toString());
            // will be retried again next time the wiki is restarted.
            try {
                store.executeWrite(context, new XWikiHibernateBaseStore.HibernateCallback<Object>() {

                    @Override
                    public Object doInHibernate(Session session) throws HibernateException {
                        session.createQuery("delete from " + InstanceId.class.getName()).executeUpdate();
                        session.save(newId);
                        return null;
                    }
                });
            } catch (XWikiException e) {
                this.logger.warn("Failed to save Instance id to database. Reason: [{}]", ExceptionUtils.getRootCauseMessage(e));
            }
            id = newId;
        }
        this.instanceId = id;
    } finally {
        // Restore original database
        context.setWikiId(originalDatabase);
    }
}
Also used : InstanceId(org.xwiki.instance.InstanceId) HibernateException(org.hibernate.HibernateException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiHibernateBaseStore(com.xpn.xwiki.store.XWikiHibernateBaseStore) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 2 with XWikiHibernateBaseStore

use of com.xpn.xwiki.store.XWikiHibernateBaseStore in project xwiki-platform by xwiki.

the class R40000XWIKI6990DataMigration method getLiquibaseChangeLog.

@Override
public String getLiquibaseChangeLog() throws DataMigrationException {
    final XWikiHibernateBaseStore store = getStore();
    this.configuration = store.getConfiguration();
    final StringBuilder sb = new StringBuilder(12000);
    final List<PersistentClass> classes = new ArrayList<PersistentClass>();
    detectDatabaseProducts(store);
    if (this.logger.isDebugEnabled()) {
        if (this.isOracle) {
            this.logger.debug("Oracle database detected, proceeding to all updates manually with deferred constraints.");
        }
        if (this.isMySQL && !this.isMySQLMyISAM) {
            this.logger.debug("MySQL innoDB database detected, proceeding to simplified updates with cascaded updates.");
        }
        if (this.isMySQLMyISAM) {
            this.logger.debug("MySQL MyISAM database detected, proceeding to all updates manually without constraints.");
        }
        if (this.isMSSQL) {
            this.logger.debug("Microsoft SQL Server database detected, proceeding to simplified updates with cascaded u" + "pdates. During data type changes, Primary Key constraints and indexes are temporarily dropped.");
        }
    }
    // Build the list of classes to check for updates
    classes.add(getClassMapping(BaseObject.class.getName()));
    for (Class<?> klass : PROPERTY_CLASS) {
        classes.add(getClassMapping(klass.getName()));
    }
    for (Class<?> klass : STATS_CLASSES) {
        classes.add(getClassMapping(klass.getName()));
    }
    // Initialize the counter of Change Logs
    this.logCount = 0;
    // do not prevent type changes, we skip all this processing for MySQL table stored using the MyISAM engine.
    if (!this.isMySQLMyISAM) {
        for (PersistentClass klass : classes) {
            this.fkTables.addAll(getForeignKeyTables(klass));
        }
    }
    // Drop all FK constraints
    for (Table table : this.fkTables) {
        appendDropForeignKeyChangeLog(sb, table);
    }
    // Process internal classes
    for (PersistentClass klass : classes) {
        // The same table mapped for StringListProperty and LargeStringProperty
        if (klass.getMappedClass() != StringListProperty.class) {
            // Update key types
            appendDataTypeChangeLogs(sb, klass);
        }
    }
    // Process dynamic and custom mapping
    final XWikiContext context = getXWikiContext();
    try {
        processCustomMappings((XWikiHibernateStore) store, new CustomMappingCallback() {

            @Override
            public void processCustomMapping(XWikiHibernateStore store, String name, String mapping, boolean hasDynamicMapping) throws XWikiException {
                if (INTERNAL.equals(mapping) || hasDynamicMapping) {
                    PersistentClass klass = R40000XWIKI6990DataMigration.this.configuration.getClassMapping(name);
                    if (!R40000XWIKI6990DataMigration.this.isMySQLMyISAM) {
                        List<Table> tables = getForeignKeyTables(klass);
                        for (Table table : tables) {
                            if (!R40000XWIKI6990DataMigration.this.fkTables.contains(table)) {
                                // Drop FK constraints for custom mapped class
                                appendDropForeignKeyChangeLog(sb, table);
                                R40000XWIKI6990DataMigration.this.fkTables.add(table);
                            }
                        }
                    }
                    // Update key types for custom mapped class
                    appendDataTypeChangeLogs(sb, klass);
                }
            }
        }, context);
    } catch (XWikiException e) {
        throw new DataMigrationException("Unable to process custom mapped classes during schema updated", e);
    }
    // Add FK constraints back, activating cascaded updates
    for (Table table : this.fkTables) {
        appendAddForeignKeyChangeLog(sb, table);
    }
    // Oracle doesn't support cascaded updates, so we still need to manually update each table
    if (this.isOracle) {
        this.fkTables.clear();
    }
    logProgress("%d schema updates required.", this.logCount);
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("About to execute this Liquibase XML: {}", sb.toString());
    }
    return sb.toString();
}
Also used : Table(org.hibernate.mapping.Table) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiHibernateStore(com.xpn.xwiki.store.XWikiHibernateStore) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) XWikiHibernateBaseStore(com.xpn.xwiki.store.XWikiHibernateBaseStore) DataMigrationException(com.xpn.xwiki.store.migration.DataMigrationException) XWikiException(com.xpn.xwiki.XWikiException) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 3 with XWikiHibernateBaseStore

use of com.xpn.xwiki.store.XWikiHibernateBaseStore in project xwiki-platform by xwiki.

the class HibernateDataMigrationManager method initializeEmptyDB.

@Override
protected void initializeEmptyDB() throws DataMigrationException {
    final XWikiContext context = getXWikiContext();
    final XWikiHibernateBaseStore store = getStore();
    final Session originalSession = store.getSession(context);
    final Transaction originalTransaction = store.getTransaction(context);
    store.setSession(null, context);
    store.setTransaction(null, context);
    try {
        updateSchema(null);
        setDBVersion(getLatestVersion());
    } finally {
        store.setSession(originalSession, context);
        store.setTransaction(originalTransaction, context);
    }
}
Also used : Transaction(org.hibernate.Transaction) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiHibernateBaseStore(com.xpn.xwiki.store.XWikiHibernateBaseStore) Session(org.hibernate.Session)

Example 4 with XWikiHibernateBaseStore

use of com.xpn.xwiki.store.XWikiHibernateBaseStore in project xwiki-platform by xwiki.

the class DatabasePingDataProvider method getDatabaseMetaData.

private DatabaseMetaData getDatabaseMetaData() {
    DatabaseMetaData metaData = null;
    XWikiContext xcontext = (XWikiContext) this.execution.getContext().getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
    if (xcontext != null) {
        XWikiStoreInterface storeInterface = xcontext.getWiki().getStore();
        if (storeInterface instanceof XWikiCacheStoreInterface) {
            storeInterface = ((XWikiCacheStoreInterface) storeInterface).getStore();
        }
        if (XWikiHibernateBaseStore.class.isAssignableFrom(storeInterface.getClass())) {
            XWikiHibernateBaseStore baseStore = (XWikiHibernateBaseStore) storeInterface;
            metaData = baseStore.getDatabaseMetaData();
        }
    }
    return metaData;
}
Also used : XWikiCacheStoreInterface(com.xpn.xwiki.store.XWikiCacheStoreInterface) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiHibernateBaseStore(com.xpn.xwiki.store.XWikiHibernateBaseStore) DatabaseMetaData(java.sql.DatabaseMetaData) XWikiStoreInterface(com.xpn.xwiki.store.XWikiStoreInterface)

Example 5 with XWikiHibernateBaseStore

use of com.xpn.xwiki.store.XWikiHibernateBaseStore in project xwiki-platform by xwiki.

the class DatabaseMailStatusStore method save.

@Override
public void save(final MailStatus status, final Map<String, Object> parameters) throws MailStoreException {
    XWikiHibernateBaseStore store = (XWikiHibernateBaseStore) this.hibernateStore;
    XWikiContext xwikiContext = this.contextProvider.get();
    // Save in the main wiki
    String currentWiki = xwikiContext.getWikiId();
    xwikiContext.setWikiId(xwikiContext.getMainXWiki());
    try {
        // Delete any previous state of the message
        delete(status.getMessageId(), parameters);
        store.executeWrite(xwikiContext, new XWikiHibernateBaseStore.HibernateCallback<Object>() {

            @Override
            public Object doInHibernate(Session session) throws HibernateException, XWikiException {
                session.save(status);
                return null;
            }
        });
        // Log the save for debugging purpose
        this.logger.debug("Saved mail status [{}]", status);
    } catch (Exception e) {
        throw new MailStoreException(String.format("Failed to save mail status [%s] to the database.", status), e);
    } finally {
        xwikiContext.setWikiId(currentWiki);
    }
}
Also used : MailStoreException(org.xwiki.mail.MailStoreException) HibernateException(org.hibernate.HibernateException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiHibernateBaseStore(com.xpn.xwiki.store.XWikiHibernateBaseStore) XWikiException(com.xpn.xwiki.XWikiException) XWikiException(com.xpn.xwiki.XWikiException) MailStoreException(org.xwiki.mail.MailStoreException) HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session)

Aggregations

XWikiHibernateBaseStore (com.xpn.xwiki.store.XWikiHibernateBaseStore)12 XWikiContext (com.xpn.xwiki.XWikiContext)10 Session (org.hibernate.Session)9 XWikiException (com.xpn.xwiki.XWikiException)7 HibernateException (org.hibernate.HibernateException)5 MailStoreException (org.xwiki.mail.MailStoreException)4 List (java.util.List)2 Query (org.hibernate.Query)2 Transaction (org.hibernate.Transaction)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 XWikiCacheStoreInterface (com.xpn.xwiki.store.XWikiCacheStoreInterface)1 HibernateCallback (com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback)1 XWikiHibernateStore (com.xpn.xwiki.store.XWikiHibernateStore)1 XWikiStoreInterface (com.xpn.xwiki.store.XWikiStoreInterface)1 DataMigrationException (com.xpn.xwiki.store.migration.DataMigrationException)1 XWikiDBVersion (com.xpn.xwiki.store.migration.XWikiDBVersion)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Liquibase (liquibase.Liquibase)1