提交 b646682d 编写于 作者: S serge-rider

#3616 Query cancel refactoring


Former-commit-id: aa2fb8cd
上级 ab21ce70
......@@ -547,6 +547,26 @@ public class OracleDataSource extends JDBCDataSource
return super.getAdapter(adapter);
}
@Override
public void cancelStatementExecute(DBRProgressMonitor monitor, JDBCStatement statement) throws DBException {
if (driverSupportsQueryCancel()) {
super.cancelStatementExecute(monitor, statement);
} else {
// Oracle server doesn't support single query cancel?
// But we could try to cancel all
try {
Connection connection = statement.getConnection().getOriginal();
BeanUtils.invokeObjectMethod(connection, "cancel");
} catch (Throwable e) {
throw new DBException("Can't cancel session queries", e, this);
}
}
}
private boolean driverSupportsQueryCancel() {
return true;
}
@NotNull
@Override
public OracleDataSource getDataSource() {
......
......@@ -17,6 +17,7 @@
package org.jkiss.dbeaver.ext.wmi.model;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.exec.*;
import org.jkiss.dbeaver.model.impl.AbstractSession;
......@@ -56,7 +57,7 @@ public class WMISession extends AbstractSession {
}
@Override
public void cancelBlock() throws DBException
public void cancelBlock(@NotNull DBRProgressMonitor monitor, @Nullable Thread blockThread) throws DBException
{
// Cancel WMI async call
}
......
......@@ -20,6 +20,7 @@ import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.exec.*;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.wmi.service.WMIConstants;
import org.jkiss.wmi.service.WMIException;
import org.jkiss.wmi.service.WMIObject;
......@@ -165,7 +166,7 @@ public class WMIStatement implements DBCStatement {
}
@Override
public void cancelBlock() throws DBException
public void cancelBlock(@NotNull DBRProgressMonitor monitor, @Nullable Thread blockThread) throws DBException
{
}
......
......@@ -19,9 +19,11 @@ package org.jkiss.dbeaver.model.exec.jdbc;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.exec.DBCStatementType;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCDataSource;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCExecutionContext;
import java.sql.Connection;
import java.sql.SQLException;
......@@ -38,6 +40,8 @@ public interface JDBCSession extends DBCSession, Connection {
Connection getOriginal()
throws SQLException;
JDBCExecutionContext getExecutionContext();
@Override
@NotNull
JDBCStatement prepareStatement(
......
......@@ -31,6 +31,7 @@ import org.jkiss.dbeaver.model.exec.*;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCDatabaseMetaData;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCFactory;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement;
import org.jkiss.dbeaver.model.impl.jdbc.data.handlers.JDBCObjectValueHandler;
import org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCConnectionImpl;
import org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCFactoryDefault;
......@@ -722,4 +723,13 @@ public abstract class JDBCDataSource
public DBDValueHandler getDefaultValueHandler() {
return JDBCObjectValueHandler.INSTANCE;
}
public void cancelStatementExecute(DBRProgressMonitor monitor, JDBCStatement statement) throws DBException {
try {
statement.cancel();
}
catch (SQLException e) {
throw new DBException(e, this);
}
}
}
......@@ -63,7 +63,7 @@ public class JDBCConnectionImpl extends AbstractSession implements JDBCSession,
@NotNull
@Override
public DBCExecutionContext getExecutionContext() {
public JDBCExecutionContext getExecutionContext() {
return context;
}
......@@ -672,7 +672,7 @@ public class JDBCConnectionImpl extends AbstractSession implements JDBCSession,
}
@Override
public void cancelBlock()
public void cancelBlock(@NotNull DBRProgressMonitor monitor, @Nullable Thread blockThread)
throws DBException
{
try {
......
......@@ -29,6 +29,7 @@ import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils;
import org.jkiss.dbeaver.model.qm.QMUtils;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.utils.CommonUtils;
import java.sql.*;
......@@ -88,15 +89,10 @@ public class JDBCStatementImpl<STATEMENT extends Statement> implements JDBCState
}
@Override
public void cancelBlock()
public void cancelBlock(@NotNull DBRProgressMonitor monitor, @Nullable Thread blockThread)
throws DBException
{
try {
this.cancel();
}
catch (SQLException e) {
throw new DBException(e, connection.getDataSource());
}
getConnection().getDataSource().cancelStatementExecute(monitor, this);
}
@NotNull
......
......@@ -16,56 +16,21 @@
*/
package org.jkiss.dbeaver.model.runtime;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.jkiss.dbeaver.DBException;
/**
* Abstract Database Job
*/
public class BlockCanceler extends Job
public class BlockCanceler
{
private static final long INTERRUPT_TIMEOUT = 2000;
private final Thread thread;
private boolean blockCanceled = false;
public BlockCanceler(Thread thread) {
super("Interrupter of " + thread.getName());
setSystem(true);
setUser(false);
this.thread = thread;
}
@Override
protected IStatus run(IProgressMonitor monitor) {
if (!blockCanceled) {
thread.interrupt();
}
return Status.OK_STATUS;
}
public static void cancelBlock(DBRProgressMonitor monitor, Thread blockActiveThread) throws DBException {
BlockCanceler canceler = null;
if (blockActiveThread != null) {
// Schedule thread interrupt job
canceler = new BlockCanceler(blockActiveThread);
canceler.schedule(INTERRUPT_TIMEOUT);
}
DBRBlockingObject block = monitor.getActiveBlock();
if (block != null) {
final Thread thread = Thread.currentThread();
final String threadOldName = thread.getName();
thread.setName("Operation cancel [" + block + "]");
try {
block.cancelBlock();
if (canceler != null) {
canceler.blockCanceled = true;
}
block.cancelBlock(monitor, blockActiveThread);
} catch (Throwable e) {
throw new DBException("Cancel error", e);
} finally {
......
......@@ -17,6 +17,8 @@
package org.jkiss.dbeaver.model.runtime;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
/**
......@@ -30,7 +32,9 @@ public interface DBRBlockingObject {
* In actual implementation this object may not block process at the moment of invocation
* of this method. Implementor should check object's state and cancel blocking on demand.
* @throws DBException on error
* @param monitor monitor
* @param blockThread thread which initiated the block. Can be null.
*/
void cancelBlock() throws DBException;
void cancelBlock(@NotNull DBRProgressMonitor monitor, @Nullable Thread blockThread) throws DBException;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册