Search in sources :

Example 1 with InteractionSpec

use of jakarta.resource.cci.InteractionSpec in project eclipselink by eclipse-ee4j.

the class OracleNoSQLPlatform method buildInteractionSpec.

/**
 * Allow the platform to build the interaction spec based on properties defined in the interaction.
 */
@Override
public InteractionSpec buildInteractionSpec(EISInteraction interaction) {
    InteractionSpec spec = interaction.getInteractionSpec();
    if (spec == null) {
        OracleNoSQLInteractionSpec noSqlSpec = new OracleNoSQLInteractionSpec();
        Object operation = interaction.getProperty(OPERATION);
        if (operation == null) {
            throw new EISException("'" + OPERATION + "' property must be set on the query's interation.");
        }
        if (operation instanceof String) {
            operation = OracleNoSQLOperation.valueOf((String) operation);
        }
        noSqlSpec.setOperation((OracleNoSQLOperation) operation);
        // Allows setting of consistency as a property.
        Object consistency = interaction.getProperty(CONSISTENCY);
        if (consistency == null) {
            // Default to session property.
            consistency = interaction.getQuery().getSession().getProperty(CONSISTENCY);
        }
        if (consistency instanceof Consistency) {
            noSqlSpec.setConsistency((Consistency) consistency);
        } else if (consistency instanceof String) {
            String constant = (String) consistency;
            if (constant.equals("ABSOLUTE")) {
                noSqlSpec.setConsistency(Consistency.ABSOLUTE);
            } else if (constant.equals("NONE_REQUIRED")) {
                noSqlSpec.setConsistency(Consistency.NONE_REQUIRED);
            } else {
                throw new EISException("Invalid consistency property value: " + constant);
            }
        }
        // Allows setting of durability as a property.
        Object durability = interaction.getProperty(DURABILITY);
        if (durability == null) {
            // Default to session property.
            durability = interaction.getQuery().getSession().getProperty(DURABILITY);
        }
        if (durability instanceof Durability) {
            noSqlSpec.setDurability((Durability) durability);
        } else if (durability instanceof String) {
            String constant = (String) durability;
            if (constant.equals("COMMIT_NO_SYNC")) {
                noSqlSpec.setDurability(Durability.COMMIT_NO_SYNC);
            } else if (constant.equals("COMMIT_SYNC")) {
                noSqlSpec.setDurability(Durability.COMMIT_SYNC);
            } else if (constant.equals("COMMIT_WRITE_NO_SYNC")) {
                noSqlSpec.setDurability(Durability.COMMIT_WRITE_NO_SYNC);
            } else {
                throw new EISException("Invalid durability property value: " + constant);
            }
        }
        // Allows setting of timeout as a property.
        Object timeout = interaction.getProperty(TIMEOUT);
        if (timeout == null) {
            // Default to session property.
            timeout = interaction.getQuery().getSession().getProperty(TIMEOUT);
        }
        if (timeout instanceof Number) {
            noSqlSpec.setTimeout(((Number) timeout).longValue());
        } else if (timeout instanceof String) {
            noSqlSpec.setTimeout(Long.parseLong(((String) timeout)));
        } else if (interaction.getQuery().getQueryTimeout() > 0) {
            noSqlSpec.setTimeout(interaction.getQuery().getQueryTimeout());
        }
        // Allows setting of version as a property.
        Object version = interaction.getProperty(VERSION);
        if (version == null) {
            // Default to session property.
            version = interaction.getQuery().getSession().getProperty(VERSION);
        }
        if (version == null) {
            if (interaction.getQuery().getDescriptor() != null) {
                ClassDescriptor descriptor = interaction.getQuery().getDescriptor();
                if (descriptor.usesOptimisticLocking() && descriptor.getOptimisticLockingPolicy() instanceof SelectedFieldsLockingPolicy) {
                    DatabaseField field = ((SelectedFieldsLockingPolicy) descriptor.getOptimisticLockingPolicy()).getLockFields().get(0);
                    if (interaction.getInputRow() != null) {
                        version = interaction.getInputRow().get(field);
                    }
                }
            }
        }
        if (version instanceof Version) {
            noSqlSpec.setVersion((Version) version);
        } else if (version instanceof byte[]) {
            noSqlSpec.setVersion(Version.fromByteArray((byte[]) version));
        }
        spec = noSqlSpec;
    }
    return spec;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) OracleNoSQLInteractionSpec(org.eclipse.persistence.internal.nosql.adapters.nosql.OracleNoSQLInteractionSpec) InteractionSpec(jakarta.resource.cci.InteractionSpec) Consistency(oracle.kv.Consistency) Durability(oracle.kv.Durability) EISException(org.eclipse.persistence.eis.EISException) OracleNoSQLInteractionSpec(org.eclipse.persistence.internal.nosql.adapters.nosql.OracleNoSQLInteractionSpec) SelectedFieldsLockingPolicy(org.eclipse.persistence.descriptors.SelectedFieldsLockingPolicy) Version(oracle.kv.Version) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField)

