在调用 EJB TotoCacheBean 期间发生系统异常,
方法:public void ...TotoCacheBean.refreshAlertCache() ...:注意:javax.ejb.EJBException:事务中止原因:com.sun.jts.jta.TransactionManagerImpl.commit(TransactionManagerImpl.java) 处的 javax.transaction.RollbackException :334)
@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 个类。但如果这是我会这样做的原因。或者问题是使用读写锁?谢谢你的把戏