我用一些CRUD方法编写了一个简单的MongoDB包:
package backend
import "labix.org/v2/mgo"
type MongoDBConn struct {
session *mgo.Session
}
type ToDo struct {
Title string
Description string
}
func NewMongoDBConn() *MongoDBConn {
return &MongoDBConn{}
}
func (m *MongoDBConn) Connect(url string) *mgo.Session {
session, err := mgo.Dial(url)
if err != nil {
panic(err)
}
m.session = session
return m.session
}
func (m *MongoDBConn) Stop() {
m.session.Close()
}
func (m *MongoDBConn) AddToDo(title, description string) (err error) {
c := m.session.DB("test").C("people")
err = c.Insert(&ToDo{title, description})
if err != nil {
panic(err)
}
return nil
}
我有一个server.go,我在其中创建了一个Http Server,并为不同的URL提供了处理程序。我希望能够连接到MongoDB并在特定的处理程序中调用AddToDo方法。我可以从服务器的main方法连接到DB:
import (
"./backend"
//other boilerplate imports
)
func AddHandler(writer http.ResponseWriter, request *http.Request) {
log.Printf("serving %v %v", request.Method, request.URL.Path[1:])
if request.Method != "POST" {
serve404(writer)
return
}
title := request.FormValue("title")
description := request.FormValue("description")
fmt.Fprintf(writer, " title description %v %v", title, description)
//I can't call mongoConn.AddToDo(title, description) from here
}
func main() {
//connect to mongoDB
mongoConn := backend.NewMongoDBConn()
_ = mongoConn.Connect("localhost")
defer mongoConn.Stop()
}
但我不确定如何从处理程序中调用mongoConn.AddToDo(标题,描述字符串)方法。我应该创建一个全局数据库连接变量吗?
答案 0 :(得分:3)
是的,全局会话是处理这种情况的简单方法。然后,在每个处理程序的顶部,您可以执行以下操作:
func handler(...) {
session := globalSession.Copy()
defer session.Close()
}
以便每个处理程序都可以使用自己的会话。
请注意,复制和关闭会话是廉价操作,内部将对连接池起作用,而不是为每个创建的会话建立新连接。
答案 1 :(得分:2)
两种简单的方法:
1.全球数据库会话
package main
import (
"net/http"
"log"
"fmt"
"./backend"
)
var mongoConn * backend.MongoDBConn
func AddHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("serving %v %v", r.Method, r.URL.Path[1:])
if r.Method != "POST" {
fmt.Fprintln(w, "Not POST Method ")
return
}
title := r.FormValue("title")
description := r.FormValue("description")
fmt.Fprintf(w, " title description %v %v", title, description)
//I can't call mongoConn.AddToDo(title, description) from here
mongoConn.AddToDo(title, description)
}
const AddForm = `
<html><body>
<form method="POST" action="/add">
Name: <input type="text" name="title">
Age: <input type="text" name="description">
<input type="submit" value="Add">
</form>
</body></html>
`
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, AddForm)
}
func main() {
//connect to mongoDB
mongoConn = backend.NewMongoDBConn()
_ = mongoConn.Connect("localhost")
defer mongoConn.Stop()
http.HandleFunc("/", Index)
http.HandleFunc("/add", AddHandler)
log.Println("Start Server:")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}
2.每个请求都有一个新的数据库连接
import (
"./backend"
//other boilerplate imports
)
func AddHandler(writer http.ResponseWriter, request *http.Request) {
log.Printf("serving %v %v", request.Method, request.URL.Path[1:])
if request.Method != "POST" {
serve404(writer)
return
}
title := request.FormValue("title")
description := request.FormValue("description")
fmt.Fprintf(writer, " title description %v %v", title, description)
//................
mongoConn := backend.NewMongoDBConn()
_ = mongoConn.Connect("localhost")
mongoConn.AddToDo(title, description)
//....................
mongoConn.Stop()
}
......
更好的解决方案:
您可以在处理之前创建数据库会话池 请求您选择一个并放入该请求的上下文中。然后 请求完成后,将连接推回池中。
如果池为空,则创建新连接如果池已满 你关闭连接
有关详情,请点击here。