fix bug
This commit is contained in:
parent
11843b395f
commit
67d07d9d3e
|
@ -287,8 +287,8 @@ func (e *Engine) DropAll() error {
|
||||||
|
|
||||||
func (e *Engine) CreateTables(beans ...interface{}) error {
|
func (e *Engine) CreateTables(beans ...interface{}) error {
|
||||||
session := e.NewSession()
|
session := e.NewSession()
|
||||||
defer session.Close()
|
|
||||||
err := session.Begin()
|
err := session.Begin()
|
||||||
|
defer session.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,11 +7,12 @@ import (
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"os"
|
"os"
|
||||||
//"time"
|
//"time"
|
||||||
|
"sync/atomic"
|
||||||
xorm "xorm"
|
xorm "xorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Id int
|
Id int64
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,18 +25,10 @@ func mysqlEngine() (*xorm.Engine, error) {
|
||||||
return xorm.NewEngine("mysql", "root:123@/test?charset=utf8")
|
return xorm.NewEngine("mysql", "root:123@/test?charset=utf8")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
var u *User = &User{}
|
||||||
engine, err := sqliteEngine()
|
|
||||||
// engine, err := mysqlEngine()
|
|
||||||
|
|
||||||
if err != nil {
|
func test(engine *xorm.Engine) {
|
||||||
fmt.Println(err)
|
err := engine.CreateTables(u)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
u := &User{}
|
|
||||||
|
|
||||||
err = engine.CreateTables(u)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
|
@ -84,5 +77,24 @@ func main() {
|
||||||
for i := 0; i < size; i++ {
|
for i := 0; i < size; i++ {
|
||||||
<-queue
|
<-queue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conns := atomic.LoadInt32(&xorm.ConnectionNum)
|
||||||
|
fmt.Println("connection number:", conns)
|
||||||
fmt.Println("end")
|
fmt.Println("end")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
engine, err := sqliteEngine()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
test(engine)
|
||||||
|
|
||||||
|
engine, err = mysqlEngine()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
test(engine)
|
||||||
|
}
|
||||||
|
|
21
pool.go
21
pool.go
|
@ -2,8 +2,9 @@ package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
//"fmt"
|
"fmt"
|
||||||
//"sync"
|
//"sync"
|
||||||
|
"sync/atomic"
|
||||||
//"time"
|
//"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -15,21 +16,22 @@ type IConnectionPool interface {
|
||||||
type NoneConnectPool struct {
|
type NoneConnectPool struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ConnectionNum int32 = 0
|
||||||
|
|
||||||
func (p NoneConnectPool) RetrieveDB(engine *Engine) (db *sql.DB, err error) {
|
func (p NoneConnectPool) RetrieveDB(engine *Engine) (db *sql.DB, err error) {
|
||||||
|
atomic.AddInt32(&ConnectionNum, 1)
|
||||||
db, err = engine.OpenDB()
|
db, err = engine.OpenDB()
|
||||||
|
fmt.Printf("--open a connection--%x\n", &db)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p NoneConnectPool) ReleaseDB(engine *Engine, db *sql.DB) {
|
func (p NoneConnectPool) ReleaseDB(engine *Engine, db *sql.DB) {
|
||||||
|
atomic.AddInt32(&ConnectionNum, -1)
|
||||||
|
fmt.Printf("--close a connection--%x\n", &db)
|
||||||
db.Close()
|
db.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*type SimpleConnectPool struct {
|
||||||
var (
|
|
||||||
total int = 0
|
|
||||||
)
|
|
||||||
|
|
||||||
type SimpleConnectPool struct {
|
|
||||||
releasedSessions []*sql.DB
|
releasedSessions []*sql.DB
|
||||||
cur int
|
cur int
|
||||||
usingSessions map[*sql.DB]time.Time
|
usingSessions map[*sql.DB]time.Time
|
||||||
|
@ -44,8 +46,8 @@ func (p SimpleConnectPool) RetrieveDB(engine *Engine) (*sql.DB, error) {
|
||||||
var err error = nil
|
var err error = nil
|
||||||
fmt.Printf("%x, rbegin - released:%v, using:%v\n", &p, p.cur+1, len(p.usingSessions))
|
fmt.Printf("%x, rbegin - released:%v, using:%v\n", &p, p.cur+1, len(p.usingSessions))
|
||||||
if p.cur < 0 {
|
if p.cur < 0 {
|
||||||
total = total + 1
|
ConnectionNum = ConnectionNum + 1
|
||||||
fmt.Printf("new %v\n", total)
|
fmt.Printf("new %v\n", ConnectionNum)
|
||||||
db, err = engine.OpenDB()
|
db, err = engine.OpenDB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -68,6 +70,7 @@ func (p SimpleConnectPool) ReleaseDB(engine *Engine, db *sql.DB) {
|
||||||
defer p.mutex.Unlock()
|
defer p.mutex.Unlock()
|
||||||
fmt.Printf("%x, lbegin - released:%v, using:%v\n", &p, p.cur+1, len(p.usingSessions))
|
fmt.Printf("%x, lbegin - released:%v, using:%v\n", &p, p.cur+1, len(p.usingSessions))
|
||||||
if p.cur >= 29 {
|
if p.cur >= 29 {
|
||||||
|
ConnectionNum = ConnectionNum - 1
|
||||||
db.Close()
|
db.Close()
|
||||||
} else {
|
} else {
|
||||||
p.cur = p.cur + 1
|
p.cur = p.cur + 1
|
||||||
|
|
38
session.go
38
session.go
|
@ -221,19 +221,16 @@ func (session *Session) scanMapIntoStruct(obj interface{}, objMap map[string][]b
|
||||||
}
|
}
|
||||||
if x != 0 {
|
if x != 0 {
|
||||||
structInter := reflect.New(structField.Type())
|
structInter := reflect.New(structField.Type())
|
||||||
st := session.Statement
|
newsession := session.Engine.NewSession()
|
||||||
session.Statement.Init()
|
defer newsession.Close()
|
||||||
has, err := session.Id(x).Get(structInter.Interface())
|
has, err := newsession.Id(x).Get(structInter.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
session.Statement = st
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if has {
|
if has {
|
||||||
v = structInter.Elem().Interface()
|
v = structInter.Elem().Interface()
|
||||||
session.Statement = st
|
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("cascade obj is not exist!")
|
fmt.Println("cascade obj is not exist!")
|
||||||
session.Statement = st
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -273,6 +270,9 @@ func (session *Session) innerExec(sql string, args ...interface{}) (sql.Result,
|
||||||
|
|
||||||
func (session *Session) Exec(sql string, args ...interface{}) (sql.Result, error) {
|
func (session *Session) Exec(sql string, args ...interface{}) (sql.Result, error) {
|
||||||
err := session.newDb()
|
err := session.newDb()
|
||||||
|
if session.IsAutoCommit {
|
||||||
|
defer session.Close()
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -457,6 +457,10 @@ func (session *Session) Query(sql string, paramStr ...interface{}) (resultsSlice
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if session.IsAutoCommit {
|
||||||
|
defer session.Close()
|
||||||
|
}
|
||||||
|
|
||||||
if session.Statement.RefTable != nil && session.Statement.RefTable.PrimaryKey != "" {
|
if session.Statement.RefTable != nil && session.Statement.RefTable.PrimaryKey != "" {
|
||||||
sql = strings.Replace(sql, "(id)", session.Statement.RefTable.PrimaryKey, -1)
|
sql = strings.Replace(sql, "(id)", session.Statement.RefTable.PrimaryKey, -1)
|
||||||
}
|
}
|
||||||
|
@ -538,7 +542,11 @@ func (session *Session) Insert(beans ...interface{}) (int64, error) {
|
||||||
isInTransaction := !session.IsAutoCommit
|
isInTransaction := !session.IsAutoCommit
|
||||||
|
|
||||||
if !isInTransaction {
|
if !isInTransaction {
|
||||||
session.Begin()
|
err = session.Begin()
|
||||||
|
defer session.Close()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, bean := range beans {
|
for _, bean := range beans {
|
||||||
|
@ -548,7 +556,7 @@ func (session *Session) Insert(beans ...interface{}) (int64, error) {
|
||||||
lastId, err = session.InsertMulti(bean)
|
lastId, err = session.InsertMulti(bean)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !isInTransaction {
|
if !isInTransaction {
|
||||||
session.Rollback()
|
err = session.Rollback()
|
||||||
}
|
}
|
||||||
return lastId, err
|
return lastId, err
|
||||||
}
|
}
|
||||||
|
@ -558,7 +566,7 @@ func (session *Session) Insert(beans ...interface{}) (int64, error) {
|
||||||
lastId, err = session.InsertOne(sliceValue.Index(i).Interface())
|
lastId, err = session.InsertOne(sliceValue.Index(i).Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !isInTransaction {
|
if !isInTransaction {
|
||||||
session.Rollback()
|
err = session.Rollback()
|
||||||
}
|
}
|
||||||
return lastId, err
|
return lastId, err
|
||||||
}
|
}
|
||||||
|
@ -568,7 +576,7 @@ func (session *Session) Insert(beans ...interface{}) (int64, error) {
|
||||||
lastId, err = session.InsertOne(bean)
|
lastId, err = session.InsertOne(bean)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !isInTransaction {
|
if !isInTransaction {
|
||||||
session.Rollback()
|
err = session.Rollback()
|
||||||
}
|
}
|
||||||
return lastId, err
|
return lastId, err
|
||||||
}
|
}
|
||||||
|
@ -707,7 +715,17 @@ func (session *Session) InsertOne(bean interface{}) (int64, error) {
|
||||||
pkValue := reflect.Indirect(reflect.ValueOf(bean)).FieldByName(table.PKColumn().FieldName)
|
pkValue := reflect.Indirect(reflect.ValueOf(bean)).FieldByName(table.PKColumn().FieldName)
|
||||||
if pkValue.CanSet() {
|
if pkValue.CanSet() {
|
||||||
var v interface{} = id
|
var v interface{} = id
|
||||||
|
switch pkValue.Type().Kind() {
|
||||||
|
case reflect.Int8, reflect.Int16, reflect.Int32:
|
||||||
|
v = int(id)
|
||||||
pkValue.Set(reflect.ValueOf(v))
|
pkValue.Set(reflect.ValueOf(v))
|
||||||
|
case reflect.Int64:
|
||||||
|
pkValue.Set(reflect.ValueOf(v))
|
||||||
|
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||||
|
v = uint(id)
|
||||||
|
pkValue.Set(reflect.ValueOf(v))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
4
xorm.go
4
xorm.go
|
@ -29,14 +29,14 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
|
||||||
return nil, errors.New(fmt.Sprintf("Unsupported driver name: %v", driverName))
|
return nil, errors.New(fmt.Sprintf("Unsupported driver name: %v", driverName))
|
||||||
}
|
}
|
||||||
|
|
||||||
/*engine.Pool = SimpleConnectPool{
|
/*engine.Pool = &SimpleConnectPool{
|
||||||
releasedSessions: make([]*sql.DB, 30),
|
releasedSessions: make([]*sql.DB, 30),
|
||||||
usingSessions: map[*sql.DB]time.Time{},
|
usingSessions: map[*sql.DB]time.Time{},
|
||||||
cur: -1,
|
cur: -1,
|
||||||
maxWaitTimeOut: 14400,
|
maxWaitTimeOut: 14400,
|
||||||
mutex: &sync.Mutex{},
|
mutex: &sync.Mutex{},
|
||||||
}*/
|
}*/
|
||||||
engine.Pool = NoneConnectPool{}
|
engine.Pool = &NoneConnectPool{}
|
||||||
|
|
||||||
return engine, nil
|
return engine, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue