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);
}
}
}
}
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);
}
}
Aggregations