diff --git a/redis.go b/redis.go index c124bf7..1c5f6c3 100644 --- a/redis.go +++ b/redis.go @@ -40,7 +40,7 @@ func (c *baseClient) conn() (*conn, error) { } func (c *baseClient) initConn(cn *conn) error { - if c.opt.Password == "" || c.opt.DB == 0 { + if c.opt.Password == "" && c.opt.DB == 0 { return nil } diff --git a/redis_test.go b/redis_test.go index 4d3e003..3588aad 100644 --- a/redis_test.go +++ b/redis_test.go @@ -150,6 +150,20 @@ func (t *RedisConnectorTest) TestIdleTimeout(c *C) { } } +func (t *RedisConnectorTest) TestSelectDb(c *C) { + client1 := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + DB: 1, + }) + c.Assert(client1.Set("key", "db1").Err(), IsNil) + + client2 := redis.NewTCPClient(&redis.Options{ + Addr: redisAddr, + DB: 2, + }) + c.Assert(client2.Get("key").Err(), Equals, redis.Nil) +} + //------------------------------------------------------------------------------ type RedisConnPoolTest struct {