diff --git a/client/session.go b/client/session.go index 1474991..133be69 100644 --- a/client/session.go +++ b/client/session.go @@ -1147,12 +1147,12 @@ func newSessionWithSpecifiedSqlDialect(config *Config) Session { return Session{config: config} } -func NewClusterSession(clusterConfig *ClusterConfig) Session { +func NewClusterSession(clusterConfig *ClusterConfig) (Session, error) { clusterConfig.sqlDialect = TreeSqlDialect return newClusterSessionWithSqlDialect(clusterConfig) } -func newClusterSessionWithSqlDialect(clusterConfig *ClusterConfig) Session { +func newClusterSessionWithSqlDialect(clusterConfig *ClusterConfig) (Session, error) { session := Session{} node := endPoint{} for i := 0; i < len(clusterConfig.NodeUrls); i++ { @@ -1180,9 +1180,9 @@ func newClusterSessionWithSqlDialect(clusterConfig *ClusterConfig) Session { } } if !session.trans.IsOpen() { - log.Fatal("No Server Can Connect") + return session, fmt.Errorf("no server can connect") } - return session + return session, nil } func (s *Session) initClusterConn(node endPoint) error { diff --git a/client/sessionpool.go b/client/sessionpool.go index 556eb8d..2670196 100644 --- a/client/sessionpool.go +++ b/client/sessionpool.go @@ -114,8 +114,11 @@ func (spool *SessionPool) getTableSession() (ITableSession, error) { func (spool *SessionPool) ConstructSession(config *PoolConfig) (session Session, err error) { if len(config.NodeUrls) > 0 { - session = newClusterSessionWithSqlDialect(getClusterSessionConfig(config)) - if err := session.OpenCluster(spool.enableCompression); err != nil { + session, err = newClusterSessionWithSqlDialect(getClusterSessionConfig(config)) + if err != nil { + return session, err + } + if err = session.OpenCluster(spool.enableCompression); err != nil { log.Print(err) return session, err } diff --git a/client/tablesession.go b/client/tablesession.go index f219217..9beb60d 100644 --- a/client/tablesession.go +++ b/client/tablesession.go @@ -109,8 +109,11 @@ func NewTableSession(config *Config, enableRPCCompression bool, connectionTimeou // - An error if there is an issue during session initialization. func NewClusterTableSession(clusterConfig *ClusterConfig, enableRPCCompression bool) (ITableSession, error) { clusterConfig.sqlDialect = TableSqlDialect - session := newClusterSessionWithSqlDialect(clusterConfig) - if err := session.OpenCluster(enableRPCCompression); err != nil { + session, err := newClusterSessionWithSqlDialect(clusterConfig) + if err != nil { + return nil, err + } + if err = session.OpenCluster(enableRPCCompression); err != nil { return nil, err } return &TableSession{session: session}, nil diff --git a/example/session_example.go b/example/session_example.go index 4777486..32026c1 100644 --- a/example/session_example.go +++ b/example/session_example.go @@ -152,8 +152,11 @@ func connectCluster() { UserName: "root", Password: "root", } - session = client.NewClusterSession(config) - if err := session.OpenCluster(false); err != nil { + session, err := client.NewClusterSession(config) + if err != nil { + log.Fatal(err) + } + if err = session.OpenCluster(false); err != nil { log.Fatal(err) } } diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index fd50245..e4b91ea 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -47,8 +47,10 @@ func (s *e2eTestSuite) SetupSuite() { UserName: "root", Password: "root", } - s.session = client.NewClusterSession(&clusterConfig) - err := s.session.Open(false, 0) + session, err := client.NewClusterSession(&clusterConfig) + s.Require().NoError(err) + s.session = session + err = s.session.Open(false, 0) s.Require().NoError(err) }