Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement [Database Template] [MySQL] [PostgreSQL] [SQLite] Close Database Connection #204

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cmd/template/dbdriver/files/service/mysql.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ import (
_ "github.com/joho/godotenv/autoload"
)

// Service represents a service that interacts with a database.
type Service interface {
// Health returns a map of health status information.
// The keys and values in the map are service-specific.
Health() map[string]string

// Close terminates the database connection.
// It returns an error if the connection cannot be closed.
Close() error
}

type service struct {
Expand Down Expand Up @@ -102,3 +109,12 @@ func (s *service) Health() map[string]string {

return stats
}

// Close closes the database connection.
// It logs a message indicating the disconnection from the specific database.
// If the connection is successfully closed, it returns nil.
// If an error occurs while closing the connection, it returns the error.
func (s *service) Close() error {
log.Printf("Disconnected from database: %s", dbname)
return s.db.Close()
Comment on lines +117 to +119
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't we want to try to close before logging? What happens if s.db.Close returns an error? Will it still have disconnected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't we want to try to close before logging? What happens if s.db.Close returns an error? Will it still have disconnected?

The log.Printf("Disconnected from database: %s", dbname) statement is useful when used with goroutines, such as for asynchronously starting and shutting down the server. However, it's recommended to handle the error returned by s.db.Close before logging the disconnection message. This way, you can ensure that the database connection is properly closed, even if an error occurs during the process. If s.db.Close returns an error, it doesn't necessarily mean that the connection has been successfully disconnected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@briancbarrow example how it work its like this

$ go run cmd/api/main.go
2024/04/04 20:52:02 [H0llyW00dzZ Firewall] [INFO] Connected to database: defaultdb
2024/04/04 20:52:04 [H0llyW00dzZ] [INFO] Starting server on :8080

 ┌───────────────────────────────────────────────────┐ 
 │                    H0llyW00dzZ                    │ 
 │                   Fiber v2.52.4                   │ 
 │               http://127.0.0.1:8080               │ 
 │       (bound on host 0.0.0.0 and port 8080)       │ 
 │                                                   │ 
 │ Handlers ............ 537 Processes ........... 1 │ 
 │ Prefork ....... Disabled  PID ............. 15836 │ 
 └───────────────────────────────────────────────────┘ 

2024/04/04 20:52:12 [H0llyW00dzZ] [INFO] Shutting down server... reason: interrupt
2024/04/04 20:52:12 [H0llyW00dzZ] [INFO] Disconnected from database: defaultdb
2024/04/04 20:52:12 [H0llyW00dzZ] [INFO] Database connection closed.
2024/04/04 20:52:17 [H0llyW00dzZ] [INFO] Server exiting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That My Fiber application is fully managed by goroutines running in parallel.

}
16 changes: 16 additions & 0 deletions cmd/template/dbdriver/files/service/postgres.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ import (
_ "github.com/joho/godotenv/autoload"
)

// Service represents a service that interacts with a database.
type Service interface {
// Health returns a map of health status information.
// The keys and values in the map are service-specific.
Health() map[string]string

// Close terminates the database connection.
// It returns an error if the connection cannot be closed.
Close() error
}

type service struct {
Expand Down Expand Up @@ -96,3 +103,12 @@ func (s *service) Health() map[string]string {

return stats
}

// Close closes the database connection.
// It logs a message indicating the disconnection from the specific database.
// If the connection is successfully closed, it returns nil.
// If an error occurs while closing the connection, it returns the error.
func (s *service) Close() error {
log.Printf("Disconnected from database: %s", database)
return s.db.Close()
}
16 changes: 16 additions & 0 deletions cmd/template/dbdriver/files/service/sqlite.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ import (
_ "github.com/joho/godotenv/autoload"
)

// Service represents a service that interacts with a database.
type Service interface {
// Health returns a map of health status information.
// The keys and values in the map are service-specific.
Health() map[string]string

// Close terminates the database connection.
// It returns an error if the connection cannot be closed.
Close() error
}

type service struct {
Expand Down Expand Up @@ -95,3 +102,12 @@ func (s *service) Health() map[string]string {

return stats
}

// Close closes the database connection.
// It logs a message indicating the disconnection from the specific database.
// If the connection is successfully closed, it returns nil.
// If an error occurs while closing the connection, it returns the error.
func (s *service) Close() error {
log.Printf("Disconnected from database: %s", dburl)
return s.db.Close()
}