Flink自定义MySQLSink批量写入出现死锁解决

一、错误日志

java.sql.BatchUpdateException: Deadlock found when trying to get lock; try restarting transaction
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.cj.util.Util.handleNewInstance(Util.java:192)
    at com.mysql.cj.util.Util.getInstance(Util.java:167)
    at com.mysql.cj.util.Util.getInstance(Util.java:174)
    at com.mysql.cj.jdbc.exceptions.SQLError.createBatchUpdateException(SQLError.java:224)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeBatchSerially(ClientPreparedStatement.java:847)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeBatchInternal(ClientPreparedStatement.java:435)
    at com.mysql.cj.jdbc.StatementImpl.executeBatch(StatementImpl.java:796)
    at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeBatch(DruidPooledPreparedStatement.java:565)

二、原因分析

  在同一批次、同一事务中操作相同主键的数据,当提交事务时,就会出现死锁。

三、解决思路

  1、在sink算子中做去重处理,将同一批次的数据进行去重处理,保证同一批次提交数据中,不会操作多条主键相同的数据,这样事务就能完整提交,也就不会操作死锁。

  2、sink算子设置一个并行度,保证在同一时间不会并行操作相同主键的数据。

posted @ 2022-08-29 11:29  宜家数据小哥  阅读(857)  评论(0编辑  收藏  举报