From d90967009adcea2d878e27a56ba32024394932ee Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 1 Sep 2013 10:37:46 +0800 Subject: [PATCH] fixed maxconns bug --- doc.go | 8 ++++++++ engine.go | 11 ++++------- examples/maxconnect.go | 4 ++-- mapper.go | 7 ------- mysql.go | 7 ------- pool.go | 27 +++++++++------------------ postgres.go | 7 ------- session.go | 7 ------- sqlite3.go | 7 ------- statement.go | 7 ------- table.go | 13 ++++--------- xorm.go | 7 ------- 12 files changed, 27 insertions(+), 85 deletions(-) create mode 100644 doc.go diff --git a/doc.go b/doc.go new file mode 100644 index 00000000..bd6ae5e3 --- /dev/null +++ b/doc.go @@ -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 diff --git a/engine.go b/engine.go index b74132e6..a03bfb29 100644 --- a/engine.go +++ b/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()) diff --git a/examples/maxconnect.go b/examples/maxconnect.go index afbcc837..41c66d20 100644 --- a/examples/maxconnect.go +++ b/examples/maxconnect.go @@ -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++ { diff --git a/mapper.go b/mapper.go index be998b54..a00682b5 100644 --- a/mapper.go +++ b/mapper.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 ( diff --git a/mysql.go b/mysql.go index c18276fb..b70bc4c4 100644 --- a/mysql.go +++ b/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 ( diff --git a/pool.go b/pool.go index b8caa273..3dc74329 100644 --- a/pool.go +++ b/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 { - //fmt.Println("signaling...") - p.cond.Signal() - } + //if p.curConns >= p.maxConns-2 { + //fmt.Println("signaling...") + p.cond.Signal() + //} p.curConns -= 1 + p.cond.L.Unlock() } } diff --git a/postgres.go b/postgres.go index 1a8040e9..4dc6555d 100644 --- a/postgres.go +++ b/postgres.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 "strconv" diff --git a/session.go b/session.go index fc7db522..7162f8ac 100644 --- a/session.go +++ b/session.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 ( diff --git a/sqlite3.go b/sqlite3.go index bd5712b5..8f47cf03 100644 --- a/sqlite3.go +++ b/sqlite3.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 type sqlite3 struct { diff --git a/statement.go b/statement.go index 4f044283..1156e353 100644 --- a/statement.go +++ b/statement.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 ( diff --git a/table.go b/table.go index 356aa015..c31a9d5d 100644 --- a/table.go +++ b/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 } diff --git a/xorm.go b/xorm.go index a965f713..3671a2b8 100644 --- a/xorm.go +++ b/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 (