我有一个MDB,它使用JPA调用Singleton EJB来维护hourlyTotals。我收到StaleObjectStateException:Row被另一个事务更新或删除
实体Bean代码
@Entity
@Table(name = "hour_db")
public class HourlyTotalEntity {
@Id
private Date transactionTime;
@Column(name="success_count")
private long successCount;
@Version int version;
public Date getTransactionTime() {
return transactionTime;
}
public void setTransactionTime(Date transactionTime) {
this.transactionTime = transactionTime;
}
public void setSuccessCount(long successCount)
{
this.successCount = successCount;
}
public long getSuccessCount()
{
return successCount;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
}
EJB代码
@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
@Lock(LockType.READ)
public class HourlyTotalEJB {
@PersistenceContext (unitName="DashboardJPA")
private EntityManager em;
public void create(Date transactionTime) throws Exception
{
transactionTime = trim(transactionTime);
HourlyTotalEntity entity = em.find(HourlyTotalEntity.class, transactionTime,LockModeType.OPTIMISTIC_FORCE_INCREMENT);
if(entity == null)
{
entity = new HourlyTotalEntity();
entity.setTransactionTime(transaction);
entity.setSuccessCount(0);
}
entity.setAuthSuccessCount(entity.getAuthSuccessCount() + 1);
em.persist(entity);
em.flush();
em.clear();
}
private Date trim(Date date)
{
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
}
MDB在onMessage(消息消息)方法上调用create(transactionTime)。在大量转换期间,方法create(transcationTime)将由MDB同时调用并导致StaleObjectStateException:Row被另一个事务更新或删除
如何解决上述问题?