简体   繁体   English

Theads僵局问题

[英]Theads deadlock issue

Env: 信封:

  Wildfly 8.1
  Struts
  EJB 3.1
  Postgres 9

Facing a thread deadlock issue. 面临线程死锁问题。 Hoping to get some clarity. 希望能弄清楚。

Code: 码:

  • An action class calls two updates using an EJB 动作类使用EJB调用两次更新
  bean.writeLockedUpdate(o1) #1 ... bean.writeLockedUpdate(o1) #2 
  • EJB bean's writeLockedUpdate(Object) does plain JDBC update via JPA EJB bean的writeLockedUpdate(Object)通过JPA进行纯JDBC更新

Scenario: 场景:

  • UserRequestThread1 starts and completed #1 and doing some work within action class UserRequestThread1启动并完成#1,并在操作类中进行一些工作
  • UserRequestThread2 started and acquired write lock on the bean and is inside bean.writeLockedUpdate(o1). UserRequestThread2启动并获得了对bean的写锁定,并且位于bean.writeLockedUpdate(o1)内部。

Both user threads trying to modify same o1 object. 两个用户线程都试图修改同一 o1对象。

Thread dump snip for those two threads: 这两个线程的线程转储片段:

Thread1: 线程1:

"default task-56" #558 prio=5 os_prio=0 tid=0x724a4400 nid=0x1674 waiting on condition [0x3d2ef000] java.lang.Thread.State: TIMED_WAITING (parking) “默认任务56”#558 prio = 5 os_prio = 0 tid = 0x724a4400 nid = 0x1674等待条件[0x3d2ef000] java.lang.Thread.State:TIMED_WAITING(停车)
at org.jboss.as.ejb3.concurrency.EJBReadWriteLock$WriteLock.tryLock(EJBReadWriteLock.java:209) 在org.jboss.as.ejb3.concurrency.EJBReadWriteLock $ WriteLock.tryLock(EJBReadWriteLock.java:209)

at Bean$$$view22.writeLockedUpdate(Unknown Source) 在Bean $$$ view22.writeLockedUpdate(未知来源)

Thread2: 线程2:

"default task-43" #530 prio=5 os_prio=0 tid=0x6bf10800 nid=0x15aa runnable [0x299c8000] java.lang.Thread.State: RUNNABLE at java.net.SocketInputStream.socketRead0(Native Method) “默认任务-43”#530 prio = 5 os_prio = 0 tid = 0x6bf10800 nid = 0x15aa runnable [0x299c8000] java.lang.Thread.State:在java.net.SocketInputStream.socketRead0(本机方法)处为RUNNABLE
at org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:143) 在org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:143)

at Bean$$$view22.writeLockedUpdate(Unknown Source) 在Bean $$$ view22.writeLockedUpdate(未知来源)

Possible explanation: 可能的解释:

I believe here is what happening. 我相信这里正在发生。

  1. thread2 having acquired bean write lock, is wating for DB row lock 已获得bean写锁的thread2正在等待DB行锁
  2. thread1 having acquired DB row lock, is waiting for bean write lock 已获取数据库行锁的thread1正在等待Bean写锁

Jconsole doesnt show any deadlock. Jconsole没有显示任何死锁。 Guessing its because the second lock being blocked is at DB level 猜测其原因是因为第二个锁被阻止在数据库级别

Queation: 报价:

Is there a way to verify the above explanation ? 有没有办法验证上述解释?

Thanks in advance. 提前致谢。

Got the confirmation from both Java and postgresql DB. 得到了来自Java和postgresql数据库的确认。

  1. The thread dump clearly shows that thread1 is waiting for the EJB write lock 线程转储清楚地表明thread1正在等待EJB写锁

  2. Following query on postgres reveals the operations that are blocked 对postgres的以下查询显示了被阻止的操作

select bl.pid as blocked_pid, bl.mode as blocked_mode, a.usename as blocked_user, ka.query as blocking_statement, now() - ka.query_start as blocking_duration, kl.pid as blocking_pid, kl.mode as blocking_mode, ka.usename as blocking_user, a.query as blocked_statement, now() - a.query_start as blocked_duration from pg_catalog.pg_locks bl join pg_catalog.pg_stat_activity a on bl.pid = a.pid join pg_catalog.pg_locks kl join pg_catalog.pg_stat_activity ka on kl.pid = ka.pid on bl.transactionid = kl.transactionid and bl.pid != kl.pid where not bl.granted 选择bl.pid作为blocked_pid,bl.mode作为blocked_mode,a.usename作为blocked_user,ka.query作为blocking_statement,now()-ka.query_start作为blocking_duration,kl.pid作为blocking_pid,kl.mode作为blocking_mode,ka.usename从pg_catalog.pg_locks bl作为pg_catalog.pg_locks的a.query_start作为blocked_duration的a.query_block,从pg.cata = a.pid的pg_catalog.pg_stat_activity a = bl.transactionid上的ka.pid = kl.transactionid和bl.pid!= kl.pid此处未获得bl.grant

In my case the above query returns one row that shows that update is blocked 就我而言,以上查询返回一行,表明更新被阻止

Thanks, Rakesh 谢谢,Rakesh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM