解决mysql中事务的deadlock问题步骤

1. 通过调整innodb_lock_wait_timeout的值,默认50,增加这个值不是一个好办法,会从某种程序上减少响应速度。

2. 调整隔离模式(isolation)为应用可以接受的比较低的级别,例如设置为:READ COMMITTED
如果应用接收不了,低的隔离级别,可以考虑使用session,在执行容易产生deadlock的事务之前执行命令:SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;然后执行事务。

3. 调整事务为合理的大小,事务越小越不容易产生死锁。

4. 对于容易产生死锁的事务来说,可以一直指定需要的资源,既加锁所有涉及的表,不使用先使用意向锁,在使用排它锁。例如:

set autocommit=0;

lock tables t1 write, t2, write , t3 read;

update statement;

commit;

unlock tables t1,t2,t3;

5. 对不同的应用访问同一表,使用相同的顺序,对于表里面的数据,访问里面的行也使用相同次序。

6. 优化表的索引,因为innodb是row lock, row lock是对索引上的索引项加锁,优化调整索引在一定程度上能避免一些死锁问题。

7. 使用show innodb status 来分析TRANSACTIONS里面的内容,分析那些语句产生死锁,从而做出一些调整,或者使用maatkit工具中mk-deadlock-logger 来记录那些语句产生deadlock.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Kettle连接MySQL时可能会出现死锁问题,这是因为两个事务锁住了对方的数据。解决这个问题的方法是使用事务来保证数据的一致性,同时避免出现死锁。具体的解决方案如下: 1. 在Kettle使用事务来保证数据的一致性,可以使用“Transaction”步骤来实现。 2. 在MySQL使用“innodb_lock_wait_timeout”参数来设置等待锁的超时时间,避免出现死锁。 3. 在Kettle使用“Table Input”步骤来读取数据时,可以使用“FOR UPDATE”语句来锁定数据,避免出现死锁。 下面是一个使用事务解决死锁问题的Kettle作业的例子: ```kettle <?xml version="1.0" encoding="UTF-8"?> <job> <name>test_transaction</name> <description/> <extended_description/> <job_version>1.0</job_version> <job_status>0</job_status> <entry_list> <entry> <name>Start</name> <description/> <type>START</type> <plugin_id>START</plugin_id> <status>0</status> <attributes> <attribute> <code>nr</code> <attribute_key>Nr</attribute_key> <value>0</value> </attribute> </attributes> </entry> <entry> <name>Transaction Start</name> <description/> <type>TRANS</type> <plugin_id>TRANS</plugin_id> <status>0</status> <attributes> <attribute> <code>name</code> <attribute_key>name</attribute_key> <value>transaction_start</value> </attribute> <attribute> <code>use_batch</code> <attribute_key>use_batch</attribute_key> <value>N</value> </attribute> <attribute> <code>use_log</code> <attribute_key>use_log</attribute_key> <value>N</value> </attribute> <attribute> <code>use_thread_priorities</code> <attribute_key>use_thread_priorities</attribute_key> <value>N</value> </attribute> <attribute> <code>use_single_threaded</code> <attribute_key>use_single_threaded</attribute_key> <value>N</value> </attribute> <attribute> <code>set_logfile</code> <attribute_key>set_logfile</attribute_key> <value>N</value> </attribute> <attribute> <code>set_loglevel</code> <attribute_key>set_loglevel</attribute_key> <value>0</value> </attribute> <attribute> <code>set_transaction_size</code> <attribute_key>set_transaction_size</attribute_key> <value>1000</value> </attribute> <attribute> <code>set_commit</code> <attribute_key>set_commit</attribute_key> <value>1000</value> </attribute> <attribute> <code>set_rollback</code> <attribute_key>set_rollback</attribute_key> <value>1000</value> </attribute> </attributes> <connections> <connection> <from>Transaction Start</from> <from_nr>0</from_nr> <to>Table Input</to> <to_nr>0</to_nr> <from_type>TRANS</from_type> <to_type>STEP</to_type> <enabled>Y</enabled> </connection> </connections> </entry> <entry> <name>Table Input</name> <description/> <type>TABLE_INPUT</type> <plugin_id>TableInput</plugin_id> <status>0</status> <attributes> <attribute> <code>connection</code> <attribute_key>connection</attribute_key> <value>test</value> </attribute> <attribute> <code>table</code> <attribute_key>table</attribute_key> <value>test</value> </attribute> <attribute> <code>sql</code> <attribute_key>sql</attribute_key> <value>SELECT * FROM test WHERE id = 1 FOR UPDATE</value> </attribute> </attributes> <connections> <connection> <from>Transaction Start</from> <from_nr>0</from_nr> <to>Table Output</to> <to_nr>0</to_nr> <from_type>STEP</from_type> <to_type>STEP</to_type> <enabled>Y</enabled> </connection> </connections> </entry> <entry> <name>Table Output</name> <description/> <type>TABLE_OUTPUT</type> <plugin_id>TableOutput</plugin_id> <status>0</status> <attributes> <attribute> <code>connection</code> <attribute_key>connection</attribute_key> <value>test</value> </attribute> <attribute> <code>table</code> <attribute_key>table</attribute_key> <value>test</value> </attribute> <attribute> <code>truncate</code> <attribute_key>truncate</attribute_key> <value>N</value> </attribute> <attribute> <code>commit_size</code> <attribute_key>commit_size</attribute_key> <value>1000</value> </attribute> </attributes> <connections> <connection> <from>Table Input</from> <from_nr>0</from_nr> <to>Transaction Commit</to> <to_nr>0</to_nr> <from_type>STEP</from_type> <to_type>TRANS</to_type> <enabled>Y</enabled> </connection> </connections> </entry> <entry> <name>Transaction Commit</name> <description/> <type>TRANS</type> <plugin_id>TRANS</plugin_id> <status>0</status> <attributes> <attribute> <code>name</code> <attribute_key>name</attribute_key> <value>transaction_commit</value> </attribute> <attribute> <code>use_batch</code> <attribute_key>use_batch</attribute_key> <value>N</value> </attribute> <attribute> <code>use_log</code> <attribute_key>use_log</attribute_key> <value>N</value> </attribute> <attribute> <code>use_thread_priorities</code> <attribute_key>use_thread_priorities</attribute_key> <value>N</value> </attribute> <attribute> <code>use_single_threaded</code> <attribute_key>use_single_threaded</attribute_key> <value>N</value> </attribute> <attribute> <code>set_logfile</code> <attribute_key>set_logfile</attribute_key> <value>N</value> </attribute> <attribute> <code>set_loglevel</code> <attribute_key>set_loglevel</attribute_key> <value>0</value> </attribute> <attribute> <code>set_transaction_size</code> <attribute_key>set_transaction_size</attribute_key> <value>1000</value> </attribute> <attribute> <code>set_commit</code> <attribute_key>set_commit</attribute_key> <value>1000</value> </attribute> <attribute> <code>set_rollback</code> <attribute_key>set_rollback</attribute_key> <value>1000</value> </attribute> </attributes> <connections> <connection> <from>Table Output</from> <from_nr>0</from_nr> <to>Success</to> <to_nr>0</to_nr> <from_type>STEP</from_type> <to_type>DUMMY</to_type> <enabled>Y</enabled> </connection> </connections> </entry> <entry> <name>Success</name> <description/> <type>DUMMY</type> <plugin_id>Dummy</plugin_id> <status>0</status> <attributes> <attribute> <code>nr</code> <attribute_key>Nr</attribute_key> <value>0</value> </attribute> </attributes> </entry> </entry_list> <notepads> <notepad> <name/> <value/> </notepad> </notepads> <max_log_lines>0</max_log_lines> <log_size_limit>0</log_size_limit> <log_interval>0</log_interval> <log_file/> <shared_objects_file/> <arguments> <argument> <name>rep</name> <value/> </argument> <argument> <name>dir</name> <value/> </argument> <argument> <name>level</name> <value>Basic</value> </argument> </arguments> <created_user/> <created_date>2021/08/23 16:22:08 CST</created_date> <modified_user/> <modified_date>2021/08/23 16:22:08 CST</modified_date> <size_rowset>5000</size_rowset> <sleep_time_empty>0</sleep_time_empty> <sleep_time_full>0</sleep_time_full> <unique_connections>N</unique_connections> <feedback_shown>Y</feedback_shown> <feedback_size>5000</feedback_size> <using_thread_priorities>N</using_thread_priorities> <shared_objects/> <dependency/> </job> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值