Skip to content

Commit a8540a7

Browse files
committed
changes for adding statement cache and extract sql_id as part of prepare statement
1 parent 44bebff commit a8540a7

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

worker/cppworker/worker/OCCChild.cpp

+38-14
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static const unsigned int DEFAULT_TRANS_TIMEOUT = 5; //!< Default global transac
122122
static const char *const DEFAULT_MODULE_NAME = "Unknown"; //!< Default module name
123123

124124
//!<Key that occ client sends client machine name, should match OCCClient::send_client_info
125-
static const std::string SERVER_VERSION = "10g";
125+
static const std::string SERVER_VERSION = "19c";
126126
static const std::string SERVER_RELEASE_PREFIX = "Enterprise Edition Release ";
127127
static const std::string SERVER_DB_PREFIX = "Oracle Database ";
128128
static const std::string CLIENT_NAME_PREFIX = "Name: ";
@@ -1033,6 +1033,7 @@ int OCCChild::handle_command(const int _cmd, std::string &_line)
10331033
if (m_sql_rewritten) {
10341034
c->AddData("sqlhash", m_orig_query_hash);
10351035
}
1036+
c->AddData("SQL_ID", sql_id.c_str());
10361037
}
10371038
OCIAttrSet((dvoid *)authp, OCI_HTYPE_SESSION, (dvoid *) const_cast<char*>(m_bind_data.c_str()),
10381039
m_bind_data.length(), OCI_ATTR_CLIENT_IDENTIFIER, errhp);
@@ -1170,6 +1171,7 @@ int OCCChild::handle_command(const int _cmd, std::string &_line)
11701171
CalTransaction cal_trans("FETCH");
11711172
cal_trans.SetName(m_query_hash);
11721173
cal_trans.AddData("HOST", m_dbhost_name);
1174+
cal_trans.AddData("SQL_ID", sql_id.c_str());
11731175

11741176
//fetch a block of rows
11751177
long long fetched_bsize = fetch(_line);
@@ -1651,6 +1653,16 @@ int OCCChild::connect(const std::string& db_username, const std::string& db_pass
16511653
log_oracle_error(rc,"Failed to set failover callback.");
16521654
}
16531655

1656+
// Enable statement cache
1657+
if (enable_cache)
1658+
{
1659+
rc = OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, &max_cache_size, (ub4)0, (ub4)OCI_ATTR_STMTCACHESIZE, errhp);
1660+
if (rc != OCI_SUCCESS)
1661+
{
1662+
log_oracle_error(rc, "Failed to set statement cache.");
1663+
}
1664+
}
1665+
16541666
has_session = true;
16551667
cal_trans.Completed();
16561668

@@ -2377,10 +2389,10 @@ int OCCChild::execute_query(const std::string& query)
23772389
return -1;
23782390
}
23792391

