1

我用一些 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 服务器并为不同的 URL 提供了处理程序。我希望能够连接到 MongoDB 并在特定处理程序中调用 AddToDo 方法。我可以从我的服务器的主要方法连接到数据库:

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(title, description string) 方法。我应该创建一个全局数据库连接变量吗?

4

2 回答 2

3

是的,全局会话是一种简单的处理方法。然后,在每个处理程序的顶部,您可以执行以下操作:

func handler(...) {
    session := globalSession.Copy()
    defer session.Close()
}

这样每个处理程序都可以使用自己的会话。

请注意,复制和关闭会话是廉价操作,内部将针对连接池工作,而不是为每个创建的会话建立新连接。

于 2014-01-13T14:22:01.047 回答
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()

} 

......

更好的解决方案:

您可以创建一个数据库会话池,然后在处理请求之前选择一个并放入该请求的上下文中。然后在请求完成后,您将连接推回池中。

如果池为空,则创建一个新连接 如果池已满,则关闭连接

欲了解更多信息,请单击此处

于 2012-12-24T11:42:56.080 回答