本文整理汇总了Golang中github.com/jackc/pgx.NewConnPool函数的典型用法代码示例。如果您正苦于以下问题:Golang NewConnPool函数的具体用法?Golang NewConnPool怎么用?Golang NewConnPool使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewConnPool函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: SetUpSuite
func (s *S) SetUpSuite(c *C) {
s.discoverd, s.cleanup = setup(c)
if err := pgtestutils.SetupPostgres(dbname); err != nil {
c.Fatal(err)
}
pgxConfig := newPgxConnPoolConfig()
pgxpool, err := pgx.NewConnPool(pgxConfig)
if err != nil {
c.Fatal(err)
}
db := postgres.New(pgxpool, nil)
if err = migrateDB(db); err != nil {
c.Fatal(err)
}
db.Close()
// reconnect with prepared statements
pgxConfig.AfterConnect = schema.PrepareStatements
pgxpool, err = pgx.NewConnPool(pgxConfig)
if err != nil {
c.Fatal(err)
}
db = postgres.New(pgxpool, nil)
s.pgx = db.ConnPool
s.pgx.Exec(sqlCreateTruncateTables)
}
开发者ID:ably-forks,项目名称:flynn,代码行数:29,代码来源:setup_test.go
示例2: TestNewConnPool
func TestNewConnPool(t *testing.T) {
t.Parallel()
var numCallbacks int
afterConnect := func(c *pgx.Conn) error {
numCallbacks++
return nil
}
config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: 2, AfterConnect: afterConnect}
pool, err := pgx.NewConnPool(config)
if err != nil {
t.Fatal("Unable to establish connection pool")
}
defer pool.Close()
// It initially connects once
stat := pool.Stat()
if stat.CurrentConnections != 1 {
t.Errorf("Expected 1 connection to be established immediately, but %v were", numCallbacks)
}
// Pool creation returns an error if any AfterConnect callback does
errAfterConnect := errors.New("Some error")
afterConnect = func(c *pgx.Conn) error {
return errAfterConnect
}
config = pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: 2, AfterConnect: afterConnect}
pool, err = pgx.NewConnPool(config)
if err != errAfterConnect {
t.Errorf("Expected errAfterConnect but received unexpected: %v", err)
}
}
开发者ID:yunhor,项目名称:pgx,代码行数:34,代码来源:conn_pool_test.go
示例3: TestAcquireTimeoutSanity
func TestAcquireTimeoutSanity(t *testing.T) {
t.Parallel()
config := pgx.ConnPoolConfig{
ConnConfig: *defaultConnConfig,
MaxConnections: 1,
}
// case 1: default 0 value
pool, err := pgx.NewConnPool(config)
if err != nil {
t.Fatalf("Expected NewConnPool with default config.AcquireTimeout not to fail, instead it failed with '%v'", err)
}
pool.Close()
// case 2: negative value
config.AcquireTimeout = -1 * time.Second
_, err = pgx.NewConnPool(config)
if err == nil {
t.Fatal("Expected NewConnPool with negative config.AcquireTimeout to fail, instead it did not")
}
// case 3: positive value
config.AcquireTimeout = 1 * time.Second
pool, err = pgx.NewConnPool(config)
if err != nil {
t.Fatalf("Expected NewConnPool with positive config.AcquireTimeout not to fail, instead it failed with '%v'", err)
}
defer pool.Close()
}
开发者ID:yunhor,项目名称:pgx,代码行数:30,代码来源:conn_pool_test.go
示例4: Setup
func (c *ConnectionInfo) Setup() {
var err interface{}
c.Pool, err = pgx.NewConnPool(getConfig())
if err != nil {
log.Fatalf("Initializing PostgreSQL connection pool failed: %v", err)
}
}
开发者ID:jaittola,项目名称:loca,代码行数:7,代码来源:db.go
示例5: createConnPool
func createConnPool() (*pgx.ConnPool, error) {
var config pgx.ConnPoolConfig
var err error
config.ConnConfig, err = pgx.ParseEnvLibpq()
if err != nil {
return nil, err
}
if config.Host == "" {
config.Host = "localhost"
}
if config.User == "" {
config.User = os.Getenv("USER")
}
if config.Database == "" {
config.Database = "pgxdata"
}
config.TLSConfig = nil
config.UseFallbackTLS = false
config.MaxConnections = 10
return pgx.NewConnPool(config)
}
开发者ID:jackc,项目名称:pgxdata,代码行数:26,代码来源:integration_test.go
示例6: Open
func (d *DB) Open() error {
conf := pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host: d.config.Host,
Port: uint16(d.config.Port),
User: d.config.Username,
Password: d.config.Password,
Database: d.config.Database,
Logger: d.config.Logger,
},
MaxConnections: d.config.MaxConnections,
}
p, err := pgx.NewConnPool(conf)
if err != nil {
return err
}
db, err := stdlib.OpenFromConnPool(p)
if err != nil {
return err
}
dbx := sqlx.NewDb(db, "pgx")
d.queryer = queryer{impl: dbx}
return nil
}
开发者ID:jonasi,项目名称:pg,代码行数:30,代码来源:db.go
示例7: TestPoolWithAcquireTimeoutSet
func TestPoolWithAcquireTimeoutSet(t *testing.T) {
t.Parallel()
connAllocTimeout := 2 * time.Second
config := pgx.ConnPoolConfig{
ConnConfig: *defaultConnConfig,
MaxConnections: 1,
AcquireTimeout: connAllocTimeout,
}
pool, err := pgx.NewConnPool(config)
if err != nil {
t.Fatalf("Unable to create connection pool: %v", err)
}
defer pool.Close()
// Consume all connections ...
allConnections := acquireAllConnections(t, pool, config.MaxConnections)
defer releaseAllConnections(pool, allConnections)
// ... then try to consume 1 more. It should fail after a short timeout.
_, timeTaken, err := acquireWithTimeTaken(pool)
if err == nil || err.Error() != "Timeout: All connections in pool are busy" {
t.Fatalf("Expected error to be 'Timeout: All connections in pool are busy', instead it was '%v'", err)
}
if timeTaken < connAllocTimeout {
t.Fatalf("Expected connection allocation time to be at least %v, instead it was '%v'", connAllocTimeout, timeTaken)
}
}
开发者ID:yunhor,项目名称:pgx,代码行数:30,代码来源:conn_pool_test.go
示例8: OpenDatabaseConnection
func OpenDatabaseConnection(config configuration.DatabaseConfiguration) (pool *pgx.ConnPool, err error) {
connectionUri := fmt.Sprintf("postgres://%s:%[email protected]%s:%s/%s?sslmode=%s",
config.User,
config.Password,
config.Host,
config.Port,
config.Database,
config.SSLMode)
connectionConfig, err := pgx.ParseURI(connectionUri)
if err != nil {
return pool, err
}
maxConnections := 50
poolConfig := pgx.ConnPoolConfig{connectionConfig, maxConnections, nil}
pool, err = pgx.NewConnPool(poolConfig)
if err != nil {
return pool, err
}
Database = pool
return pool, err
}
开发者ID:bpina,项目名称:go-tracker,代码行数:28,代码来源:data.go
示例9: TestOpenFromConnPool
func TestOpenFromConnPool(t *testing.T) {
connConfig := pgx.ConnConfig{
Host: "127.0.0.1",
User: "pgx_md5",
Password: "secret",
Database: "pgx_test",
}
config := pgx.ConnPoolConfig{ConnConfig: connConfig}
pool, err := pgx.NewConnPool(config)
if err != nil {
t.Fatalf("Unable to create connection pool: %v", err)
}
defer pool.Close()
db, err := stdlib.OpenFromConnPool(pool)
if err != nil {
t.Fatalf("Unable to create connection pool: %v", err)
}
defer closeDB(t, db)
// Can get pgx.ConnPool from driver
driver := db.Driver().(*stdlib.Driver)
if driver.Pool == nil {
t.Fatal("Expected driver opened through OpenFromConnPool to have Pool, but it did not")
}
// Normal sql/database still works
var n int64
err = db.QueryRow("select 1").Scan(&n)
if err != nil {
t.Fatalf("db.QueryRow unexpectedly failed: %v", err)
}
}
开发者ID:pombredanne,项目名称:pgx,代码行数:34,代码来源:sql_test.go
示例10: Connect
// Connect connects to the database using env vars.
// After connect, it creates tables if missing.
func (p *DB) Connect() (err error) {
cfg, err := pgx.ParseEnvLibpq()
if err != nil {
return
}
pool, err := pgx.NewConnPool(pgx.ConnPoolConfig{
ConnConfig: cfg,
MaxConnections: 25,
})
if err != nil {
return
}
c, err := pgx_stdlib.OpenFromConnPool(pool)
if err != nil {
return
}
p.conn, err = gorm.Open("postgres", c)
if err != nil {
return
}
d := p.conn.AutoMigrate(&secrets.Secret{}, &secrets.Key{})
return d.Error
}
开发者ID:nutmegdevelopment,项目名称:nutcracker,代码行数:30,代码来源:postgres.go
示例11: main
func main() {
var err error
connPoolConfig := pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host: "127.0.0.1",
User: "jack",
Password: "jack",
Database: "url_shortener",
Logger: log.New("module", "pgx"),
},
MaxConnections: 5,
AfterConnect: afterConnect,
}
pool, err = pgx.NewConnPool(connPoolConfig)
if err != nil {
log.Crit("Unable to create connection pool", "error", err)
os.Exit(1)
}
http.HandleFunc("/", urlHandler)
log.Info("Starting URL shortener on localhost:8080")
err = http.ListenAndServe("localhost:8080", nil)
if err != nil {
log.Crit("Unable to start web server", "error", err)
os.Exit(1)
}
}
开发者ID:jgrossophoff,项目名称:pgx,代码行数:28,代码来源:main.go
示例12: openPgxStdlib
func openPgxStdlib(config pgx.ConnPoolConfig) (*sql.DB, error) {
connPool, err := pgx.NewConnPool(config)
if err != nil {
return nil, err
}
return stdlib.OpenFromConnPool(connPool)
}
开发者ID:jackc,项目名称:go_db_bench,代码行数:8,代码来源:main.go
示例13: createConnPool
func createConnPool(t *testing.T, maxConnections int) *pgx.ConnPool {
config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: maxConnections}
pool, err := pgx.NewConnPool(config)
if err != nil {
t.Fatalf("Unable to create connection pool: %v", err)
}
return pool
}
开发者ID:yunhor,项目名称:pgx,代码行数:8,代码来源:conn_pool_test.go
示例14: GetPoll
// Get PostgreSQL pool
func (self *Database) GetPoll() (pool *pgx.ConnPool) {
pool, err := pgx.NewConnPool(self.getConfig())
if err != nil {
self.Log.Error(fmt.Sprintf("Unable to create connection pool to database: %v\n", err))
os.Exit(1)
}
self.Log.Info("starting Database service...")
return pool
}
开发者ID:grengojbo,项目名称:ads,代码行数:11,代码来源:database.go
示例15: openTestClientMaxConns
func openTestClientMaxConns(t testing.TB, maxConnections int) *Client {
connPoolConfig := pgx.ConnPoolConfig{
ConnConfig: testConnConfig,
MaxConnections: maxConnections,
AfterConnect: PrepareStatements,
}
pool, err := pgx.NewConnPool(connPoolConfig)
if err != nil {
t.Fatal(err)
}
return NewClient(pool)
}
开发者ID:maheshbale,项目名称:que-go,代码行数:12,代码来源:que_test.go
示例16: initDatabase
func initDatabase(dbHost string, dbUser string, dbPass string, dbName string, dbPort uint16, maxConnectionsInPool int) (*pgx.ConnPool, error) {
var successOrFailure string = "OK"
var config pgx.ConnPoolConfig
config.Host = dbHost
config.User = dbUser
config.Password = dbPass
config.Database = dbName
config.Port = dbPort
config.MaxConnections = maxConnectionsInPool
config.AfterConnect = func(conn *pgx.Conn) error {
worldSelectStmt = mustPrepare(conn, "worldSelectStmt", "SELECT id, randomNumber FROM World WHERE id = $1")
worldUpdateStmt = mustPrepare(conn, "worldUpdateStmt", "UPDATE World SET randomNumber = $1 WHERE id = $2")
fortuneSelectStmt = mustPrepare(conn, "fortuneSelectStmt", "SELECT id, message FROM Fortune")
// Disable synchronous commit for the current db connection
// as a performance optimization.
// See http://www.postgresql.org/docs/current/static/runtime-config-wal.html
// for details.
if _, err := conn.Exec("SET synchronous_commit TO OFF"); err != nil {
log.Fatalf("Error when disabling synchronous commit")
}
return nil
}
fmt.Println("--------------------------------------------------------------------------------------------")
connPool, err := pgx.NewConnPool(config)
if err != nil {
successOrFailure = "FAILED"
log.Println("Connecting to database ", dbName, " as user ", dbUser, " ", successOrFailure, ": \n ", err)
} else {
log.Println("Connecting to database ", dbName, " as user ", dbUser, ": ", successOrFailure)
log.Println("Fetching one record to test if db connection is valid...")
var w World
n := randomWorldNum()
if errPing := connPool.QueryRow("worldSelectStmt", n).Scan(&w.Id, &w.RandomNumber); errPing != nil {
log.Fatalf("Error scanning world row: %s", errPing)
}
log.Println("OK")
}
fmt.Println("--------------------------------------------------------------------------------------------")
return connPool, err
}
开发者ID:Rayne,项目名称:FrameworkBenchmarks,代码行数:53,代码来源:hello.go
示例17: TestNewConnPoolMaxConnectionsCannotBeLessThan2
func TestNewConnPoolMaxConnectionsCannotBeLessThan2(t *testing.T) {
t.Parallel()
config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: 1}
pool, err := pgx.NewConnPool(config)
if err == nil {
pool.Close()
t.Fatal(`Expected NewConnPool to fail with "MaxConnections must be at least 2" error, but it succeeded`)
}
if err.Error() != "MaxConnections must be at least 2" {
t.Fatalf(`Expected NewConnPool to fail with "MaxConnections must be at least 2" error, but it failed with %v`, err)
}
}
开发者ID:rjammala,项目名称:pgx,代码行数:13,代码来源:conn_pool_test.go
示例18: InitConnection
// initial ConnPool of pgx
func (pgdb *PostgresDB) InitConnection() error {
//var pool *pgx.ConnPool
var err error
pgdb.Pool, err = pgx.NewConnPool(pgdb.poolConfig)
if err != nil {
log.Info("Unable to create connection pool", "error", err)
os.Exit(1)
}
log.Info("database connect sueecss")
return nil
}
开发者ID:tsingson,项目名称:todos-backend,代码行数:14,代码来源:todo-storage-postgres.go
示例19: TestNewConnPoolDefaultsTo5MaxConnections
func TestNewConnPoolDefaultsTo5MaxConnections(t *testing.T) {
t.Parallel()
config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig}
pool, err := pgx.NewConnPool(config)
if err != nil {
t.Fatal("Unable to establish connection pool")
}
defer pool.Close()
if n := pool.Stat().MaxConnections; n != 5 {
t.Fatalf("Expected pool to default to 5 max connections, but it was %d", n)
}
}
开发者ID:yunhor,项目名称:pgx,代码行数:14,代码来源:conn_pool_test.go
示例20: setupTestDB
func setupTestDB(c *C, dbname string) *postgres.DB {
if err := pgtestutils.SetupPostgres(dbname); err != nil {
c.Fatal(err)
}
pgxpool, err := pgx.NewConnPool(pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host: os.Getenv("PGHOST"),
Database: dbname,
},
})
if err != nil {
c.Fatal(err)
}
return postgres.New(pgxpool, nil)
}
开发者ID:ably-forks,项目名称:flynn,代码行数:15,代码来源:controller_test.go
注:本文中的github.com/jackc/pgx.NewConnPool函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论