MCPcopy Create free account
hub / github.com/Tencent/phxsql / Connect

Method Connect

phxsqlclient/phxsqlclientbase.cpp:150–187  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

148}
149
150int PhxSQLClientBase::Connect() {
151 if (!m_poMySQLProp->bConnected) {
152 int iConnTimeout = GetConfig()->GetConnTimeout();
153 int iReadTimeout = GetConfig()->GetReadTimeout();
154 SetOption(MYSQL_OPT_CONNECT_TIMEOUT, (const char *) &iConnTimeout);
155 SetOption(MYSQL_OPT_READ_TIMEOUT, (const char *) &iReadTimeout);
156
157 char *dbname = NULL;
158 if (m_poMySQLProp->pcDB[0] != '\0') {
159 dbname = m_poMySQLProp->pcDB;
160 }
161
162 MySQLEndPoint_t tEndPoint, tPreviousEndPoint;
163 tEndPoint.m_iPort = 0;
164 tPreviousEndPoint.m_iPort = 0;
165 for (int32_t i = 0; i < 3; ++i) {
166 int ret = m_poDisasterStrategy->GetEndPoint(m_poConfig->GetEndPoints(), tPreviousEndPoint, tEndPoint);
167 tPreviousEndPoint = tEndPoint;
168 if (ret != phxsql::OK || tEndPoint.m_sMySQLIP == "" || tEndPoint.m_iPort == 0) {
169 //do log and monitor here
170 return phxsql::GET_END_POINT_FAIL;
171 }
172
173 if (mysql_real_connect(&m_poMySQLProp->connection, tEndPoint.m_sMySQLIP.c_str(), m_poMySQLProp->pcUser,
174 m_poMySQLProp->pcPass, dbname, tEndPoint.m_iPort, NULL, m_poMySQLProp->iClientFlag)
175 == NULL) {
176 m_poDisasterStrategy->ReportEndPoint(tEndPoint, -1, 0);
177 //do log and monitor here
178 continue;
179 }
180 m_poMySQLProp->tEndPoint = tEndPoint;
181 m_poMySQLProp->bConnected = true;
182 break;
183 }
184 }
185 //do log and monitor here
186 return phxsql::OK;
187}
188
189int PhxSQLClientBase::SetOption(mysql_option option, const char *arg) {
190 return mysql_options(&m_poMySQLProp->connection, option, arg);

Callers

nothing calls this directly

Calls 4

GetConnTimeoutMethod · 0.80
GetReadTimeoutMethod · 0.80
GetEndPointMethod · 0.45
ReportEndPointMethod · 0.45

Tested by

no test coverage detected