doucu7525 2017-10-10 07:44
浏览 488

Golang使用JDBC或ODBC或什么连接类型

go version go1.8.1 windows/amd64 and I am importing

"github.com/mattn/go-oci8" 
"database/sql" 

For connecting to my oracle database. Here when I give db username, password, port and table name in connection string

func openAndConnectToDb(sbconfig ConnectorConfig) *sql.DB {
    logger := sbgoclient.Log
    logger.Println("Open the database")
    //  oraprop := LoadConfig("oraproperties.yml")
    fmt.Println("Load config complete")
    orrrr := sbconfig.DB_Username + "/" + sbconfig.DB_Password + "@" + "//" + sbconfig.DB_Ip + ":" + sbconfig.DB_Port + "/" + sbconfig.DB_Schema
    fmt.Println("orrr formed: ", orrrr)
    db, err := sql.Open(sbconfig.DbType, orrrr)
    if err != nil {
        logger.Println("database connection failed...")
        logger.Fatal(err)
    }

    //Use a backoff/retry strategy - we can start this client before
    //the database is started, and see it eventually connect and process
    //queries
    var dbError error
    maxAttempts := 20
    for attempts := 1; attempts <= maxAttempts; attempts++ {
        logger.Println("pinging database...")
        dbError = db.Ping()
        if dbError == nil {
            logger.Println("database ping successfull........")
            fmt.Println("database ping successfull........")
            break
        }
        logger.Println("Ping failed: ", dbError, "retry in ", attempts, " seconds.")
        time.Sleep(time.Duration(attempts) * time.Second)
    }
    if dbError != nil {
        logger.Fatal(dbError)
    }

    return db
}

It fails to connect, whereas in java if I specify username, password, port, servicename and tablename in JDBC connection string it connects successfully.

The output is

time="2017-10-10T13:43:02+05:30" level=info msg="Open the database" 
time="2017-10-10T13:43:02+05:30" level=info msg="pinging database..." 
time="2017-10-10T13:43:24+05:30" level=info msg="Ping failed:  ORA-12170: TNS:Connect timeout occurred
retry in  1  seconds." 
time="2017-10-10T13:43:25+05:30" level=info msg="pinging database..." 
time="2017-10-10T13:43:46+05:30" level=info msg="Ping failed:  ORA-12170: TNS:Connect timeout occurred 
retry in  2  seconds." 
  • 写回答

1条回答 默认 最新

  • doson1998 2017-10-10 08:48
    关注

    I cannot tell you exactly why your code ist not working, but I can provide you with some sample code I am involved in in an advisory capacity:

    https://github.com/odbaeu/oracledb_metricbeat/blob/master/oracledb.go

    Here the code fragment creating a new connection:

    // NewDB returns a new oracle database handle. The dsn value (data source name)
    // must be valid, otherwise an error will be returned.
    //
    //   DSN Format: username/password@host:port/service_name
    func NewDB(ociURL string) (*sql.DB, error) {
        // NLS_LANG is set to American format. At least NLS_NUMERIC_CHARACTERS has to be ".,".
        os.Setenv("NLS_LANG", "AMERICAN_AMERICA.AL32UTF8")
        os.Setenv("NLS_DATE_FORMAT", "YYYY-MM-DD\"T\"HH24:MI:SS")
    
        // Open DB connection
        oConn, err := sql.Open("oci8", ociURL)
        if err != nil {
            return oConn, errors.Wrap(err, "sql open failed")
        }
    
        return oConn, nil
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题