2380-
rc = OCIStmtPrepare(
2381-
stmthp, errhp, (text *) const_cast<char*>(query.c_str()),
2382-
(ub4) query.length(),
2383-
(ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT);
2392+
rc = OCIStmtPrepare2(svchp,
2393+
stmthp, errhp, (text *)const_cast<char *>(query.c_str()),
2394+
(ub4)query.length(),
2395+
(ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT);
23842396
if (rc != OCI_SUCCESS)
23852397
{
23862398
DO_OCI_HANDLE_FREE(stmthp, OCI_HTYPE_STMT, LOG_WARNING);
@@ -3131,13 +3143,13 @@ int OCCChild::prepare(const std::string& _statement, occ::ApiVersion _version)
31313143
cache_misses++;
31323144

31333145
// prepare the new statement
3134-
rc = OCIStmtPrepare(
3135-
entry->stmthp,
3136-
errhp,
3137-
(text *) const_cast<char *> (statement.c_str()),
3138-
(ub4) statement.length(),
3139-
(ub4) OCI_NTV_SYNTAX,
3140-
(ub4) OCI_DEFAULT);
3146+
rc = OCIStmtPrepare2(svchp,
3147+
entry->stmthp,
3148+
errhp,
3149+
(text *)const_cast<char *>(statement.c_str()),
3150+
(ub4)statement.length(),
3151+
(ub4)OCI_NTV_SYNTAX,
3152+
OCI_DEFAULT | OCI_PREP2_GET_SQL_ID);
31413153
if (rc != OCI_SUCCESS)
31423154
{
31433155
sql_error(rc, entry);
@@ -3160,6 +3172,18 @@ int OCCChild::prepare(const std::string& _statement, occ::ApiVersion _version)
31603172
return -1;
31613173
}
31623174

3175+
// Get SQL_ID
3176+
char sql_id_data[32];
3177+
ub4 sql_id_len = sizeof(sql_id_data);
3178+
rc = OCIAttrGet((CONST dvoid *)entry->stmthp, OCI_HTYPE_STMT, sql_id_data, &sql_id_len, OCI_ATTR_SQL_ID, errhp);
3179+
if (rc != OCI_SUCCESS)
3180+
{
3181+
WRITE_LOG_ENTRY(logfile, LOG_INFO, "failed to fetch sql_id from statement.");
3182+
sql_error(rc, entry);
3183+
free_stmt(entry);
3184+
return -1;
3185+
}
3186+
sql_id = std::string(sql_id_data, sql_id_len);
31633187
// // Delineate between SELECT and SELECT ... FOR UPDATE
31643188
// if ((entry->type == SELECT_STMT) &&
31653189
// statement.contains(" FOR UPDATE"))
@@ -5393,7 +5417,7 @@ int OCCChild::get_db_charset(std::string& _charset)
53935417
sys_context('USERENV', 'DB_UNIQUE_NAME') AS db_uname, \
53945418
sys_context('USERENV', 'SID') AS sid \
53955419
FROM nls_database_parameters WHERE parameter = 'NLS_CHARACTERSET'";
5396-
rc = OCIStmtPrepare(stmthp, errhp, (text *) const_cast<char*>(sql), strlen(sql), OCI_NTV_SYNTAX, OCI_DEFAULT);
5420+
rc = OCIStmtPrepare2(svchp, stmthp, errhp, (text *)const_cast<char *>(sql), strlen(sql), OCI_NTV_SYNTAX, OCI_DEFAULT);
53975421
if (rc != OCI_SUCCESS)
53985422
{
53995423
DO_OCI_HANDLE_FREE(stmthp, OCI_HTYPE_STMT, LOG_WARNING);
@@ -5504,7 +5528,7 @@ int OCCChild::execute_query_with_n_binds( const std::string & _sql, const std::v
55045528
return -1;
55055529
}
55065530

5507-
rc = OCIStmtPrepare(m_session_var_stmthp, errhp, (text *) const_cast<char*>(_sql.c_str()), (ub4) _sql.length(), OCI_NTV_SYNTAX, OCI_DEFAULT);
5531+
rc = OCIStmtPrepare2(svchp, m_session_var_stmthp, errhp, (text *)const_cast<char *>(_sql.c_str()), (ub4)_sql.length(), OCI_NTV_SYNTAX, OCI_DEFAULT);
55085532
if (rc != OCI_SUCCESS)
55095533
{
55105534
DO_OCI_HANDLE_FREE(m_session_var_stmthp, OCI_HTYPE_STMT, LOG_WARNING);

worker/cppworker/worker/OCCChild.h

+1
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ class OCCChild : public Worker
278278
std::string m_orig_query_hash;
279279
int bits_to_match; // Sampled Bind Hash logging. Sampling ratio (1:pow(2,bits_to_match)). Default 1 (Sampling ratio 1:2)
280280
unsigned long long int bit_mask; // Compute based on bits_to_match
281+
std::string sql_id; // Database SQL ID for statement
281282

282283
public:
283284
// need to pass in a server socket which is already bound to the correct port

0 commit comments

Comments
 (0)