From 6db387a21e245dc4aa7f009ca60574e42d934ffb Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Wed, 21 Jun 2017 03:57:05 +0300 Subject: [PATCH] Refactor session close as xorm already does everything needed internally (#2020) --- models/attachment.go | 2 +- models/branches.go | 6 +++--- models/gpg_key.go | 4 ++-- models/issue.go | 8 ++++---- models/issue_comment.go | 4 ++-- models/issue_label.go | 8 ++++---- models/issue_milestone.go | 6 +++--- models/issue_user.go | 2 +- models/lfs.go | 4 ++-- models/migrations/migrations.go | 17 +++++------------ models/migrations/v13.go | 2 +- models/models.go | 7 ------- models/org.go | 4 ++-- models/org_team.go | 12 ++++++------ models/pull.go | 4 ++-- models/repo.go | 12 ++++++------ models/repo_collaboration.go | 6 +++--- models/repo_editor.go | 2 +- models/ssh_key.go | 8 ++++---- models/user.go | 6 +++--- models/user_follow.go | 4 ++-- models/user_mail.go | 4 ++-- models/webhook.go | 2 +- 23 files changed, 60 insertions(+), 74 deletions(-) diff --git a/models/attachment.go b/models/attachment.go index d800a47109d..3b3521bf603 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -48,7 +48,7 @@ func (a *Attachment) AfterSet(colName string, _ xorm.Cell) { // IncreaseDownloadCount is update download count + 1 func (a *Attachment) IncreaseDownloadCount() error { sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() // Update download count. if _, err := sess.Exec("UPDATE `attachment` SET download_count=download_count+1 WHERE id=?", a.ID); err != nil { diff --git a/models/branches.go b/models/branches.go index a71c0e96222..6115f9c687f 100644 --- a/models/branches.go +++ b/models/branches.go @@ -95,7 +95,7 @@ func (repo *Repository) AddProtectedBranch(branchName string, canPush bool) erro } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -126,7 +126,7 @@ func (repo *Repository) ChangeProtectedBranch(id int64, canPush bool) error { ProtectedBranch.CanPush = canPush sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -146,7 +146,7 @@ func (repo *Repository) DeleteProtectedBranch(id int64) (err error) { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } diff --git a/models/gpg_key.go b/models/gpg_key.go index 5763ef84038..d39cc54ce60 100644 --- a/models/gpg_key.go +++ b/models/gpg_key.go @@ -127,7 +127,7 @@ func AddGPGKey(ownerID int64, content string) (*GPGKey, error) { //Get DB session sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return nil, err } @@ -267,7 +267,7 @@ func DeleteGPGKey(doer *User, id int64) (err error) { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } diff --git a/models/issue.go b/models/issue.go index d4d0a5a2ffe..b5553b7c82d 100644 --- a/models/issue.go +++ b/models/issue.go @@ -406,7 +406,7 @@ func (issue *Issue) clearLabels(e *xorm.Session, doer *User) (err error) { // Triggers appropriate WebHooks, if any. func (issue *Issue) ClearLabels(doer *User) (err error) { sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -470,7 +470,7 @@ func (ts labelSorter) Swap(i, j int) { // Triggers appropriate WebHooks, if any. func (issue *Issue) ReplaceLabels(labels []*Label, doer *User) (err error) { sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -598,7 +598,7 @@ func (issue *Issue) changeStatus(e *xorm.Session, doer *User, repo *Repository, // ChangeStatus changes issue status to open or closed. func (issue *Issue) ChangeStatus(doer *User, repo *Repository, isClosed bool) (err error) { sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -899,7 +899,7 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) { // NewIssue creates new issue with labels for repository. func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) { sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } diff --git a/models/issue_comment.go b/models/issue_comment.go index ead3f1bed35..2da70decffd 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -502,7 +502,7 @@ type CreateCommentOptions struct { // CreateComment creates comment of issue or commit. func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) { sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return nil, err } @@ -620,7 +620,7 @@ func UpdateComment(c *Comment) error { // DeleteComment deletes the comment func DeleteComment(comment *Comment) error { sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err := sess.Begin(); err != nil { return err } diff --git a/models/issue_label.go b/models/issue_label.go index 67e0ac77b54..19bf01c6c77 100644 --- a/models/issue_label.go +++ b/models/issue_label.go @@ -223,7 +223,7 @@ func DeleteLabel(repoID, labelID int64) error { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -298,7 +298,7 @@ func NewIssueLabel(issue *Issue, label *Label, doer *User) (err error) { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -327,7 +327,7 @@ func newIssueLabels(e *xorm.Session, issue *Issue, labels []*Label, doer *User) // NewIssueLabels creates a list of issue-label relations. func NewIssueLabels(issue *Issue, labels []*Label, doer *User) (err error) { sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -375,7 +375,7 @@ func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) ( // DeleteIssueLabel deletes issue-label relation. func DeleteIssueLabel(issue *Issue, label *Label, doer *User) (err error) { sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } diff --git a/models/issue_milestone.go b/models/issue_milestone.go index cfd2ce17076..d12c309b877 100644 --- a/models/issue_milestone.go +++ b/models/issue_milestone.go @@ -104,7 +104,7 @@ func (m *Milestone) APIFormat() *api.Milestone { // NewMilestone creates new milestone of repository. func NewMilestone(m *Milestone) (err error) { sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -215,7 +215,7 @@ func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -330,7 +330,7 @@ func DeleteMilestoneByRepoID(repoID, id int64) error { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } diff --git a/models/issue_user.go b/models/issue_user.go index 024a3cccd92..11d47dd0a66 100644 --- a/models/issue_user.go +++ b/models/issue_user.go @@ -69,7 +69,7 @@ func updateIssueUserByAssignee(e Engine, issue *Issue) (err error) { // UpdateIssueUserByAssignee updates issue-user relation for assignee. func UpdateIssueUserByAssignee(issue *Issue) (err error) { sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } diff --git a/models/lfs.go b/models/lfs.go index 99b8138ab75..f908cae1f2e 100644 --- a/models/lfs.go +++ b/models/lfs.go @@ -56,7 +56,7 @@ func NewLFSMetaObject(m *LFSMetaObject) (*LFSMetaObject, error) { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return nil, err } @@ -94,7 +94,7 @@ func RemoveLFSMetaObjectByOid(oid string) error { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err := sess.Begin(); err != nil { return err } diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 2a54c497c1f..99fd0124800 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -167,13 +167,6 @@ Please try to upgrade to a lower version (>= v0.6.0) first, then upgrade to curr return nil } -func sessionRelease(sess *xorm.Session) { - if !sess.IsCommitedOrRollbacked { - sess.Rollback() - } - sess.Close() -} - func fixLocaleFileLoadPanic(_ *xorm.Engine) error { cfg, err := ini.Load(setting.CustomConf) if err != nil { @@ -214,7 +207,7 @@ func trimCommitActionAppURLPrefix(x *xorm.Engine) error { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -287,7 +280,7 @@ func issueToIssueLabel(x *xorm.Engine) error { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -330,7 +323,7 @@ func attachmentRefactor(x *xorm.Engine) error { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -408,7 +401,7 @@ func renamePullRequestFields(x *xorm.Engine) (err error) { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -492,7 +485,7 @@ func generateOrgRandsAndSalt(x *xorm.Engine) (err error) { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } diff --git a/models/migrations/v13.go b/models/migrations/v13.go index c9a50a992e0..f81271f9812 100644 --- a/models/migrations/v13.go +++ b/models/migrations/v13.go @@ -23,7 +23,7 @@ func ldapUseSSLToSecurityProtocol(x *xorm.Engine) error { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } diff --git a/models/models.go b/models/models.go index 02a782cde32..6a8fbfe624c 100644 --- a/models/models.go +++ b/models/models.go @@ -49,13 +49,6 @@ type Engine interface { Where(interface{}, ...interface{}) *xorm.Session } -func sessionRelease(sess *xorm.Session) { - if !sess.IsCommitedOrRollbacked { - sess.Rollback() - } - sess.Close() -} - var ( x *xorm.Engine tables []interface{} diff --git a/models/org.go b/models/org.go index 5c2d89a78db..b4cbabcb53c 100644 --- a/models/org.go +++ b/models/org.go @@ -127,7 +127,7 @@ func CreateOrganization(org, owner *User) (err error) { org.Type = UserTypeOrganization sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -481,7 +481,7 @@ func RemoveOrgUser(orgID, userID int64) error { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err := sess.Begin(); err != nil { return err } diff --git a/models/org_team.go b/models/org_team.go index 5c97a7032e9..a7a1ba85d6a 100644 --- a/models/org_team.go +++ b/models/org_team.go @@ -119,7 +119,7 @@ func (t *Team) AddRepository(repo *Repository) (err error) { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -180,7 +180,7 @@ func (t *Team) RemoveRepository(repoID int64) error { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -309,7 +309,7 @@ func UpdateTeam(t *Team, authChanged bool) (err error) { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -354,7 +354,7 @@ func DeleteTeam(t *Team) error { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err := sess.Begin(); err != nil { return err } @@ -482,7 +482,7 @@ func AddTeamMember(team *Team, userID int64) error { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err := sess.Begin(); err != nil { return err } @@ -584,7 +584,7 @@ func removeTeamMember(e Engine, team *Team, userID int64) error { // RemoveTeamMember removes member from given team of given organization. func RemoveTeamMember(team *Team, userID int64) error { sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err := sess.Begin(); err != nil { return err } diff --git a/models/pull.go b/models/pull.go index b5b63bebf11..882ee1b2208 100644 --- a/models/pull.go +++ b/models/pull.go @@ -406,7 +406,7 @@ func (pr *PullRequest) setMerged() (err error) { pr.HasMerged = true sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -602,7 +602,7 @@ func (pr *PullRequest) testPatch() (err error) { // NewPullRequest creates new pull request with labels for repository. func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest, patch []byte) (err error) { sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } diff --git a/models/repo.go b/models/repo.go index f8fef47ebee..8d971384201 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1271,7 +1271,7 @@ func CreateRepository(u *User, opts CreateRepoOptions) (_ *Repository, err error } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return nil, err } @@ -1355,7 +1355,7 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return fmt.Errorf("sess.Begin: %v", err) } @@ -1580,7 +1580,7 @@ func updateRepository(e Engine, repo *Repository, visibilityChanged bool) (err e // UpdateRepository updates a repository func UpdateRepository(repo *Repository, visibilityChanged bool) (err error) { sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -1625,7 +1625,7 @@ func DeleteRepository(uid, repoID int64) error { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -2240,7 +2240,7 @@ func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Reposit } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return nil, err } @@ -2284,7 +2284,7 @@ func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Reposit // Copy LFS meta objects in new session sess2 := x.NewSession() - defer sessionRelease(sess2) + defer sess2.Close() if err = sess2.Begin(); err != nil { return nil, err } diff --git a/models/repo_collaboration.go b/models/repo_collaboration.go index 39df5b9efa2..791f022a3f5 100644 --- a/models/repo_collaboration.go +++ b/models/repo_collaboration.go @@ -46,7 +46,7 @@ func (repo *Repository) AddCollaborator(u *User) error { collaboration.Mode = AccessModeWrite sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -136,7 +136,7 @@ func (repo *Repository) ChangeCollaborationAccessMode(uid int64, mode AccessMode collaboration.Mode = mode sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -161,7 +161,7 @@ func (repo *Repository) DeleteCollaboration(uid int64) (err error) { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } diff --git a/models/repo_editor.go b/models/repo_editor.go index 40898dd4c26..720e0716258 100644 --- a/models/repo_editor.go +++ b/models/repo_editor.go @@ -401,7 +401,7 @@ func DeleteUploads(uploads ...*Upload) (err error) { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } diff --git a/models/ssh_key.go b/models/ssh_key.go index 38b172d2ed4..947b2dabfd0 100644 --- a/models/ssh_key.go +++ b/models/ssh_key.go @@ -430,7 +430,7 @@ func AddPublicKey(ownerID int64, name, content string) (*PublicKey, error) { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return nil, err } @@ -535,7 +535,7 @@ func DeletePublicKey(doer *User, id int64) (err error) { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -732,7 +732,7 @@ func AddDeployKey(repoID int64, name, content string) (*DeployKey, error) { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return nil, err } @@ -812,7 +812,7 @@ func DeleteDeployKey(doer *User, id int64) error { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } diff --git a/models/user.go b/models/user.go index 8d8afdfdb80..6484c8cf696 100644 --- a/models/user.go +++ b/models/user.go @@ -436,7 +436,7 @@ func (u *User) UploadAvatar(data []byte) error { m := resize.Resize(avatar.AvatarSize, avatar.AvatarSize, img, resize.NearestNeighbor) sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -715,7 +715,7 @@ func CreateUser(u *User) (err error) { u.MaxRepoCreation = -1 sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -1033,7 +1033,7 @@ func deleteUser(e *xorm.Session, u *User) error { // but issues/comments/pulls will be kept and shown as someone has been deleted. func DeleteUser(u *User) (err error) { sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } diff --git a/models/user_follow.go b/models/user_follow.go index 2d64d05473a..4bde71cb91c 100644 --- a/models/user_follow.go +++ b/models/user_follow.go @@ -24,7 +24,7 @@ func FollowUser(userID, followID int64) (err error) { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -50,7 +50,7 @@ func UnfollowUser(userID, followID int64) (err error) { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } diff --git a/models/user_mail.go b/models/user_mail.go index 4ccf08e9d9d..285ba74f61b 100644 --- a/models/user_mail.go +++ b/models/user_mail.go @@ -127,7 +127,7 @@ func (email *EmailAddress) Activate() error { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } @@ -208,7 +208,7 @@ func MakeEmailPrimary(email *EmailAddress) error { } sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err } diff --git a/models/webhook.go b/models/webhook.go index 5cb2e13a6d5..b7e687a4616 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -267,7 +267,7 @@ func UpdateWebhook(w *Webhook) error { // ID must be specified and do not assign unnecessary fields. func deleteWebhook(bean *Webhook) (err error) { sess := x.NewSession() - defer sessionRelease(sess) + defer sess.Close() if err = sess.Begin(); err != nil { return err }