Skip to content

Commit

Permalink
Refactor session close as xorm already does everything needed interna…
Browse files Browse the repository at this point in the history
…lly (go-gitea#2020)
  • Loading branch information
lafriks authored and lunny committed Jun 21, 2017
1 parent 754482b commit 6db387a
Show file tree
Hide file tree
Showing 23 changed files with 60 additions and 74 deletions.
2 changes: 1 addition & 1 deletion models/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions models/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions models/gpg_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions models/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions models/issue_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion models/issue_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions models/lfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 5 additions & 12 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion models/migrations/v13.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 0 additions & 7 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
4 changes: 2 additions & 2 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions models/org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions models/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit 6db387a

Please sign in to comment.