问题
在一个事务方法中,发送http请求,由于http请求没有设置sockettimeout导致,请求阻塞,虽然事务方法设置了超时时间1秒,但是一直不超时,程序假死。
//伪代码
public void save(){
//插入数据库一条记录
insert into user(name,age) values('hello',20);
//发送http请求,通知appA
http.get('url');
}
<!--springContext.xml-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" timeout="1"/>
</tx:attributes>
</tx:advice>
分析
原来是对事务超时时间理解不充分导致。
transaction timeout,设置超时时间,每执行一次sql就check一次时间。注意,这里是每执行一次sql就check一次时间,代码中,执行插入sql时,check时间没超时,等再执行http请求时,这个不是sql,所以不会check过期时间,加上请求假死,所以,程序就hold在那,事务也不超时回滚。