aboutsummaryrefslogtreecommitdiff
path: root/sqlite3.go
diff options
context:
space:
mode:
authorPhilip O'Toole <philip.otoole@yahoo.com>2017-06-17 12:22:09 -0700
committerPhilip O'Toole <philip.otoole@yahoo.com>2017-06-17 12:26:06 -0700
commitcd1cbf523a80ebe39ccb1b829d760852e298c128 (patch)
tree40a8bef15df9ef56a1445c2a42b5cb87a22d4e6d /sqlite3.go
parentMerge pull request #2 from mattn/master (diff)
downloadgolite-cd1cbf523a80ebe39ccb1b829d760852e298c128.tar.gz
golite-cd1cbf523a80ebe39ccb1b829d760852e298c128.tar.xz
Sync database-close and statement-close
Potential fix for issue #426.
Diffstat (limited to 'sqlite3.go')
-rw-r--r--sqlite3.go15
1 files changed, 14 insertions, 1 deletions
diff --git a/sqlite3.go b/sqlite3.go
index 56e55e2..0cd4666 100644
--- a/sqlite3.go
+++ b/sqlite3.go
@@ -113,6 +113,7 @@ import (
"runtime"
"strconv"
"strings"
+ "sync"
"time"
"unsafe"
@@ -157,6 +158,7 @@ type SQLiteDriver struct {
// SQLiteConn implement sql.Conn.
type SQLiteConn struct {
+ dbMu sync.Mutex
db *C.sqlite3
loc *time.Location
txlock string
@@ -679,11 +681,22 @@ func (c *SQLiteConn) Close() error {
return c.lastError()
}
deleteHandles(c)
+ c.dbMu.Lock()
c.db = nil
+ c.dbMu.Unlock()
runtime.SetFinalizer(c, nil)
return nil
}
+func (c *SQLiteConn) dbConnOpen() bool {
+ if c == nil {
+ return false
+ }
+ c.dbMu.Lock()
+ defer c.dbMu.Unlock()
+ return c.db != nil
+}
+
// Prepare the query string. Return a new statement.
func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
return c.prepare(context.Background(), query)
@@ -713,7 +726,7 @@ func (s *SQLiteStmt) Close() error {
return nil
}
s.closed = true
- if s.c == nil || s.c.db == nil {
+ if !s.c.dbConnOpen() {
return errors.New("sqlite statement with already closed database connection")
}
rv := C.sqlite3_finalize(s.s)