use of com.orientechnologies.orient.core.command.OCommandExecutor 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);
}
}
use of com.orientechnologies.orient.core.command.OCommandExecutor in project orientdb by orientechnologies.
the class OAbstractPaginatedStorage method command.
/**
* Executes the command request and return the result back.
*/
public Object command(final OCommandRequestText iCommand) {
while (true) {
try {
final OCommandExecutor executor = OCommandManager.instance().getExecutor(iCommand);
// COPY THE CONTEXT FROM THE REQUEST
executor.setContext(iCommand.getContext());
executor.setProgressListener(iCommand.getProgressListener());
executor.parse(iCommand);
return executeCommand(iCommand, executor);
} catch (ORetryQueryException e) {
if (iCommand instanceof OQueryAbstract) {
final OQueryAbstract query = (OQueryAbstract) iCommand;
query.reset();
}
continue;
}
}
}
use of com.orientechnologies.orient.core.command.OCommandExecutor in project orientdb by orientechnologies.
the class OServerCommandPostCommand method execute.
@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
final String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: command/<database>/<language>/<command-text>[/limit][/<fetchPlan>]");
// TRY TO GET THE COMMAND FROM THE URL, THEN FROM THE CONTENT
final String language = urlParts.length > 2 ? urlParts[2].trim() : "sql";
String text = urlParts.length > 3 ? urlParts[3].trim() : iRequest.content;
final int limit = urlParts.length > 4 ? Integer.parseInt(urlParts[4].trim()) : -1;
String fetchPlan = urlParts.length > 5 ? urlParts[5] : null;
final String accept = iRequest.getHeader("accept");
Object params = null;
String mode = "resultset";
if (iRequest.content != null && !iRequest.content.isEmpty()) {
// CONTENT REPLACES TEXT
if (iRequest.content.startsWith("{")) {
// JSON PAYLOAD
final ODocument doc = new ODocument().fromJSON(iRequest.content);
text = doc.field("command");
params = doc.field("parameters");
if (doc.containsField("mode"))
mode = doc.field("mode");
if (params instanceof Collection) {
final Object[] paramArray = new Object[((Collection) params).size()];
((Collection) params).toArray(paramArray);
params = paramArray;
}
} else {
text = iRequest.content;
}
}
if (text == null)
throw new IllegalArgumentException("text cannot be null");
iRequest.data.commandInfo = "Command";
iRequest.data.commandDetail = text;
ODatabaseDocument db = null;
Object response;
try {
db = getProfiledDatabaseInstance(iRequest);
final OCommandRequestText cmd = (OCommandRequestText) OCommandManager.instance().getRequester(language);
cmd.setText(text);
cmd.setLimit(limit);
cmd.setFetchPlan(fetchPlan);
final OCommandExecutor executor = OCommandManager.instance().getExecutor(cmd);
executor.setContext(cmd.getContext());
executor.setProgressListener(cmd.getProgressListener());
executor.parse(cmd);
if (!executor.isIdempotent() && iRequest.httpMethod.equals("GET"))
throw new OCommandExecutionException("Cannot execute non idempotent command using HTTP GET");
// REQUEST CAN'T MODIFY THE RESULT, SO IT'S CACHEABLE
cmd.setCacheableResult(true);
if (params == null) {
response = db.command(cmd).execute();
} else {
response = db.command(cmd).execute(params);
}
fetchPlan = executor.getFetchPlan();
String format = null;
if (iRequest.parameters.get("format") != null)
format = iRequest.parameters.get("format");
if (fetchPlan != null)
if (format != null)
format += ",fetchPlan:" + fetchPlan;
else
format = "fetchPlan:" + fetchPlan;
Map<String, Object> additionalContent = null;
final List<String> tips = (List<String>) executor.getContext().getVariable("tips");
if (tips != null) {
additionalContent = new HashMap<String, Object>(1);
additionalContent.put("warnings", tips);
}
if (iRequest.getHeader("TE") != null)
iResponse.setStreaming(true);
iResponse.writeResult(response, format, accept, additionalContent, mode);
} finally {
if (db != null) {
db.activateOnCurrentThread();
db.close();
}
}
return false;
}
Aggregations