Search in sources :

Example 1 with TablePerMultitenantPolicy

use of org.eclipse.persistence.descriptors.TablePerMultitenantPolicy in project eclipselink by eclipse-ee4j.

the class MultitenantMetadata method process.

/**
 * INTERNAL:
 */
public void process(MetadataDescriptor descriptor) {
    ClassDescriptor classDescriptor = descriptor.getClassDescriptor();
    MultitenantPolicy policy;
    if (m_type == null || m_type.equals(MultitenantType.SINGLE_TABLE.name()) || m_type.equals(MultitenantType.VPD.name())) {
        if (m_type == null || m_type.equals(MultitenantType.SINGLE_TABLE.name())) {
            policy = new SingleTableMultitenantPolicy(classDescriptor);
            // As soon as we find one entity that is multitenant, turn off
            // native SQL queries Users can set the property on their
            // persistence unit if they want it back on. Or per query.
            getProject().setAllowNativeSQLQueries(false);
        } else {
            policy = new VPDMultitenantPolicy(classDescriptor);
            // Within VPD, we must ensure we are using an Always exclusive mode.
            ((ServerSession) getProject().getSession()).getDefaultConnectionPolicy().setExclusiveMode(ConnectionPolicy.ExclusiveMode.Always);
        }
        // Set the include criteria flag on the query manager (in VPD this will be false).
        ((SingleTableMultitenantPolicy) policy).setIncludeTenantCriteria(includeCriteria());
        // Single table multi-tenancy (perhaps using VPD).
        processTenantDiscriminators(descriptor, (SingleTableMultitenantPolicy) policy);
    } else {
        // Initialize the policy.
        policy = new TablePerMultitenantPolicy(classDescriptor);
        // Process the tenant table discriminator.
        processTenantTableDiscriminator(descriptor, (TablePerMultitenantPolicy) policy);
    }
    // Set the policy on the descriptor.
    classDescriptor.setMultitenantPolicy(policy);
    // we are sharing an EMF.
    if (getProject().usesMultitenantSharedEmf()) {
        if (getProject().usesMultitenantSharedCache()) {
            // Caching details are processed before multitenant metadata.
            if (classDescriptor.isSharedIsolation()) {
                classDescriptor.setCacheIsolation(CacheIsolationType.PROTECTED);
            }
        } else {
            classDescriptor.setCacheIsolation(CacheIsolationType.ISOLATED);
        }
    }
}
Also used : VPDMultitenantPolicy(org.eclipse.persistence.descriptors.VPDMultitenantPolicy) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) MultitenantPolicy(org.eclipse.persistence.descriptors.MultitenantPolicy) TablePerMultitenantPolicy(org.eclipse.persistence.descriptors.TablePerMultitenantPolicy) VPDMultitenantPolicy(org.eclipse.persistence.descriptors.VPDMultitenantPolicy) SingleTableMultitenantPolicy(org.eclipse.persistence.descriptors.SingleTableMultitenantPolicy) SingleTableMultitenantPolicy(org.eclipse.persistence.descriptors.SingleTableMultitenantPolicy) TablePerMultitenantPolicy(org.eclipse.persistence.descriptors.TablePerMultitenantPolicy)

Example 2 with TablePerMultitenantPolicy

use of org.eclipse.persistence.descriptors.TablePerMultitenantPolicy in project eclipselink by eclipse-ee4j.

the class AbstractSession method updateTablePerTenantDescriptors.

/**
 * INTERNAL:
 * Set the table per tenant. This should be called per client session after
 * the start of a transaction. From JPA this method is called on the entity
 * manager by setting the multitenant table per tenant property.
 */
public void updateTablePerTenantDescriptors(String property, Object value) {
    // When all the table per tenant descriptors are set, we should initialize them.
    boolean shouldInitializeDescriptors = hasTablePerTenantDescriptors();
    for (ClassDescriptor descriptor : getTablePerTenantDescriptors()) {
        TablePerMultitenantPolicy policy = (TablePerMultitenantPolicy) descriptor.getMultitenantPolicy();
        if ((!policy.hasContextTenant()) && policy.usesContextProperty(property)) {
            policy.setContextTenant((String) value);
        }
        shouldInitializeDescriptors = shouldInitializeDescriptors && policy.hasContextTenant();
    }
    if (shouldInitializeDescriptors) {
        // descriptors, we can go through the initialization phases safely.
        try {
            // First initialize basic properties (things that do not depend on anything else)
            for (ClassDescriptor descriptor : tablePerTenantDescriptors) {
                descriptor.preInitialize(this);
            }
            // Second initialize basic mappings
            for (ClassDescriptor descriptor : tablePerTenantDescriptors) {
                descriptor.initialize(this);
            }
            // Third initialize child dependencies
            for (ClassDescriptor descriptor : tablePerTenantDescriptors) {
                descriptor.postInitialize(this);
            }
            if (getIntegrityChecker().hasErrors()) {
                handleSevere(new IntegrityException(getIntegrityChecker()));
            }
        } finally {
            clearIntegrityChecker();
        }
        getCommitManager().initializeCommitOrder();
        // once all the descriptors have been initialized.
        if (hasTablePerTenantQueries()) {
            for (DatabaseQuery query : getTablePerTenantQueries()) {
                processJPAQuery(query);
            }
        }
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) TablePerMultitenantPolicy(org.eclipse.persistence.descriptors.TablePerMultitenantPolicy) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException)

Aggregations

ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 TablePerMultitenantPolicy (org.eclipse.persistence.descriptors.TablePerMultitenantPolicy)2 MultitenantPolicy (org.eclipse.persistence.descriptors.MultitenantPolicy)1 SingleTableMultitenantPolicy (org.eclipse.persistence.descriptors.SingleTableMultitenantPolicy)1 VPDMultitenantPolicy (org.eclipse.persistence.descriptors.VPDMultitenantPolicy)1 IntegrityException (org.eclipse.persistence.exceptions.IntegrityException)1 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)1