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
import (
@ -73,6 +66,10 @@ func (engine *Engine) SetPool(pool IConnectPool) error {
return engine.Pool.Init(engine)
}
func (engine *Engine) SetMaxConns(conns int) {
engine.Pool.SetMaxConns(conns)
}
func Type(bean interface{}) reflect.Type {
sliceValue := reflect.Indirect(reflect.ValueOf(bean))
return reflect.TypeOf(sliceValue.Interface())

View File

@ -34,8 +34,8 @@ func test(engine *xorm.Engine) {
return
}
engine.Pool.SetMaxConns(5)
size := 10
engine.Pool.SetMaxConns(10)
size := 100
queue := make(chan int, size)
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
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
import (

23
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
import (
@ -92,6 +81,7 @@ type SysConnectPool struct {
maxConns int
curConns int
mutex *sync.Mutex
condMutex *sync.Mutex
cond *sync.Cond
}
@ -111,7 +101,8 @@ func (s *SysConnectPool) Init(engine *Engine) error {
s.maxConns = -1
s.curConns = 0
s.mutex = &sync.Mutex{}
s.cond = sync.NewCond(s.mutex)
s.condMutex = &sync.Mutex{}
s.cond = sync.NewCond(s.condMutex)
return nil
}
@ -119,13 +110,13 @@ func (s *SysConnectPool) Init(engine *Engine) error {
func (p *SysConnectPool) RetrieveDB(engine *Engine) (db *sql.DB, err error) {
if p.maxConns != -1 {
p.cond.L.Lock()
defer p.cond.L.Unlock()
//fmt.Println("before retrieve - current connections:", p.curConns, p.maxConns)
for p.curConns >= p.maxConns-1 {
//fmt.Println("waiting...")
p.cond.Wait()
}
p.curConns += 1
p.cond.L.Unlock()
}
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) {
if p.maxConns != -1 {
p.cond.L.Lock()
defer p.cond.L.Unlock()
//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...")
p.cond.Signal()
}
//}
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
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
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
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
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
import (
@ -121,17 +114,19 @@ func Type2SQLType(t reflect.Type) (st SQLType) {
case reflect.Array, reflect.Slice:
if t.Elem() == reflect.TypeOf(b) {
st = SQLType{Blob, 0, 0}
} else {
st = SQLType{Text, 0, 0}
}
case reflect.Bool:
st = SQLType{Bool, 0, 0}
case reflect.String:
st = SQLType{Varchar, 64, 0}
st = SQLType{Varchar, 255, 0}
case reflect.Struct:
if t == reflect.TypeOf(tm) {
st = SQLType{DateTime, 0, 0}
}
default:
st = SQLType{Varchar, 64, 0}
st = SQLType{Varchar, 255, 0}
}
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
import (