jdbc cancel mysql_当执行Statement的cancel()之后发生了什么?

JDBC的Statement接口提供cancel()方法用于取消执行中的SQL语句。在MySQL中,StatementImpl类实现了此功能,通过发送Kill Query指令来中断线程。cancel()方法会在执行executeQuery()时启动一个超时定时任务,如果在指定时间内未收到响应,将触发超时异常并中断执行。cancelTimeoutMutex变量用于并发控制,确保结果返回后能正确处理已取消的状态。然而,如果结果已经返回而cancel()也设置成功,程序仍可继续,只是网络IO会被关闭。
摘要由CSDN通过智能技术生成

java.sql.Statement接口中有cancel()方法;

/*** Cancels this Statement object if both the DBMS and* driver support aborting an SQL statement.* This method can be used by one thread to cancel a statement that* is being executed by another thread.** @exception SQLException if a database access error occurs or* this method is called on a closed Statement* @exception SQLFeatureNotSupportedException if the JDBC driver does not support* this method*/

void cancel() throws SQLException;

首先:当我们的Statement执行executeQuery(String sql)的时候,其实执行该逻辑的线程会被阻塞一段时间从而等待网络IO返回或者超时;

所以你看注释上说

This method can be used by one thread to cancel a statement that is being executed by another thread.

例如在com.mysql.jdbc.StatementImpl类中的cancel实现,是向数据库发送了Kill Query指令

/*** Cancels this Statement object if both the DBMS and driver support* aborting an SQL statement. This method can be used by one thread to* cancel a statement that is being executed by another thread.*/

public void cancel() throws SQLException {

if (!this.statementExecuting.get()) {

return;

}

if (!this.isClosed &&

this.connection != null &&

this.connection.versionMeetsMinimum(5, 0, 0)) {

Connection cancelConn = null;

java.sql.Statement cancelStmt = null;

try {

cancelConn = this.connection.duplicate();

cancelStmt = cancelConn.createStatement();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flink 可以通过实现自定义的 SourceFunction 来从 MySQL 获取数据。具体步骤如下: 1.引入相关依赖。需要引入 flink-sql-connector-mysql 依赖,该依赖为 Flink 官方提供的 MySQL 连接器。 2.实现自定义的 SourceFunction。需要实现 SourceFunction 接口的 run() 和 cancel() 方法。在 run() 方法,可以使用 JDBC 连接 MySQL 数据库,并执行查询语句,将查询结果作为 Flink 的数据源进行处理。在 cancel() 方法,可以将连接关闭。 3.将自定义的 SourceFunction 添加到 Flink 程序。可以通过 StreamExecutionEnvironment 的 addSource() 方法将自定义的 SourceFunction 添加到 Flink 程序。 示例代码如下: ```java public class MySQLSourceFunction implements SourceFunction<Row> { private final String driverClassName = "com.mysql.jdbc.Driver"; private final String dbUrl = "jdbc:mysql://localhost:3306/test"; private final String query = "SELECT * FROM my_table"; private final String username = "root"; private final String password = "password"; private Connection connection = null; private PreparedStatement statement = null; private ResultSet resultSet = null; @Override public void run(SourceContext<Row> ctx) throws Exception { // 加载驱动 Class.forName(driverClassName); // 连接数据库 connection = DriverManager.getConnection(dbUrl, username, password); // 执行查询 statement = connection.prepareStatement(query); resultSet = statement.executeQuery(); // 处理查询结果 while (resultSet.next()) { Row row = new Row(2); row.setField(0, resultSet.getInt("id")); row.setField(1, resultSet.getString("name")); ctx.collect(row); } } @Override public void cancel() { // 关闭连接 try { if (resultSet != null) { resultSet.close(); } if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } // 将自定义的 SourceFunction 添加到 Flink 程序 StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.addSource(new MySQLSourceFunction()).print(); env.execute(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值