fixed maxconns bug
This commit is contained in:
parent
f0e87becd2
commit
d90967009a
|
@ -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
|
11
engine.go
11
engine.go
|
@ -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())
|
||||
|
|
|
@ -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++ {
|
||||
|
|
|
@ -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 (
|
||||
|
|
7
mysql.go
7
mysql.go
|
@ -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
23
pool.go
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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 (
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 (
|
||||
|
|
13
table.go
13
table.go
|
@ -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
|
||||
}
|
||||
|
|
7
xorm.go
7
xorm.go
|
@ -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 (
|
||||
|
|
Loading…
Reference in New Issue