use of org.jkiss.dbeaver.model.exec.DBCTransactionManager in project dbeaver by serge-rider.
the class DataSourceAutoCommitHandler method updateElement.
@Override
public void updateElement(UIElement element, Map parameters) {
IWorkbenchWindow workbenchWindow = element.getServiceLocator().getService(IWorkbenchWindow.class);
if (workbenchWindow == null || workbenchWindow.getActivePage() == null) {
return;
}
IEditorPart activeEditor = workbenchWindow.getActivePage().getActiveEditor();
if (activeEditor == null) {
return;
}
boolean autoCommit = true;
DBPTransactionIsolation isolation = null;
DBCExecutionContext context = getExecutionContext(activeEditor);
if (context != null && context.isConnected()) {
DBCTransactionManager txnManager = DBUtils.getTransactionManager(context);
if (txnManager != null) {
try {
// Change auto-commit mode
autoCommit = txnManager.isAutoCommit();
isolation = txnManager.getTransactionIsolation();
} catch (DBCException e) {
log.warn(e);
}
}
} else if (activeEditor instanceof IDataSourceContainerProvider) {
DBPDataSourceContainer container = ((IDataSourceContainerProvider) activeEditor).getDataSourceContainer();
if (container != null) {
autoCommit = container.isDefaultAutoCommit();
isolation = container.getActiveTransactionsIsolation();
}
}
element.setChecked(autoCommit);
// Update command image
element.setIcon(DBeaverIcons.getImageDescriptor(autoCommit ? UIIcon.TXN_COMMIT_AUTO : UIIcon.TXN_COMMIT_MANUAL));
String isolationName = isolation == null ? "?" : isolation.getTitle();
element.setText(autoCommit ? "Switch to manual commit (" + isolationName + ")" : "Switch to auto-commit");
element.setTooltip(autoCommit ? "Manual commit (" + isolationName + ")" : "Auto-commit");
}
use of org.jkiss.dbeaver.model.exec.DBCTransactionManager in project dbeaver by serge-rider.
the class DataSourceAutoCommitHandler method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
DBCExecutionContext context = getExecutionContext(event, true);
if (context != null) {
DBCTransactionManager txnManager = DBUtils.getTransactionManager(context);
if (txnManager != null) {
try {
final DBPDataSourceContainer container = context.getDataSource().getContainer();
boolean newAutocommit = !container.isDefaultAutoCommit();
if (context.isConnected()) {
// Get flag from connection
newAutocommit = !txnManager.isAutoCommit();
}
container.setDefaultAutoCommit(newAutocommit, context, true, new Runnable() {
@Override
public void run() {
// Save config
container.persistConfiguration();
}
});
} catch (DBException e) {
UIUtils.showErrorDialog(null, "Auto-Commit", "Error while toggle auto-commit", e);
}
}
}
return null;
}
use of org.jkiss.dbeaver.model.exec.DBCTransactionManager in project dbeaver by serge-rider.
the class DataSourceTransactionModeContributor method fillContributionItems.
@Override
protected void fillContributionItems(final List<IContributionItem> menuItems) {
IWorkbenchWindow window = DBeaverUI.getActiveWorkbenchWindow();
if (window == null) {
return;
}
IEditorPart activePart = window.getActivePage().getActiveEditor();
DBPDataSourceContainer container = AbstractDataSourceHandler.getDataSourceContainer(activePart);
DBPDataSource dataSource = null;
if (container != null) {
dataSource = container.getDataSource();
}
if (dataSource == null) {
return;
}
final DBPDataSourceInfo dsInfo = dataSource.getInfo();
DBCTransactionManager txnManager = DBUtils.getTransactionManager(dataSource.getDefaultContext(false));
if (txnManager != null) {
menuItems.add(ActionUtils.makeCommandContribution(window, CoreCommands.CMD_TOGGLE_AUTOCOMMIT, CommandContributionItem.STYLE_CHECK));
menuItems.add(new Separator());
// Transactions
DBPTransactionIsolation txnLevelCurrent = null;
try {
txnLevelCurrent = txnManager.getTransactionIsolation();
} catch (DBCException ex) {
log.warn("Can't determine current transaction isolation level", ex);
}
for (DBPTransactionIsolation txi : CommonUtils.safeCollection(dsInfo.getSupportedTransactionsIsolation())) {
if (!txi.isEnabled()) {
continue;
}
menuItems.add(ActionUtils.makeActionContribution(new TransactionIsolationAction(dataSource, txi, txi.equals(txnLevelCurrent)), true));
}
}
}
use of org.jkiss.dbeaver.model.exec.DBCTransactionManager in project dbeaver by serge-rider.
the class AbstractCommandContext method saveChanges.
@Override
public void saveChanges(DBRProgressMonitor monitor) throws DBException {
if (!executionContext.isConnected()) {
throw new DBException("Context [" + executionContext.getContextName() + "] isn't connected to the database");
}
List<CommandQueue> commandQueues = getCommandQueues();
// Validate commands
for (CommandQueue queue : commandQueues) {
for (CommandInfo cmd : queue.commands) {
cmd.command.validateCommand();
}
}
// Execute commands
List<CommandInfo> executedCommands = new ArrayList<>();
try {
for (CommandQueue queue : commandQueues) {
// Make list of not-executed commands
for (int i = 0; i < queue.commands.size(); i++) {
if (monitor.isCanceled()) {
break;
}
CommandInfo cmd = queue.commands.get(i);
while (cmd.mergedBy != null) {
cmd = cmd.mergedBy;
}
if (!cmd.executed) {
// Persist changes
//if (CommonUtils.isEmpty(cmd.persistActions)) {
DBEPersistAction[] persistActions = cmd.command.getPersistActions();
if (!ArrayUtils.isEmpty(persistActions)) {
cmd.persistActions = new ArrayList<>(persistActions.length);
for (DBEPersistAction action : persistActions) {
cmd.persistActions.add(new PersistInfo(action));
}
}
//}
if (!CommonUtils.isEmpty(cmd.persistActions)) {
try (DBCSession session = openCommandPersistContext(monitor, cmd.command)) {
DBException error = null;
for (PersistInfo persistInfo : cmd.persistActions) {
DBEPersistAction.ActionType actionType = persistInfo.action.getType();
if (persistInfo.executed && actionType == DBEPersistAction.ActionType.NORMAL) {
continue;
}
if (monitor.isCanceled()) {
break;
}
try {
if (error == null || actionType == DBEPersistAction.ActionType.FINALIZER) {
queue.objectManager.executePersistAction(session, cmd.command, persistInfo.action);
}
persistInfo.executed = true;
} catch (DBException e) {
persistInfo.error = e;
persistInfo.executed = false;
if (actionType != DBEPersistAction.ActionType.OPTIONAL) {
error = e;
}
}
}
if (error != null) {
throw error;
}
// Commit metadata changes
DBCTransactionManager txnManager = DBUtils.getTransactionManager(session.getExecutionContext());
if (txnManager != null && !txnManager.isAutoCommit()) {
txnManager.commit(session);
}
}
cmd.executed = true;
}
}
if (cmd.executed) {
// should remain - they constructs queue by merging with other commands
synchronized (commands) {
// Remove original command from stack
//final CommandInfo thisCommand = queue.commands.get(i);
commands.remove(cmd);
}
}
if (!executedCommands.contains(cmd)) {
executedCommands.add(cmd);
}
}
}
// Let's clear commands
// If everything went well then there should be nothing to do else.
// But some commands may still remain in queue if they merged each other
// (e.g. create + delete of the same entity produce 2 commands and zero actions).
// There were no exceptions during save so we assume that everything went well
commands.clear();
userParams.clear();
/*
// Refresh object states
for (CommandQueue queue : commandQueues) {
if (queue.getObject() instanceof DBPStatefulObject) {
try {
((DBPStatefulObject) queue.getObject()).refreshObjectState(monitor);
} catch (DBCException e) {
// Just report an error
log.error(e);
}
}
}
*/
} finally {
try {
// Update UI
if (atomic) {
for (CommandInfo cmd : executedCommands) {
if (cmd.reflector != null) {
cmd.reflector.redoCommand(cmd.command);
}
}
}
// Update model
for (CommandInfo cmd : executedCommands) {
cmd.command.updateModel();
}
} catch (Exception e) {
log.warn("Error updating model", e);
}
clearCommandQueues();
clearUndidCommands();
// Notify listeners
for (DBECommandListener listener : getListeners()) {
listener.onSave();
}
}
}
Aggregations