minus5 / gofreetds

Go Sql Server database driver.
MIT License
113 stars 48 forks source link

Added map to count occurrances of FreeTDS message numbers #37

Closed MarkSonghurst closed 8 years ago

MarkSonghurst commented 8 years ago

I have a need to intercept certain FreeTDS Message Numbers in my daemon, and if they occur act upon them (specifically, delay and then retry if I get 1204 or 1205).

This pull request adds a map to count occurrences of Message Numbers as they are reported and a function to get the count of a specific Message Number. I've also added a necessary mutex which is required to pass "go test --race"

I've also made the Conn.reconnect() function public, so I can reconnect before retrying my procedure call. I could not see another way to do this when using a pooled connection as you cannot actually close a pooled connection, only return it to the pool.

"go fmt" has also been run.

This addresses issue #36

MarkSonghurst commented 8 years ago

@ianic can this get merged please?

ianic commented 8 years ago

I don't feel that making reconnect public is the right move. If I understand we have unusable connection and calling ExecSp on it. Could the solution be to add something like:

   if conn.IsDead() {
      //reconnect
    }

to the: https://github.com/minus5/gofreetds/blob/master/conn_sp.go#L28 like it is in: https://github.com/minus5/gofreetds/blob/master/conn.go#L246

hope that conn.IsDead is returning true in the deadlock case.

MarkSonghurst commented 8 years ago

@ianic commit https://github.com/minus5/gofreetds/pull/37/commits/6ae3a2b645c5fc3465d66d61f0a5b4d466c79d1f reverts reconnect to private again and adds a reconnect call into ExecSp

ianic commented 8 years ago

Maybe we can make a function conn.IsErrorRetriable (for example) which will return true in case of deadlock and lock timeout. It seams to me that for application is sufficient to know what to do, retry or give up. Message numbers are somehow internal to the driver. What do you think about that?

MarkSonghurst commented 8 years ago

I think you'd be committing gofreetds to maintain an internal list of SQL Server error codes which declare when something can be retried, or not. A list which might vary based on SQL Server version?

With direct access to the error codes, an application can use this information for other purposes besides determining when to retry, for example - log deadlocks, alert on permissions errors, etc.

MarkSonghurst commented 8 years ago

@ianic Hi Igor, If you don't want to accept this pull request please can you close it, or just comment here and I will close it myself. I'm trying to finalise up my Go package dependencies for a major release so it would be good to know if I'm pinning to my Fork of gofreetds, or master.

Thanks, Mark.