Skip to content

Commit

Permalink
fix: call new session every request
Browse files Browse the repository at this point in the history
  • Loading branch information
xappyyy committed Dec 24, 2023
1 parent dc4369d commit c920b3a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/server/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewSqlHandler(db *gorm.DB) interfaces.SqlHandler {

//Create obj in database
func (handler *SqlHandler) Create(obj interface{}) error {
result := handler.db.Session(&gorm.Session{}).Create(obj)
result := handler.db.Create(obj)
if result.Error != nil {
return result.Error
}
Expand All @@ -27,7 +27,7 @@ func (handler *SqlHandler) Create(obj interface{}) error {

//Update column of obj in database with column_name and value
func (handler *SqlHandler) UpdateColumn(obj interface{}, column_name string, value interface{}) error {
result := handler.db.Session(&gorm.Session{}).Model(&obj).Update(column_name, value)
result := handler.db.Model(&obj).Update(column_name, value)
if result.Error != nil {
return result.Error
}
Expand All @@ -36,7 +36,7 @@ func (handler *SqlHandler) UpdateColumn(obj interface{}, column_name string, val

//Get data of object by primary key
func (handler *SqlHandler) GetByPrimaryKey(obj interface{}) error {
result := handler.db.Session(&gorm.Session{}).First(obj)
result := handler.db.First(obj)
if result.Error != nil {
return result.Error
}
Expand All @@ -45,7 +45,7 @@ func (handler *SqlHandler) GetByPrimaryKey(obj interface{}) error {

//Get data of object with associations
func (handler *SqlHandler) GetWithAssociations(obj interface{}) error {
result := handler.db.Session(&gorm.Session{}).Preload(clause.Associations).First(obj)
result := handler.db.Preload(clause.Associations).First(obj)
if result.Error != nil {
return result.Error
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func New() *Server {
fmt.Printf("Error connecting to the database: %v\n", err)
os.Exit(0)
}
db = db.Exec(fmt.Sprintf("SET search_path TO %s", config.Database.Schema))
db = db.Exec(fmt.Sprintf("SET search_path TO %s", config.Database.Schema)).Session(&gorm.Session{})

sqlHandler := NewSqlHandler(db)

Expand Down

0 comments on commit c920b3a

Please sign in to comment.