首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >同步链接

同步链接
EN

Stack Overflow用户
提问于 2013-03-27 11:10:01
回答 1查看 95关注 0票数 0

在对EJB TotoCacheBean调用期间发生了系统异常,

方法:公共...TotoCacheBean.refreshAlertCache() .:注意: javax.ejb.EJBException:由: javax.transaction.RollbackException引起的事务中止

代码语言:javascript
复制
@Singleton
@PersistenceContext(name = "persistence/popul", unitName = "popul")
@TransactionAttribute(value = TransactionAttributeType.SUPPORTS)
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
@Startup
public class TotoCacheBean
{
    private final ReadWriteLock lock    = new ReentrantReadWriteLock();

@Inject
private MessageDAO messageDAO;

public enum Type
{
    ALERT, GLOBAL
};

private Map<Type, TotoCache>    cacheStorage;

@PostConstruct
public void postConstruct() throws DAOException
{
    refreshAlertCache();
    refreshGlobalCache();
}

@Schedule(second = "*/20", minute = "*", hour = "*", persistent = false)
public void refreshAlertCache() throws DAOException
{
    refreshCache(Type.ALERT);
}

@Schedule(second = "10", minute = "*", hour = "*", persistent = false)
public void refreshGlobalCache() throws DAOException
{
    refreshCache(Type.GLOBAL);
}       

private void refreshCache(Type type) throws DAOException
{
    Date now = DateTools.getDate();
    LinkedHashMap<String, LinkedHashMap<String, ActMessDTO>> messages = messageDAO.getActMessSorted(now, (type == Type.ALERT));
    setCache(type, new TotoCache(messages, now));
}


public TotoCache getCache(Type type)
{
    lock.readLock().lock();

    try
    {
        return getCacheStorage().get(type);
    }
    finally
    {
        lock.readLock().unlock();
    }
}

private void setCache(Type type, TotoCache cache)
{
    lock.writeLock().lock();

    try
    {
        getCacheStorage().put(type, cache);
    }
    finally
    {
        lock.writeLock().unlock();
    }
}

private Map<Type, TotoCache> getCacheStorage()
{
    if (this.cacheStorage == null)
        this.cacheStorage = new HashMap<Type, TotoCache>();

    return this.cacheStorage;
}

}`

你有什么办法解决这个问题吗?LinkedHashMap不是同步的,但我不想更改它,因为它将更改10个以上的类。但是如果这是我要做的原因。或者问题是ReadWriteLock的使用?谢谢你的花招。

EN

回答 1

Stack Overflow用户

发布于 2013-04-01 08:27:26

您同时使用ConcurrencyManagementType.BEANTransactionAttributeType.SUPPORTS,这是相互矛盾的。

来自文件:

只有在使用容器管理事务划分时才能指定它。

要么拥有ConcurrencyManagementType.CONTAINER,要么使用UserTransaction接口手动管理事务。

关于各种属性类型,请参考这里 &根据您的需求应用。

LinkedHashMap不同步..。

默认情况下,带有@Singleton的bean将具有LockType.WRITE

LockType.WRITE :用于对bean实例的独占访问。

当客户端调用该方法时,单例会话bean将被锁定到其他客户端。因此,不需要对并发性进行显式控制。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15657405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档