Esempio n. 1
0
func Test(c *gin.Context) {

	var json PostJSON
	err := c.BindJSON(&json)
	if checkErr(c, err, "json error") {
		return
	}

	ctx := c.Value("gContext").(context.Context)

	// データをselect
	model.Find(3)

	// use redis
	redisTest(ctx)

	// データをupdate
	var h *xorm.Engine
	h = hoge.GetDBShardConnection("user", 1)

	session := h.NewSession()
	defer session.Close()

	err = session.Begin()
	if checkErr(c, err, "begin error") {
		return
	}

	var u []model.User
	err = session.Where("id = ?", 3).ForUpdate().Find(&u)
	if checkErr(c, err, "user not found") {
		return
	}

	user := u[0]
	user.Score += 1

	//time.Sleep(3 * time.Second)

	//res, e := session.Id(user.Id).Cols("score").Update(&user) // 単一 PK
	_, err = session.Id(core.PK{user.Id, user.Name}).Update(&user) // 複合PK
	if checkErr(c, err, "update error") {
		return
	}

	err = session.Commit()
	if checkErr(c, err, "commit error") {
		return
	}

	c.JSON(http.StatusOK, &user)
}
Esempio n. 2
0
// FromContext return the gitCommit of the current request
func FromContext(ctx gin.Context) (*rethink.Session, bool) {
	session, ok := ctx.Value(rethinkSessionKey).(*rethink.Session)
	return session, ok
}