I want a upsert
list of entries, so I have two options: one just uses one session, another instance of the session for each entry. So, in my opinion, the first method may slow down than the second, but will the first cause too many sessions?
1. use one session
func (this *CvStoreServiceImpl) SetCvJobItemMeasureList(accessToken *base_datatype.ServiceAccessToken, versionPolicy string, jobItemList []*cv_common_type.CvJobItemMeasure) (err error) {
session := this.session.Clone()
defer session.Close()
for _, jobItem := range jobItemList {
objKey := &orm.ItemIdKey{
VersionName: versionPolicy, //XXX
ItemId: jobItem.ItemId,
}
obj := orm.ConvertToCvJobItemMeasureObj(versionPolicy, jobItem)
_, err2 := this.jobMeasureCollection.With(session).Upsert(objKey, obj)
if nil != err2 {
err = &common_error.NamedError{err2.Error()}
this.logger.Println(err2.Error())
}
}
return
}
2. copy session for each record
func (this *CvStoreServiceImpl) SetCvJobItemMeasure(accessToken *base_datatype.ServiceAccessToken, versionPolicy string, jobItem *cv_common_type.CvJobItemMeasure) (err error) {
session := this.session.Clone()
defer session.Close()
objKey := &orm.ItemIdKey{
VersionName: versionPolicy, //XXX
ItemId: jobItem.ItemId,
}
obj := orm.ConvertToCvJobItemMeasureObj(versionPolicy, jobItem)
_, err2 := this.jobMeasureCollection.With(session).Upsert(objKey, obj)
if nil != err2 {
err = &common_error.NamedError{err2.Error()}
return
}
return
}
then call this method in forloop:
for _, item := range cvMeasure.GetJobList() {
err = this.SetCvJobItemMeasure(accessToken, versionPolicy, item)
if nil != err {
return
}
}
source
share