限制資料庫連線

這段程式碼片段示範如何設定閒置和開啟連線的數量上限,藉此設定連線集區。限制連線有助於管理資料庫資源,並防止應用程式耗用過多資料庫伺服器資源。

程式碼範例

Go

如要向 AlloyDB 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。

// Set maximum number of connections in idle connection pool.
db.SetMaxIdleConns(5)

// Set maximum number of open connections to the database.
db.SetMaxOpenConns(7)

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 範例瀏覽工具