Search in sources :

Example 1 with DBCTransactionManager

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");
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IDataSourceContainerProvider(org.jkiss.dbeaver.model.IDataSourceContainerProvider) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBPTransactionIsolation(org.jkiss.dbeaver.model.DBPTransactionIsolation) DBCException(org.jkiss.dbeaver.model.exec.DBCException) IEditorPart(org.eclipse.ui.IEditorPart) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) DBCTransactionManager(org.jkiss.dbeaver.model.exec.DBCTransactionManager)

Example 2 with DBCTransactionManager

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;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) DBCTransactionManager(org.jkiss.dbeaver.model.exec.DBCTransactionManager)

Example 3 with DBCTransactionManager

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));
        }
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) DBPTransactionIsolation(org.jkiss.dbeaver.model.DBPTransactionIsolation) DBCException(org.jkiss.dbeaver.model.exec.DBCException) IEditorPart(org.eclipse.ui.IEditorPart) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBPDataSourceInfo(org.jkiss.dbeaver.model.DBPDataSourceInfo) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) Separator(org.eclipse.jface.action.Separator) DBCTransactionManager(org.jkiss.dbeaver.model.exec.DBCTransactionManager)

Example 4 with DBCTransactionManager

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();
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBException(org.jkiss.dbeaver.DBException) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession) DBCTransactionManager(org.jkiss.dbeaver.model.exec.DBCTransactionManager)

Aggregations

DBCTransactionManager (org.jkiss.dbeaver.model.exec.DBCTransactionManager)4 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)3 IEditorPart (org.eclipse.ui.IEditorPart)2 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)2 DBException (org.jkiss.dbeaver.DBException)2 DBPTransactionIsolation (org.jkiss.dbeaver.model.DBPTransactionIsolation)2 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)2 Separator (org.eclipse.jface.action.Separator)1 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)1 DBPDataSourceInfo (org.jkiss.dbeaver.model.DBPDataSourceInfo)1 IDataSourceContainerProvider (org.jkiss.dbeaver.model.IDataSourceContainerProvider)1 DBCSession (org.jkiss.dbeaver.model.exec.DBCSession)1