Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
354 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package mssqlx | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
) | ||
|
||
func retryFunc(query string, f func() (interface{}, error)) (result interface{}, err error) { | ||
for retry := 0; retry < 50; retry++ { | ||
if result, err = f(); err == nil { | ||
return | ||
} | ||
|
||
switch err { | ||
case sql.ErrConnDone: | ||
|
||
case sql.ErrTxDone, sql.ErrNoRows: | ||
return | ||
|
||
default: | ||
if isErrBadConn(err) || IsDeadlock(err) { | ||
time.Sleep(5 * time.Millisecond) | ||
} else { | ||
return | ||
} | ||
} | ||
} | ||
|
||
if err == sql.ErrConnDone || isErrBadConn(err) { | ||
reportError(query, err) | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package mssqlx | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
) | ||
|
||
type Stmt struct { | ||
*sql.Stmt | ||
} | ||
|
||
// Exec executes a prepared statement with the given arguments and returns a Result summarizing the effect of the statement. | ||
func (s *Stmt) Exec(args ...interface{}) (sql.Result, error) { | ||
return s.ExecContext(context.Background(), args...) | ||
} | ||
|
||
// ExecContext executes a prepared statement with the given arguments and returns a Result summarizing the effect of the statement. | ||
func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (result sql.Result, err error) { | ||
r, err := retryFunc("stmt_exec", func() (interface{}, error) { | ||
return s.Stmt.ExecContext(ctx, args...) | ||
}) | ||
if err == nil { | ||
result = r.(sql.Result) | ||
} | ||
return | ||
} | ||
|
||
// Query executes a prepared query statement with the given arguments and returns the query results as a *Rows. | ||
func (s *Stmt) Query(args ...interface{}) (*sql.Rows, error) { | ||
return s.QueryContext(context.Background(), args...) | ||
} | ||
|
||
// QueryContext executes a prepared query statement with the given arguments and returns the query results as a *Rows. | ||
func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (result *sql.Rows, err error) { | ||
r, err := retryFunc("stmt_query", func() (interface{}, error) { | ||
return s.Stmt.QueryContext(ctx, args...) | ||
}) | ||
if err == nil { | ||
result = r.(*sql.Rows) | ||
} | ||
return | ||
} |
Oops, something went wrong.