1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 05:09:42 +02:00

Improve document transaction scopes

This commit is contained in:
Harvey Kandola 2024-01-10 15:54:56 -05:00
parent c62fa4612b
commit 4210caca48

View file

@ -64,6 +64,13 @@ func (h *Handler) Get(w http.ResponseWriter, r *http.Request) {
return return
} }
var ok bool
ctx.Transaction, ok = h.Runtime.StartTx(sql.LevelReadUncommitted)
if !ok {
h.Runtime.Log.Info("unable to start transaction " + method)
return
}
document, err := h.Store.Document.Get(ctx, id) document, err := h.Store.Document.Get(ctx, id)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
response.WriteNotFoundError(w, method, id) response.WriteNotFoundError(w, method, id)
@ -82,7 +89,6 @@ func (h *Handler) Get(w http.ResponseWriter, r *http.Request) {
// draft mode does not record document views // draft mode does not record document views
if document.Lifecycle == workflow.LifecycleLive { if document.Lifecycle == workflow.LifecycleLive {
ctx.Transaction, err = h.Runtime.Db.Beginx()
if err != nil { if err != nil {
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err) h.Runtime.Log.Error(method, err)
@ -95,9 +101,10 @@ func (h *Handler) Get(w http.ResponseWriter, r *http.Request) {
SourceType: activity.SourceTypeDocument, SourceType: activity.SourceTypeDocument,
ActivityType: activity.TypeRead}) ActivityType: activity.TypeRead})
ctx.Transaction.Commit()
} }
ctx.Transaction.Commit()
h.Store.Audit.Record(ctx, audit.EventTypeDocumentView) h.Store.Audit.Record(ctx, audit.EventTypeDocumentView)
response.WriteJSON(w, document) response.WriteJSON(w, document)
@ -360,6 +367,13 @@ func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
return return
} }
var ok bool
ctx.Transaction, ok = h.Runtime.StartTx(sql.LevelReadUncommitted)
if !ok {
h.Runtime.Log.Info("unable to start transaction " + method)
return
}
doc, err := h.Store.Document.Get(ctx, documentID) doc, err := h.Store.Document.Get(ctx, documentID)
if err != nil { if err != nil {
response.WriteServerError(w, method, err) response.WriteServerError(w, method, err)
@ -396,13 +410,6 @@ func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
return return
} }
ctx.Transaction, err = h.Runtime.Db.Beginx()
if err != nil {
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
_, err = h.Store.Document.Delete(ctx, documentID) _, err = h.Store.Document.Delete(ctx, documentID)
if err != nil { if err != nil {
ctx.Transaction.Rollback() ctx.Transaction.Rollback()
@ -560,6 +567,13 @@ func (h *Handler) FetchDocumentData(w http.ResponseWriter, r *http.Request) {
return return
} }
var ok bool
ctx.Transaction, ok = h.Runtime.StartTx(sql.LevelReadUncommitted)
if !ok {
h.Runtime.Log.Info("unable to start transaction " + method)
return
}
document, err := h.Store.Document.Get(ctx, id) document, err := h.Store.Document.Get(ctx, id)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
response.WriteNotFoundError(w, method, id) response.WriteNotFoundError(w, method, id)
@ -708,13 +722,6 @@ func (h *Handler) FetchDocumentData(w http.ResponseWriter, r *http.Request) {
data.Versions = v data.Versions = v
data.Attachments = a data.Attachments = a
ctx.Transaction, err = h.Runtime.Db.Beginx()
if err != nil {
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
if document.Lifecycle == workflow.LifecycleLive { if document.Lifecycle == workflow.LifecycleLive {
h.Store.Activity.RecordUserActivity(ctx, activity.UserActivity{ h.Store.Activity.RecordUserActivity(ctx, activity.UserActivity{
SpaceID: document.SpaceID, SpaceID: document.SpaceID,
@ -823,10 +830,10 @@ func (h *Handler) Duplicate(w http.ResponseWriter, r *http.Request) {
return return
} }
ctx.Transaction, err = h.Runtime.Db.Beginx() var ok bool
if err != nil { ctx.Transaction, ok = h.Runtime.StartTx(sql.LevelReadUncommitted)
response.WriteServerError(w, method, err) if !ok {
h.Runtime.Log.Error(method, err) h.Runtime.Log.Info("unable to start transaction " + method)
return return
} }