我要是實現的功能是:
一個用戶會上傳照片,並且可以更新;
每次更新,不刪除數據庫記錄,只把表中的 "isAvailable"字段 狀態從"Y" 改為"N",不更新時間戳字段 "initTime",然後往數據庫中查入一條新紀錄。
然後我出現的問題是,每次更新照片的時候,舊記錄的時間戳字段 "initTime"也會被修改,而且時間比新紀錄要晚20秒左右。
項目中用了spring托管Hibernate事務
application.xml配置如下:
<!-- 配置session factory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!--配置事務管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--配置攔截器-->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 配置bean自動代理 -->
<bean id="autoproxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="proxyTargetClass" value="true"/>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
</bean>
service層代碼(部分,去掉生成文件名,路徑等跟問題無關的代碼)
public ServiceResult imageUploadByUserId(FileVO fileVO, UserLoginInfo loginInfo) throws BaseException {
if (null == fileVO || null == loginInfo) {
setServiceResult(REP_USER_PARAM_ERROR, MSG_USER_PARAM_EMPTY, null);
return mServiceResult;
}
int userRecordId = loginInfo.getUserRecordId();
String imageType = fileVO.getFileType();
//更新原有圖片記錄
List<BaseModel> imagesResult = mImageInfoDAO
.getImageInfoByUserIdAndType(userRecordId, imageType);
if (null != imagesResult) {
for (BaseModel item : imagesResult) {
((TImageInfo) item).setIsNew(IS_NEW_VALUE_NO);
mImageInfoDAO.saveOrUpdateImageInfoByInstance(((TImageInfo) item));
}
}
//保存數據庫
TImageInfo imageInfo = new TImageInfo();
imageInfo.setOwnerRecordId(userRecordId);
imageInfo.setImagePath(targetFileRelativePath);
imageInfo.setThumbnailPath(targetThumbnailRelativePath);
imageInfo.setImageType(imageType);
imageInfo.setImagePurpose(fileVO.getFilePurpose());
imageInfo.setContentType(fileVO.getFileContentType());
imageInfo.setIsNew(IS_NEW_VALUE_YES);
imageInfo.setInitTime(new Timestamp(timestamp));
if (mImageInfoDAO.saveOrUpdateImageInfoByInstance(imageInfo)) {
setServiceResult(REP_SERVER_HANDLE_SUCCESS, null, null);
} else {
setServiceResult(REP_SERVER_HANDLE_ERROR, null, null);
}
return mServiceResult;
}
Dao層查詢方法
public List<BaseModel> getImageInfoByUserIdAndType(int userRecordId, String imageType) throws BaseException {
try {
List<BaseModel> resultList = getSession().createCriteria(TImageInfo.class)
.add(Restrictions.eq(OWNER_RECORD_ID_KEY, userRecordId))
.add(Restrictions.eq(IMAGE_TYPE_KEY, imageType))
.list();
if (null == resultList || resultList.isEmpty()) {
return null;
}
return resultList;
} catch (RuntimeException re) {
BaseException be = ExceptionFactory.getException(DAOException.class,
"save or update Image Info RTException", re);
throw be;
}
Dao層更新方法
public boolean saveOrUpdateImageInfoByInstance(TImageInfo imageInfo) throws BaseException {
try {
getSession().saveOrUpdate(imageInfo);
return true;
} catch (RuntimeException re) {
BaseException be = ExceptionFactory.getException(DAOException.class,
"save or update Image Info RTException", re);
throw be;
}
}
http://www.linuxidc.com/Linux/2014-09/106460.htm
http://ask.csdn.net/questions/182398