使用JBoss Cache 1.4.1进行死锁

时间:2011-12-14 06:34:05

标签: hibernate java-ee concurrency deadlock second-level-cache

我使用的是:JBoss AS 4.2.3,JBoss Cache 1.4.1 SP12,Hibernate 3.2.6; JPA 1.0.1 与

<attribute name="NodeLockingScheme">PESSIMISTIC</attribute>
<attribute name="IsolationLevel">READ_COMMITTED</attribute>

<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.use_structured_entries" value="true"/>
<property name="hibernate.cache.usage" value="transactional"/>
<property name="hibernate.cache.provider_class"
          value="org.hibernate.cache.TreeCacheProvider"/>
<property name="hibernate.treecache.local_puts_only" value="true"/>
<property name="hibernate.treecache.querycache.local_writes_only" value="true"/>

设置;

我有这样的实体:

@Entity  
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL, region = "AccountCache")  
public class Account {  
    ...some fields and methods...  
}

@Entity
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class User {
    ....
    @ManyToOne
    private Account account;
    ....
}

所以,我有计时器服务,它获取有关Account的信息并做一些事情(不改变Account,只是获取,并改变另一个实体)。当用户登录,并在页面中工作(切换页面,在其中做一些事情(不是更改AccountUser实体)),并且此时计时器服务开始工作,用户页面被阻止,并且15秒后抛出错误:

....many lines of stack and such errors...
Error performing load command
org.hibernate.cache.CacheException: org.jboss.cache.lock.TimeoutException: 
failure acquiring lock: fqn=/default/AccountCache, 
caller=Thread[http-192.0.2.58-80-6,5,jboss], lock=write 
owner=GlobalTransaction:<null>:88 (org.jboss.cache.lock.LockStrategyReadCommitted@63b596)
....many lines of stack and such errors...

似乎GlobalTransaction:<null>:88是用户tx,callertimer service

通常用户线程工作正常,不到15秒。如果在@Cache中禁用Account,则一切正常(似乎)。

如果启用Optimistic锁定方案,则在同一并发(某些时候)会抛出此类错误:

Caused by: org.jboss.cache.optimistic.DataVersioningException:
Tx attempted to create /default/AccountCache/com.****.entity.account.Account#1 anew.
It has already been created since this tx started by another (possibly remote) tx.

问题))) 有什么问题?是TreeCache问题,还是我的应用问题? 我做错了什么?所有交易均由集装箱管理......

1 个答案:

答案 0 :(得分:0)

首先尝试通过删除物理文件或以编程方式清除缓存。如果没有,请尝试使用<attribute name="NodeLockingScheme">MVCC</attribute>,不管怎样,不推荐使用乐观和悲观锁定方案。