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

Fix to #50 - Call Conn.close() and prevent it being reused if pooled. #56

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func errHandler(dbprocAddr C.long, severity, dberr, oserr C.int, dberrstr, oserr

conn := getConnection(int64(dbprocAddr))
if conn != nil {
conn.addError(err)
conn.addError(err, int(dberr))
}

//fmt.Printf("err: %s", err)
Expand Down
34 changes: 32 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ type Conn struct {
messageNums map[int]int
messageMutex sync.RWMutex

errors map[int]string
errorsMutex sync.RWMutex

currentResult *Result
expiresFromPool time.Time
belongsToPool *ConnPool
Expand All @@ -120,7 +123,12 @@ func (conn *Conn) addMessage(msg string, msgno int) {
conn.messageNums[msgno] = i + 1
}

func (conn *Conn) addError(err string) {
func (conn *Conn) addError(err string, errno int) {
conn.errorsMutex.Lock()
defer conn.errorsMutex.Unlock()

conn.errors[errno] = err

if len(conn.Error) > 0 {
conn.Error += "\n"
}
Expand All @@ -141,6 +149,7 @@ func connectWithCredentials(crd *credentials) (*Conn, error) {
spParamsCache: NewParamsCache(),
credentials: *crd,
messageNums: make(map[int]int),
errors: make(map[int]string),
}
err := conn.reconnect()
if err != nil {
Expand Down Expand Up @@ -188,6 +197,14 @@ func (conn *Conn) close() {
}
}

// Remove a pooled connection from it's pool.
func RemoveFromPool(conn *Conn) *Conn {
if conn.belongsToPool != nil {
conn.belongsToPool.Remove(conn)
}
return conn
}

//ensure only one getDbProc at a time
var getDbProcMutex = &sync.Mutex{}

Expand Down Expand Up @@ -260,11 +277,15 @@ func (conn *Conn) DbUse() error {

func (conn *Conn) clearMessages() {
conn.messageMutex.Lock()
defer conn.messageMutex.Unlock()
conn.errorsMutex.Lock()

conn.Error = ""
conn.errors = make(map[int]string)
conn.Message = ""
conn.messageNums = make(map[int]int)

conn.errorsMutex.Lock()
conn.messageMutex.Unlock()
}

//Returns the number of occurances of a supplied FreeTDS message number.
Expand All @@ -276,6 +297,15 @@ func (conn *Conn) HasMessageNumber(msgno int) int {
return count
}

//Returns the error string for a supplied FreeTDS error number.
//if the error has not occurred then an empty string and false is returned.
func (conn *Conn) HasErrorNumber(errno int) (string, bool) {
conn.errorsMutex.RLock()
err, found := conn.errors[errno]
conn.errorsMutex.RUnlock()
return err, found
}

//Execute sql query.
func (conn *Conn) Exec(sql string) ([]*Result, error) {
results, err := conn.exec(sql)
Expand Down
11 changes: 11 additions & 0 deletions conn_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ func (p *ConnPool) addToPool(conn *Conn) {
}
}

// Remove a pooled connection from the pool,
// forcing a new connection to take it's place.
func (p *ConnPool) Remove(conn *Conn) {
if conn.belongsToPool != p {
return
}
p.connCount--
<-p.poolGuard //remove reservation
conn.belongsToPool = nil
}

//Release connection to the pool.
func (p *ConnPool) Release(conn *Conn) {
if conn.belongsToPool != p {
Expand Down
4 changes: 2 additions & 2 deletions conn_sp.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ func (conn *Conn) ExecSp(spName string, params ...interface{}) (*SpResult, error
return nil, err
}
for i, spParam := range spParams {
//get datavalue for the suplied stored procedure parametar
//get datavalue for the suplied stored procedure parameter
var datavalue *C.BYTE
datalen := 0
if i < len(params) {
param := params[i]
if param != nil {
data, sqlDatalen, err := typeToSqlBuf(int(spParam.UserTypeId), param, conn.freetdsVersionGte095)
if err != nil {
conn.Close() //close the connection
conn.close() //hard close the connection, if pooled don't return it.
return nil, err
}
if len(data) > 0 {
Expand Down