fixed maxconns bug

This commit is contained in:
Lunny Xiao 2013-09-01 10:37:46 +08:00
parent f0e87becd2
commit d90967009a
12 changed files with 27 additions and 85 deletions

8
doc.go Normal file
View File

@ -0,0 +1,8 @@
// Copyright 2013 The XORM Authors. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
// Package xorm provides is a simple and powerful ORM for Go. It makes your
// database operation simple.
package xorm

View File

@ -1,10 +1,3 @@
// Copyright 2013 The XORM Authors. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
// Package xorm provides is a simple and powerful ORM for Go. It makes your
// database operation simple.
package xorm package xorm
import ( import (
@ -73,6 +66,10 @@ func (engine *Engine) SetPool(pool IConnectPool) error {
return engine.Pool.Init(engine) return engine.Pool.Init(engine)
} }
func (engine *Engine) SetMaxConns(conns int) {
engine.Pool.SetMaxConns(conns)
}
func Type(bean interface{}) reflect.Type { func Type(bean interface{}) reflect.Type {
sliceValue := reflect.Indirect(reflect.ValueOf(bean)) sliceValue := reflect.Indirect(reflect.ValueOf(bean))
return reflect.TypeOf(sliceValue.Interface()) return reflect.TypeOf(sliceValue.Interface())

View File

@ -34,8 +34,8 @@ func test(engine *xorm.Engine) {
return return
} }
engine.Pool.SetMaxConns(5) engine.Pool.SetMaxConns(10)
size := 10 size := 100
queue := make(chan int, size) queue := make(chan int, size)
for i := 0; i < size; i++ { for i := 0; i < size; i++ {

View File

@ -1,10 +1,3 @@
// Copyright 2013 The XORM Authors. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
// Package xorm provides is a simple and powerful ORM for Go. It makes your
// database operation simple.
package xorm package xorm
import ( import (

View File

@ -1,10 +1,3 @@
// Copyright 2013 The XORM Authors. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
// Package xorm provides is a simple and powerful ORM for Go. It makes your
// database operation simple.
package xorm package xorm
import ( import (

27
pool.go
View File

@ -1,14 +1,3 @@
// Copyright 2013 The XORM Authors. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
// Package xorm provides is a simple and powerful ORM for Go. It makes your
// database operation simple.
// This file contains a connection pool interafce and two implements. One is
// NoneConnectionPool is for direct connecting, another is a simple connection
// pool by lock. Attention, the driver may has provided connection pool itself.
// So the default pool is NoneConnectionPool.
package xorm package xorm
import ( import (
@ -92,6 +81,7 @@ type SysConnectPool struct {
maxConns int maxConns int
curConns int curConns int
mutex *sync.Mutex mutex *sync.Mutex
condMutex *sync.Mutex
cond *sync.Cond cond *sync.Cond
} }
@ -111,7 +101,8 @@ func (s *SysConnectPool) Init(engine *Engine) error {
s.maxConns = -1 s.maxConns = -1
s.curConns = 0 s.curConns = 0
s.mutex = &sync.Mutex{} s.mutex = &sync.Mutex{}
s.cond = sync.NewCond(s.mutex) s.condMutex = &sync.Mutex{}
s.cond = sync.NewCond(s.condMutex)
return nil return nil
} }
@ -119,13 +110,13 @@ func (s *SysConnectPool) Init(engine *Engine) error {
func (p *SysConnectPool) RetrieveDB(engine *Engine) (db *sql.DB, err error) { func (p *SysConnectPool) RetrieveDB(engine *Engine) (db *sql.DB, err error) {
if p.maxConns != -1 { if p.maxConns != -1 {
p.cond.L.Lock() p.cond.L.Lock()
defer p.cond.L.Unlock()
//fmt.Println("before retrieve - current connections:", p.curConns, p.maxConns) //fmt.Println("before retrieve - current connections:", p.curConns, p.maxConns)
for p.curConns >= p.maxConns-1 { for p.curConns >= p.maxConns-1 {
//fmt.Println("waiting...") //fmt.Println("waiting...")
p.cond.Wait() p.cond.Wait()
} }
p.curConns += 1 p.curConns += 1
p.cond.L.Unlock()
} }
return p.db, nil return p.db, nil
} }
@ -134,13 +125,13 @@ func (p *SysConnectPool) RetrieveDB(engine *Engine) (db *sql.DB, err error) {
func (p *SysConnectPool) ReleaseDB(engine *Engine, db *sql.DB) { func (p *SysConnectPool) ReleaseDB(engine *Engine, db *sql.DB) {
if p.maxConns != -1 { if p.maxConns != -1 {
p.cond.L.Lock() p.cond.L.Lock()
defer p.cond.L.Unlock()
//fmt.Println("before release - current connections:", p.curConns, p.maxConns) //fmt.Println("before release - current connections:", p.curConns, p.maxConns)
if p.curConns >= p.maxConns-1 { //if p.curConns >= p.maxConns-2 {
//fmt.Println("signaling...") //fmt.Println("signaling...")
p.cond.Signal() p.cond.Signal()
} //}
p.curConns -= 1 p.curConns -= 1
p.cond.L.Unlock()
} }
} }

View File

@ -1,10 +1,3 @@
// Copyright 2013 The XORM Authors. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
// Package xorm provides is a simple and powerful ORM for Go. It makes your
// database operation simple.
package xorm package xorm
import "strconv" import "strconv"

View File

@ -1,10 +1,3 @@
// Copyright 2013 The XORM Authors. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
// Package xorm provides is a simple and powerful ORM for Go. It makes your
// database operation simple.
package xorm package xorm
import ( import (

View File

@ -1,10 +1,3 @@
// Copyright 2013 The XORM Authors. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
// Package xorm provides is a simple and powerful ORM for Go. It makes your
// database operation simple.
package xorm package xorm
type sqlite3 struct { type sqlite3 struct {

View File

@ -1,10 +1,3 @@
// Copyright 2013 The XORM Authors. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
// Package xorm provides is a simple and powerful ORM for Go. It makes your
// database operation simple.
package xorm package xorm
import ( import (

View File

@ -1,10 +1,3 @@
// Copyright 2013 The XORM Authors. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
// Package xorm provides is a simple and powerful ORM for Go. It makes your
// database operation simple.
package xorm package xorm
import ( import (
@ -121,17 +114,19 @@ func Type2SQLType(t reflect.Type) (st SQLType) {
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
if t.Elem() == reflect.TypeOf(b) { if t.Elem() == reflect.TypeOf(b) {
st = SQLType{Blob, 0, 0} st = SQLType{Blob, 0, 0}
} else {
st = SQLType{Text, 0, 0}
} }
case reflect.Bool: case reflect.Bool:
st = SQLType{Bool, 0, 0} st = SQLType{Bool, 0, 0}
case reflect.String: case reflect.String:
st = SQLType{Varchar, 64, 0} st = SQLType{Varchar, 255, 0}
case reflect.Struct: case reflect.Struct:
if t == reflect.TypeOf(tm) { if t == reflect.TypeOf(tm) {
st = SQLType{DateTime, 0, 0} st = SQLType{DateTime, 0, 0}
} }
default: default:
st = SQLType{Varchar, 64, 0} st = SQLType{Varchar, 255, 0}
} }
return return
} }

View File

@ -1,10 +1,3 @@
// Copyright 2013 The XORM Authors. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
// Package xorm provides is a simple and powerful ORM for Go. It makes your
// database operation simple.
package xorm package xorm
import ( import (