Example 2 with InteractionSpec

use of jakarta.resource.cci.InteractionSpec in project eclipselink by eclipse-ee4j.

the class EISAccessor method basicExecuteCall.

/**
 * Execute the interaction.
 * The execution can differ slightly depending on the type of interaction.
 * The call may be parameterized where the arguments are in the translation row.
 * The row will be empty if there are no parameters.
 * @return depending of the type either the row count, row or vector of rows.
 */
@Override
public Object basicExecuteCall(Call call, AbstractRecord translationRow, AbstractSession session) throws DatabaseException {
    // If the login is null, then this accessor has never been connected.
    if (getLogin() == null) {
        throw DatabaseException.databaseAccessorNotConnected();
    }
    Interaction interaction = null;
    Object result = null;
    EISInteraction eisCall = null;
    try {
        eisCall = (EISInteraction) call;
    } catch (ClassCastException e) {
        throw QueryException.invalidDatabaseCall(call);
    }
    // Record and check if auto-commit is required.
    // Some platforms may require this (AQ).
    boolean autoCommit = (!isInTransaction()) && getEISPlatform().requiresAutoCommit();
    if (autoCommit) {
        beginTransaction(session);
    }
    try {
        if (session.shouldLog(SessionLog.FINE, SessionLog.SQL)) {
            // pre-check to improve performance
            session.log(SessionLog.FINE, SessionLog.SQL, call.getLogString(this), null, this, false);
        }
        incrementCallCount(session);
        session.startOperationProfile(SessionProfiler.SqlPrepare, eisCall.getQuery(), SessionProfiler.ALL);
        Record input = null;
        Record output = null;
        try {
            interaction = getCCIConnection().createInteraction();
            input = getEISPlatform().createInputRecord(eisCall, this);
            output = getEISPlatform().createOutputRecord(eisCall, translationRow, this);
        } finally {
            session.endOperationProfile(SessionProfiler.SqlPrepare, eisCall.getQuery(), SessionProfiler.ALL);
        }
        session.startOperationProfile(SessionProfiler.StatementExecute, eisCall.getQuery(), SessionProfiler.ALL);
        try {
            boolean success = true;
            InteractionSpec interactionSpec = getEISPlatform().buildInteractionSpec(eisCall);
            if (output == null) {
                output = interaction.execute(interactionSpec, input);
            } else {
                success = interaction.execute(interactionSpec, input, output);
            }
            session.log(SessionLog.FINEST, SessionLog.QUERY, "adapter_result", output);
            if (eisCall.isNothingReturned()) {
                if (success) {
                    result = 1;
                } else {
                    result = 0;
                }
                // Fire the output parameter row to allow app to handle return value.
                if (output != null) {
                    AbstractRecord outputRow = getEISPlatform().buildRow(output, eisCall, this);
                    if (outputRow != null) {
                        eisCall.getQuery().setProperty("output", outputRow);
                        if (session.hasEventManager()) {
                            session.getEventManager().outputParametersDetected(outputRow, eisCall);
                        }
                    }
                }
            } else if (eisCall.isOneRowReturned()) {
                result = getEISPlatform().buildRow(output, eisCall, this);
            } else {
                result = getEISPlatform().buildRows(output, eisCall, this);
            }
            session.log(SessionLog.FINEST, SessionLog.QUERY, "data_access_result", output);
        } finally {
            session.endOperationProfile(SessionProfiler.StatementExecute, eisCall.getQuery(), SessionProfiler.ALL);
        }
    } catch (ResourceException exception) {
        // Ensure each resource is released, but still ensure that the real exception is thrown.
        if (interaction != null) {
            try {
                interaction.close();
            } catch (Exception closeException) {
            // Ignore error to avoid masking real exception.
            }
        }
        try {
            decrementCallCount();
        } catch (Exception closeException) {
        // Ignore error to avoid masking real exception.
        }
        try {
            if (autoCommit) {
                commitTransaction(session);
            }
        } catch (Exception closeException) {
        // Ignore error to avoid masking real exception.
        }
        throw EISException.resourceException(exception, call, this, session);
    } catch (RuntimeException exception) {
        try {
            // Ensure that the statement is closed, but still ensure that the real exception is thrown.
            try {
                if (interaction != null) {
                    interaction.close();
                }
            } finally {
                if (autoCommit) {
                    commitTransaction(session);
                }
            }
        } catch (Exception closeException) {
        }
        throw exception;
    }
    boolean transactionCommitted = false;
    boolean countDecremented = false;
    // This is in separate try block to ensure that the real exception is not masked by the close exception.
    try {
        interaction.close();
        if (autoCommit) {
            commitTransaction(session);
        }
        transactionCommitted = true;
        decrementCallCount();
        countDecremented = true;
    } catch (ResourceException exception) {
        try {
            if (!transactionCommitted) {
                if (autoCommit) {
                    commitTransaction(session);
                }
            }
        } catch (Exception ignore) {
        // Ignore error to avoid masking real exception.
        }
        try {
            if (!countDecremented) {
                decrementCallCount();
            }
        } catch (Exception ignore) {
        // Ignore error to avoid masking real exception.
        }
        throw EISException.resourceException(exception, this, session);
    }
    return result;
}
Also used : EISInteraction(org.eclipse.persistence.eis.interactions.EISInteraction) EISInteraction(org.eclipse.persistence.eis.interactions.EISInteraction) Interaction(jakarta.resource.cci.Interaction) InteractionSpec(jakarta.resource.cci.InteractionSpec) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) Record(jakarta.resource.cci.Record) ResourceException(jakarta.resource.ResourceException) QueryException(org.eclipse.persistence.exceptions.QueryException) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) ResourceException(jakarta.resource.ResourceException)

