Skip to content

Instantly share code, notes, and snippets.

@NotBadPad
Created July 24, 2014 02:26
Show Gist options
  • Save NotBadPad/e60da01f0a060ee85720 to your computer and use it in GitHub Desktop.
Save NotBadPad/e60da01f0a060ee85720 to your computer and use it in GitHub Desktop.
go:oracle-conf
package utils
/*
Golang使用Oracle连接目前只能通过oci8提供的开放接口,
通过测试,连接在并发routines执行下会出现问题,为解决此类问题,目前通过设置池中单连接来解决
*/
/**
* conf/app.conf
* oracle.dsn=unamne/upwd@ename(or ip):1521/ename?autocommit=true&charset=utf8
* oracle.maxIdelConns=1
* oracle.maxActives=100
*/
/**
* main.go
* DB, err := utils.OpenOracle()
* if err != nil {
* utils.Logger.Error("init orcale-db dw connection failure:", err.Error())
* return
* } else {
* utils.Logger.Info("init orcale-db dw connection successful.")
* defer DB.Close()
* }
* utils.DB = DB
*/
import (
"database/sql"
"github.com/astaxie/beego"
_ "github.com/mattn/go-oci8"
"os"
)
var (
odsn string
omaxIdleConns int
omaxActives int
)
var ODB *sql.DB
func OpenOracle() (db *sql.DB, err error) {
os.Setenv("NLS_LANG", "SIMPLIFIED CHINESE_CHINA.UTF8")
db, err = sql.Open("oci8", odsn)
if db != nil {
//db.SetMaxIdleConns(omaxIdleConns)
db.SetMaxOpenConns(1) //fixed oci8 bug
}
return
}
func init() {
odsn = beego.AppConfig.String("oracle.dsn")
//omaxIdleConns, _ = beego.AppConfig.Int("oracle.maxIdleConns")
//omaxActives, _ = beego.AppConfig.Int("oracle.maxActives")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment