Esempio n. 1
0
func (d *Manage) executeSubPlan(sp *SubPlan, t *Task) error {
	var (
		db  *client.DB
		err error
	)
	if sp.IsDB {
		db, err = client.Open(sp.SubDatabase.Host, sp.SubDatabase.UserName, sp.SubDatabase.Password, "", 0)
	} else {
		db, err = client.Open(sp.SubDatabase.Host, sp.SubDatabase.UserName, sp.SubDatabase.Password, sp.SubDatabase.Name, 0)
	}

	if err != nil {
		// TODO: retry, it must success
		return err
	}
	defer db.Close()
	conn, err := db.GetConn()
	if err != nil {
		// TODO: retry, it must success
		return err
	}
	defer conn.Close()
	_, err = conn.Execute(sp.SQL)
	if err != nil {
		glog.Infof("execute subplan node(%s) sql(%s) error(%v)", sp.SubDatabase.Name, sp.SQL, err)
		return err
	}
	glog.Infof("execute subplan node(%s) sql(%s) done", sp.SubDatabase.Name, sp.SQL)
	return nil
}
Esempio n. 2
0
func (p *Bpool) GetConn(backend *meta.Backend) (*client.Conn, error) {
	// check cahce
	p.RLock()
	db, ok := p.backends[backend.Name]
	p.RUnlock()
	if ok {
		glog.Infof("Get DB(%v) from cache", backend.Name)
		conn, err := db.PopConn()
		if err != nil {
			return nil, err
		}
		return conn, nil
	}
	// create a new one
	var err error
	glog.Infof("Get DB(%v) direct", backend.Name)
	db, err = client.Open(backend.Host, backend.UserName, backend.Password, backend.Name, 0)
	if err != nil {
		return nil, err
	}
	p.Lock()
	srcDB, ok := p.backends[backend.Name]
	if ok {
		db.Close()
		db = srcDB
	}
	p.backends[backend.Name] = db
	p.Unlock()
	return db.PopConn()
}