Example 3 with InteractionSpec

use of jakarta.resource.cci.InteractionSpec in project eclipselink by eclipse-ee4j.

the class MongoPlatform method buildInteractionSpec.

/**
 * Allow the platform to build the interaction spec based on properties defined in the interaction.
 */
@Override
public InteractionSpec buildInteractionSpec(EISInteraction interaction) {
    InteractionSpec spec = interaction.getInteractionSpec();
    if (spec == null) {
        MongoInteractionSpec mongoSpec = new MongoInteractionSpec();
        Object operation = interaction.getProperty(OPERATION);
        if (interaction.isQueryStringCall()) {
            mongoSpec.setCode(((QueryStringCall) interaction).getQueryString());
            operation = MongoOperation.EVAL;
        }
        if (operation == null) {
            throw new EISException("'" + OPERATION + "' property must be set on the query's interation.");
        }
        if (operation instanceof String) {
            operation = MongoOperation.valueOf((String) operation);
        }
        mongoSpec.setOperation((MongoOperation) operation);
        Object collection = interaction.getProperty(COLLECTION);
        if (collection != null) {
            mongoSpec.setCollection((String) collection);
        }
        // Allows setting of read preference as a property.
        Object preference = interaction.getProperty(READ_PREFERENCE);
        if (preference instanceof ReadPreference) {
            mongoSpec.setReadPreference((ReadPreference) preference);
        } else if (preference instanceof String) {
            String constant = (String) preference;
            if (constant.equals("PRIMARY")) {
                mongoSpec.setReadPreference(ReadPreference.primary());
            } else if (constant.equals("SECONDARY")) {
                mongoSpec.setReadPreference(ReadPreference.secondary());
            } else {
                throw new EISException("Invalid read preference property value: " + constant);
            }
        }
        // Allows setting of write concern as a property.
        Object concern = interaction.getProperty(WRITE_CONCERN);
        if (concern instanceof WriteConcern) {
            mongoSpec.setWriteConcern((WriteConcern) concern);
        } else if (concern instanceof String) {
            String constant = (String) concern;
            if (constant.equals("FSYNC_SAFE")) {
                mongoSpec.setWriteConcern(WriteConcern.FSYNC_SAFE);
            } else if (constant.equals("JOURNAL_SAFE")) {
                mongoSpec.setWriteConcern(WriteConcern.JOURNAL_SAFE);
            } else if (constant.equals("MAJORITY")) {
                mongoSpec.setWriteConcern(WriteConcern.MAJORITY);
            } else if (constant.equals("NONE")) {
                mongoSpec.setWriteConcern(/* WriteConcern.NONE */
                new WriteConcern("none"));
            } else if (constant.equals("NORMAL")) {
                mongoSpec.setWriteConcern(WriteConcern.NORMAL);
            } else if (constant.equals("REPLICAS_SAFE")) {
                mongoSpec.setWriteConcern(WriteConcern.REPLICAS_SAFE);
            } else if (constant.equals("SAFE")) {
                mongoSpec.setWriteConcern(WriteConcern.SAFE);
            } else {
                throw new EISException("Invalid read preference property value: " + constant);
            }
        }
        // Allows setting of options as a property.
        Object options = interaction.getProperty(OPTIONS);
        if (options instanceof Number) {
            mongoSpec.setOptions(((Number) options).intValue());
        } else if (options instanceof String) {
            mongoSpec.setOptions(Integer.parseInt(((String) options)));
        }
        // Allows setting of skip as a property.
        Object skip = interaction.getProperty(SKIP);
        if (skip instanceof Number) {
            mongoSpec.setSkip(((Number) skip).intValue());
        } else if (skip instanceof String) {
            mongoSpec.setSkip(Integer.parseInt(((String) skip)));
        }
        // Allows setting of limit as a property.
        Object limit = interaction.getProperty(LIMIT);
        if (limit instanceof Number) {
            mongoSpec.setLimit(((Number) limit).intValue());
        } else if (skip instanceof String) {
            mongoSpec.setLimit(Integer.parseInt(((String) limit)));
        }
        // Allows setting of batchSize as a property.
        Object batchSize = interaction.getProperty(BATCH_SIZE);
        if (batchSize instanceof Number) {
            mongoSpec.setBatchSize(((Number) batchSize).intValue());
        } else if (skip instanceof String) {
            mongoSpec.setBatchSize(Integer.parseInt(((String) batchSize)));
        }
        spec = mongoSpec;
    }
    return spec;
}
Also used : ReadPreference(com.mongodb.ReadPreference) InteractionSpec(jakarta.resource.cci.InteractionSpec) MongoInteractionSpec(org.eclipse.persistence.internal.nosql.adapters.mongo.MongoInteractionSpec) WriteConcern(com.mongodb.WriteConcern) MongoInteractionSpec(org.eclipse.persistence.internal.nosql.adapters.mongo.MongoInteractionSpec) EISException(org.eclipse.persistence.eis.EISException)

Aggregations

InteractionSpec (jakarta.resource.cci.InteractionSpec)3 EISException (org.eclipse.persistence.eis.EISException)2 ReadPreference (com.mongodb.ReadPreference)1 WriteConcern (com.mongodb.WriteConcern)1 ResourceException (jakarta.resource.ResourceException)1 Interaction (jakarta.resource.cci.Interaction)1 Record (jakarta.resource.cci.Record)1 Consistency (oracle.kv.Consistency)1 Durability (oracle.kv.Durability)1 Version (oracle.kv.Version)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 SelectedFieldsLockingPolicy (org.eclipse.persistence.descriptors.SelectedFieldsLockingPolicy)1 EISInteraction (org.eclipse.persistence.eis.interactions.EISInteraction)1 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)1 QueryException (org.eclipse.persistence.exceptions.QueryException)1 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)1 MongoInteractionSpec (org.eclipse.persistence.internal.nosql.adapters.mongo.MongoInteractionSpec)1 OracleNoSQLInteractionSpec (org.eclipse.persistence.internal.nosql.adapters.nosql.OracleNoSQLInteractionSpec)1 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)1