0

I use https://github.com/go-xorm/xorm for my Go software to handle database connection.

In every function I use call initdb to get new xorm database session (is it right way?)

func InitDb() (*xorm.Session, error) {

  // Create new engine
  engine, err := xorm.NewEngine("sqlite3",  "Database.db")

  // This is right?
  // defer engine.Close()

  // Create session
  session := engine.NewSession()

  // What about this?
  //defer session.Close()

  return session, err
}

func Hello(){
db := InitDb()
// Or should close database session here?
//defer db.Close()

}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Zola Man
  • 113
  • 9
  • 1
    https://godoc.org/github.com/go-xorm/xorm#hdr-Create_Engine says *"Generally, one engine for an application is enough."* So to answer your question *"is it right way?"*. No it's not. – mkopriva Feb 11 '19 at 12:17
  • Yes that's all, now I don't need to close engine? – Zola Man Feb 11 '19 at 12:41
  • If you want you can close the engine at the bottom of your app's `main` function, or at the top using `defer`. But my guess is that you don't have to, when your app exits any db connections that it created should be discarded... at least that's how it works with postgres and database/sql, i'm not sure about sqlite3 with xorm. – mkopriva Feb 11 '19 at 13:06
  • @MKOPriva thank you for information. I w'll try – Zola Man Feb 11 '19 at 17:43

0 Answers0