Browse Source

feature db ping with Context

ls 3 years ago
parent
commit
2cac29e0bb
1 changed files with 21 additions and 3 deletions
  1. 21 3
      db/sqlx.go

+ 21 - 3
db/sqlx.go

@@ -1,6 +1,7 @@
 package db
 
 import (
+	"context"
 	"database/sql"
 	"fmt"
 	"sync"
@@ -94,9 +95,11 @@ func (d *DB) Connect() (err error) {
 }
 
 // Close close database connect
-func (d *DB) Close() {
-	// use pool
-	//d.c.Close()
+func (d *DB) Close() error {
+	if d.c != nil {
+		return d.c.Close()
+	}
+	return nil
 }
 
 // Ping verifies a connection to the database is still alive, establishing a connection if necessary.
@@ -104,6 +107,11 @@ func (d *DB) Ping() error {
 	return d.c.Ping()
 }
 
+// PingContext verifies a connection to the database is still alive, establishing a connection if necessary.
+func (d *DB) PingContext(ctx context.Context) error {
+	return d.c.PingContext(ctx)
+}
+
 // Stats returns database statistics.
 func (d *DB) Stats() sql.DBStats {
 	return d.c.Stats()
@@ -292,6 +300,16 @@ func Ping() error {
 	return defaultDb.Ping()
 }
 
+// PingContext verifies a connection to the database is still alive, establishing a connection if necessary.
+func PingContext(ctx context.Context) error {
+	return defaultDb.PingContext(ctx)
+}
+
+// Close close database connect
+func Close() error {
+	return defaultDb.Close()
+}
+
 // Stats returns database statistics.
 func Stats() sql.DBStats {
 	return defaultDb.Stats()