0

I am using GORM with my project, everything is good until I got an error that said:

pq: sorry, too many clients already

I just use the default configuration. The error happened after I did a lot of test requests on my application.

And the error is gone after I restart my application. So, I am thinking that the GORM connection is not released after I'm done with the query. I don't check it very deep enough on GORM code, I just ask here maybe someone has already experience about it?

Dave C
  • 7,729
  • 4
  • 49
  • 65
Apin
  • 2,558
  • 2
  • 21
  • 36
  • Are you creating multiple connections, or passing one around? – matt.s Apr 29 '16 at 09:51
  • Did you use postgres ? if yes, check your `server.properties` file, and update the `MaxConnections` propertie – sbouaked Apr 29 '16 at 09:56
  • @matt.s : I use the default one, just from the GORM documentation. – Apin Apr 29 '16 at 10:00
  • @sbouaked : yes, it is. And what I need to check? – Apin Apr 29 '16 at 10:00
  • on ubuntu open /etc/postgresql/9.1/main/postgresql.conf an upgrade the 'MaxConnections' propertie. http://stackoverflow.com/questions/30778015/how-to-increase-the-max-connections-in-postgres – sbouaked Apr 29 '16 at 10:05
  • @sbouaked : I am not sure if it related to the postgres server. The app is in develop by me and only me access it. Its not make sense if the problem is the postgres server since only me access it. There is something on the go-gorm it self. – Apin Apr 29 '16 at 12:32

2 Answers2

1

The error message you are getting is a PostgreSQL error and not GORM. It is caused as you are opening the database connection more than once.

db, err := gorm.Open("postgres", "user=gorm dbname=gorm")

Should be initiated once and referred to after that.

p_mcp
  • 2,643
  • 8
  • 36
  • 75
  • Don't you think this should be done automatically by GORM? – Apin Apr 29 '16 at 23:12
  • Are you not using Postgresql at all? I'd imagine this should be done by GORM (but couldn't say for sure). – p_mcp May 02 '16 at 09:15
  • I am using Postgresql. Right now I defer a DB.close every open new connection on GORM. – Apin May 02 '16 at 12:35
  • That sounds like your problem. I'd open 1 connection, and make GORM *refer* to it on each instance. A DB connection should only ever be made once and not like you are using it – p_mcp May 02 '16 at 14:39
  • 2
    So, I should call db, err := gorm.Open("postgres", "user=gorm dbname=gorm") once? And use single db to my entire application? Is that safe on multi thread application? – Apin May 02 '16 at 23:27
  • Yes you definitely should and it should handle all of that out of the box – p_mcp May 03 '16 at 09:04
  • 1
    Already change it to single DB connection, and so far so good. I think you can update your answer above, and i will accept your answer. Thanks a lot – Apin May 03 '16 at 09:06
-2
sync.Once.Do(func() {
    instance, err := gorm.Open("postgres",
        "root:password@"+
            "tcp(localhost:3306)/rav"+
            "?charset=utf8&parseTime=True")
    if err != nil {
        log.Println("Connection Failed to Open")
        return
    }
    log.Println("Connection Established here")
    instance.DB().SetMaxIdleConns(10)
    instance.LogMode(true)
})

You can restrict the connection to singleton function so the connection happens once even though it gets called multiple times.