title: Should database connections be opened and closed in every CRUD method?
body: <p>I am using GORM ORM for Postgres access in a Go application.
  I have got 4 functions, Create, Update, Delete, and Read in a database repository.</p>
  
  <p>In each of these functions, I open a database connection, perform a CRUD operation and then close the connection just after the operation is performed as you will see <a href="https://www.daniweb.com/programming/web-development/threads/91483/when-should-you-close-a-database-connection" rel="nofollow noreferrer">here</a> and <a href="https://tutorialedge.net/golang/golang-orm-tutorial/" rel="nofollow noreferrer">here</a> and in the code snippet below, using <a href="https://gorm.io/" rel="nofollow noreferrer">GORM</a></p>
  
  <pre><code>func (e *Example) Create(m *model.Example) (*model.Example, error) {
      // open a database session
      dbSession, err := e.OpenDB() //gorm.Open("postgres", connStr)
      if err != nil {
          log.Log(err)
          return nil, err
      }
  
      // close database connection after operation is completed
      defer dbSession.Close()
  
      // create item
      db := dbSession.Create(m)
  
      if db.Error != nil {
          return nil, db.Error
      }
  
      return m, nil
  }
  </code></pre>
  
  <p>Is that the correct practice  or should  one database connection be shared  in the whole application and let the ORM handle managing connections? as stated <a href="https://github.com/jinzhu/gorm/issues/1053" rel="nofollow noreferrer">here</a>? </p>