Search in sources :

Example 1 with ODatabaseListener

use of com.orientechnologies.orient.core.db.ODatabaseListener in project orientdb by orientechnologies.

the class OAbstractPaginatedStorage method executeCommand.

public Object executeCommand(final OCommandRequestText iCommand, final OCommandExecutor executor) {
    if (iCommand.isIdempotent() && !executor.isIdempotent())
        throw new OCommandExecutionException("Cannot execute non idempotent command");
    long beginTime = Orient.instance().getProfiler().startChrono();
    try {
        ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.get();
        // CALL BEFORE COMMAND
        Iterable<ODatabaseListener> listeners = db.getListeners();
        for (ODatabaseListener oDatabaseListener : listeners) {
            oDatabaseListener.onBeforeCommand(iCommand, executor);
        }
        boolean foundInCache = false;
        Object result = null;
        if (iCommand.isCacheableResult() && executor.isCacheable() && iCommand.getParameters() == null) {
            // TRY WITH COMMAND CACHE
            result = db.getMetadata().getCommandCache().get(db.getUser(), iCommand.getText(), iCommand.getLimit());
            if (result != null) {
                foundInCache = true;
                if (iCommand.getResultListener() != null) {
                    // INVOKE THE LISTENER IF ANY
                    if (result instanceof Collection) {
                        for (Object o : (Collection) result) iCommand.getResultListener().result(o);
                    } else
                        iCommand.getResultListener().result(result);
                    // RESET THE RESULT TO AVOID TO SEND IT TWICE
                    result = null;
                }
            }
        }
        if (!foundInCache) {
            // EXECUTE THE COMMAND
            result = executor.execute(iCommand.getParameters());
            if (result != null && iCommand.isCacheableResult() && executor.isCacheable() && (iCommand.getParameters() == null || iCommand.getParameters().isEmpty()))
                // CACHE THE COMMAND RESULT
                db.getMetadata().getCommandCache().put(db.getUser(), iCommand.getText(), result, iCommand.getLimit(), executor.getInvolvedClusters(), System.currentTimeMillis() - beginTime);
        }
        // CALL AFTER COMMAND
        for (ODatabaseListener oDatabaseListener : listeners) {
            oDatabaseListener.onAfterCommand(iCommand, executor, result);
        }
        return result;
    } catch (OException e) {
        // PASS THROUGH
        throw e;
    } catch (Exception e) {
        throw OException.wrapException(new OCommandExecutionException("Error on execution of command: " + iCommand), e);
    } finally {
        if (Orient.instance().getProfiler().isRecording()) {
            final ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
            if (db != null) {
                final OSecurityUser user = db.getUser();
                final String userString = user != null ? user.toString() : null;
                Orient.instance().getProfiler().stopChrono("db." + ODatabaseRecordThreadLocal.INSTANCE.get().getName() + ".command." + iCommand.toString(), "Command executed against the database", beginTime, "db.*.command.*", null, userString);
            }
        }
    }
}
Also used : OException(com.orientechnologies.common.exception.OException) OSecurityUser(com.orientechnologies.orient.core.metadata.security.OSecurityUser) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ODatabaseListener(com.orientechnologies.orient.core.db.ODatabaseListener) OException(com.orientechnologies.common.exception.OException) OModificationOperationProhibitedException(com.orientechnologies.common.concur.lock.OModificationOperationProhibitedException)

Example 2 with ODatabaseListener

use of com.orientechnologies.orient.core.db.ODatabaseListener in project orientdb by orientechnologies.

the class TransactionAtomicTest method testTransactionPreListenerRollback.

@Test
public void testTransactionPreListenerRollback() throws IOException {
    ODocument record1 = new ODocument();
    record1.field("value", "This is the first version").save();
    final ODatabaseListener listener = new ODatabaseListener() {

        @Override
        public void onAfterTxCommit(ODatabase iDatabase) {
        }

        @Override
        public void onAfterTxRollback(ODatabase iDatabase) {
        }

        @Override
        public void onBeforeTxBegin(ODatabase iDatabase) {
        }

        @Override
        public void onBeforeTxCommit(ODatabase iDatabase) {
            throw new RuntimeException("Rollback test");
        }

        @Override
        public void onBeforeTxRollback(ODatabase iDatabase) {
        }

        @Override
        public void onClose(ODatabase iDatabase) {
        }

        @Override
        public void onBeforeCommand(OCommandRequestText iCommand, OCommandExecutor executor) {
        }

        @Override
        public void onAfterCommand(OCommandRequestText iCommand, OCommandExecutor executor, Object result) {
        }

        @Override
        public void onCreate(ODatabase iDatabase) {
        }

        @Override
        public void onDelete(ODatabase iDatabase) {
        }

        @Override
        public void onOpen(ODatabase iDatabase) {
        }

        @Override
        public boolean onCorruptionRepairDatabase(ODatabase iDatabase, final String iReason, String iWhatWillbeFixed) {
            return true;
        }
    };
    database.registerListener(listener);
    database.begin();
    try {
        database.commit();
        Assert.assertTrue(false);
    } catch (OTransactionException e) {
        Assert.assertTrue(true);
    } finally {
        database.unregisterListener(listener);
    }
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OCommandExecutor(com.orientechnologies.orient.core.command.OCommandExecutor) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ODatabase(com.orientechnologies.orient.core.db.ODatabase) ODatabaseListener(com.orientechnologies.orient.core.db.ODatabaseListener) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Aggregations

ODatabaseListener (com.orientechnologies.orient.core.db.ODatabaseListener)2 OModificationOperationProhibitedException (com.orientechnologies.common.concur.lock.OModificationOperationProhibitedException)1 OException (com.orientechnologies.common.exception.OException)1 OCommandExecutor (com.orientechnologies.orient.core.command.OCommandExecutor)1 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)1 ODatabase (com.orientechnologies.orient.core.db.ODatabase)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)1 OSecurityUser (com.orientechnologies.orient.core.metadata.security.OSecurityUser)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 Test (org.testng.annotations.Test)1