请像对待每一行代码一样对待每篇文章 —- 鹰嘴豆
学习目标
开始学习
使用并发发送websocket消息时出现丢包现象并且后台出现IllegalStateException: The remote endpoint was in state [TEXT_PARTIAL_WRITING] which is an invalid state for called method
因为有多个线程在同一时间内对同一个socket进行的消息发送,解决办法在出现并发的地方增加一个锁
Wrap your code in a synchronized method and funnel all calls through this new method. It appears the tomcat web socket cannot handle multiple messages being placed on the same websocket session at the same time. I have code which has been running flawlessly under Glassfish and fell apart instantly when I moved to Tomcat. I then altered my code as explained above and all my problems went away….and there was much rejoicing.
设计一个交易系统的支付代码
交易系统支付代码远程调用银行接口时需要考虑是否超时,以及执行时间长短对高并发下的影响
如果是超时的话需要更新订单的状态,比如说是处理中
如果在支付方法上面加数据库事务那么就会消耗数据库连接池的一个连接,在高并发下如果调用银行接口的时间过长,导致连接池耗尽。这时候需要缩小事务的范围,至少不能包含银行接口调用的处理,如何缩小呢? 需要手动的添加事务,spring提供transactionTemplate来手动添加事务
- 数据库锁有乐观锁,悲观锁。乐观锁是设置版本号而悲观锁则是行锁/表锁。支付系统一般使用状态机来实现高并发加锁,例如只有在状态为1的时候才能执行支付操作,这时候在支付方法的开始先更新这个状态为下一个状态2,然后判断返回值是否更新成功,如果更新的行数为1表示更新成功,为0表示更新失败。因为mysql的update就加一个表锁,因此可以在并发时保证数据的一致性
学习总结
贡献者列表
编辑人 | 编辑时间 | 编辑内容 |
---|---|---|