Spring / Eclipselink / MySQL应用程序数据问题

时间:2011-12-27 14:56:54

标签: java mysql spring transactions eclipselink

我的申请遇到了一个奇怪的问题。我已经实现了优惠券评级功能。根据该应用程序,我必须搜索优惠券,然后点击它将显示描述页面,并且在那里有一个评级选项,用于为用户做首选评级。我正在测试的是以下场景:我搜索优惠券“测试”,点击它并转到描述页面并对其进行评级,并检查它是否反映在数据库中。最新的额定值和总评级将反映在描述页面中。然后我再次返回主页,搜索相同的优惠券,点击它转到其描述页面并再次评分。发生的事情是,db正确更新4-6次,之后当我搜索该优惠券并转到描述页面时,它显示了之前的评级值和总评级值,这可能是评价几次时的值之前(此值已在db中重写)。然后,如果我在它上面评价,那么值不会在DB中更新。从那以后,这个功能就破了。我尝试了几个选项,禁用缓存,设置了一些点击eclipselink等,但没有解决问题。请分享您的建议。我的代码中的一些重要部分:

的persistence.xml:

<shared-cache-mode>NONE</shared-cache-mode>
<property name="eclipselink.cache.shared.default" value="false"/>
<property name="eclipselink.query-results-cache" value="false"/>  

Coupon和CouponRating具有单向一对一映射:(之前我使用过双向映射,那么同样存在问题。)

在CouponRating.java中:

@Cacheable(false)
@Entity
@Table(name = "Coupon_Rating")
public class CouponsRatings {
....
....
....
@OneToOne
@JoinColumn(name="coupon_id", unique=true, nullable=false, updatable=false)
private Coupon coupon;

@Version
@Column(name="version")
private long version;

@Column(name="uuid") 
private String uuid;
....
....
....

@Override
public int hashCode() {
    int hash = 3;
    hash = 89 * hash + (this.uuid != null ? this.uuid.hashCode() : 0);
    hash = 89 * hash + Long.valueOf(this.version).intValue();
    return hash;
}

    @Override
public boolean equals(Object obj) {
  if((obj == null) || (this.getClass() != obj.getClass()))
        return false;

  CouponsRatings couponsRatings = (CouponsRatings)obj;
  return (this.version == couponsRatings.getVersion() &&
    this.uuid.equalsIgnoreCase(couponsRatings.getUuid()));
}
....
....

RatingManagerImpl.java

....
....

@Transactional
public CouponsRatings saveRating(CouponsRatings rating) throws AppException{
    return ratingDAO.saveRating(rating);
}

@Transactional
public CouponsRatings updateRating(CouponsRatings rating)
        throws AppException {
    return ratingDAO.updateRating(rating);
}

public CouponsRatings findRatingById(Long id) {
    return ratingDAO.findRatingById(id);
}

public CouponsRatings refreshObject(CouponsRatings rating) {
    return ratingDAO.refreshObject(rating);
}

....
....

在RatingDao中:

....
....
private EntityManager entityManager;

@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}

@Override
public CouponsRatings saveRating(CouponsRatings rating) throws AppException {
try {
       entityManager.persist(rating);
} catch (Exception e) {
   logger.debug("Error in saving coupon. Reason: " + e.getMessage());
   throw new CouponzleAppException(e.getMessage());
}
return rating;
}

@Override
public CouponsRatings findRatingByCouponId(Long id) {
  CouponsRatings couponsRatings = null;
  try {
    couponsRatings = (CouponsRatings) entityManager
            .createNamedQuery("findRatingByCouponId")
            .setHint(QueryHints.CACHE_USAGE, 
                             CacheUsage.DoNotCheckCache)
            .setParameter(1, id).getSingleResult();
       } catch (NoResultException exception) {
        logger.error("No active couponsRatings available for coupon id : "
                    + id);
       }
    return couponsRatings;
}

@Override
public CouponsRatings updateRating(CouponsRatings rating)
        throws AppException {
    CouponsRatings updatedRating = null;
    try {
        updatedRating = entityManager.merge(rating);
    } catch (Exception e) {
        logger.debug("Error in updating coupon. Reason: " + .getMessage());
        throw new AppException(e.getMessage());
    }
    return updatedRating;
}

@Override
public CouponsRatings refreshObject(CouponsRatings rating) {
    CouponsRatings updatedRating = null;
    updatedRating = entityManager.merge(rating);
    entityManager.refresh(updatedRating);
    return rating;
    }

来自Action类:(仅限retreiving和更新senario)

// this sometimes loads old date as i have described
couponsRating = ratingsManager.findRatingByCouponId(Long.valueOf(
                couponId).longValue());

cupnsRating.setCurrentRating(currentRating);
cupnsRating.setLastRatingDate(new Date());
cupnsRating.setRatingSum(ratingSum);
cupnsRating.setTotalRatings(totalRatings);
couponsRating = ratingsManager.updateRating(cupnsRating);
// I tried to refresh updated object after i get the issue, but still no luck:
// couponsRating = ratingsManager.refreshObject(couponsRating);

如果我做错了,请告诉我。感谢。

1 个答案:

答案 0 :(得分:0)

检查ratingsManager的存储和重用方式。我的猜测是有多个实例,每个实例都存储自己的EntityManager实例。 EntityManager具有类似于事务的缓存,因此除非刷新实体的副本,否则它不会看到在其他EntityManage的范围内所做的更改。

如果是这种情况,您可以尝试调用refresh(在修复refreshObject方法以返回刷新的对象然后重新应用更改之后),或者在适当的时间从EntityManager中逐出/清除实体,以便从中重新加载实体数据库。