Posts

Showing posts from March, 2026

New: How to connect to mysql database from go - correct way

  The dbconnect.go Architecture In Go, sql.Open doesn't actually connect to the database—it initializes a Pool . This pool is a manager that sits between your code and the database, handing out "check-out" tickets for connections. 1. The Connection (The "Dial") The DSN string defines the network path. By adding parameters like timeout=30s , you tell the driver how long to wait for the initial handshake before giving up. 2. The Pooling (The "Manager") By setting SetMaxOpenConns , you create a hard ceiling. If you set it to 10, Go will never attempt an 11th connection. Instead, it makes the next request wait in a queue. SetConnMaxLifetime ensures that old, "stale" connections are killed off before the server (DreamHost) forcibly drops them. 3. The Context (The "Stopwatch") Using context.WithTimeout is how you control that queue. Without a context, a query might wait forever for a free connection. With a context, you say: "I ...