在对EJB TotoCacheBean调用期间发生了系统异常,
方法:公共...TotoCacheBean.refreshAlertCache() .:注意: javax.ejb.EJBException:由: javax.transaction.RollbackException引起的事务中止
@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的使用?谢谢你的花招。
发布于 2013-04-01 08:27:26
您同时使用ConcurrencyManagementType.BEAN和TransactionAttributeType.SUPPORTS,这是相互矛盾的。
来自文件:
只有在使用容器管理事务划分时才能指定它。
要么拥有ConcurrencyManagementType.CONTAINER,要么使用UserTransaction接口手动管理事务。
关于各种属性类型,请参考这里 &根据您的需求应用。
LinkedHashMap不同步..。
默认情况下,带有@Singleton的bean将具有LockType.WRITE。
LockType.WRITE :用于对bean实例的独占访问。
当客户端调用该方法时,单例会话bean将被锁定到其他客户端。因此,不需要对并发性进行显式控制。
https://stackoverflow.com/questions/15657405
复制相似问题