Kea 1.9.11
mysql_connection.h
Go to the documentation of this file.
1// Copyright (C) 2012-2021 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef MYSQL_CONNECTION_H
8#define MYSQL_CONNECTION_H
9
10#include <asiolink/io_service.h>
13#include <database/db_log.h>
15#include <mysql/mysql_binding.h>
17#include <boost/scoped_ptr.hpp>
18#include <mysql.h>
19#include <mysqld_error.h>
20#include <errmsg.h>
21#include <functional>
22#include <vector>
23#include <stdint.h>
24
25namespace isc {
26namespace db {
27
28
42
44public:
45
56 MySqlFreeResult(MYSQL_STMT* statement) : statement_(statement)
57 {}
58
63 (void) mysql_stmt_free_result(statement_);
64 }
65
66private:
67 MYSQL_STMT* statement_;
68};
69
75 uint32_t index;
76 const char* text;
77};
78
88template <typename Fun, typename... Args>
89int retryOnDeadlock(Fun& fun, Args... args) {
90 int status;
91 for (unsigned count = 0; count < 5; ++count) {
92 status = fun(args...);
93 if (status != ER_LOCK_DEADLOCK) {
94 break;
95 }
96 }
97 return (status);
98}
99
106inline int MysqlExecuteStatement(MYSQL_STMT* stmt) {
107 return (retryOnDeadlock(mysql_stmt_execute, stmt));
108}
109
117inline int MysqlQuery(MYSQL* mysql, const char* stmt) {
118 return (retryOnDeadlock(mysql_query, mysql, stmt));
119}
120
132class MySqlHolder : public boost::noncopyable {
133public:
134
140 MySqlHolder() : mysql_(mysql_init(NULL)) {
141 if (mysql_ == NULL) {
142 isc_throw(db::DbOpenError, "unable to initialize MySQL");
143 }
144 }
145
150 if (mysql_ != NULL) {
151 mysql_close(mysql_);
152 }
153 }
154
159 operator MYSQL*() const {
160 return (mysql_);
161 }
162
163private:
164 static bool atexit_;
165
166 MYSQL* mysql_;
167};
168
170class MySqlConnection;
171
192class MySqlTransaction : public boost::noncopyable {
193public:
194
204
209
211 void commit();
212
213private:
214
216 MySqlConnection& conn_;
217
222 bool committed_;
223};
224
225
234public:
235
237 typedef std::function<void(MySqlBindingCollection&)> ConsumeResultFun;
238
246 MySqlConnection(const ParameterMap& parameters,
248 DbCallback callback = DbCallback())
249 : DatabaseConnection(parameters, callback),
250 io_service_accessor_(io_accessor), io_service_(),
252 }
253
255 virtual ~MySqlConnection();
256
267 static std::pair<uint32_t, uint32_t>
268 getVersion(const ParameterMap& parameters);
269
283 void prepareStatement(uint32_t index, const char* text);
284
299 void prepareStatements(const TaggedStatement* start_statement,
300 const TaggedStatement* end_statement);
301
303 void clearStatements();
304
312 void openDatabase();
313
321
327 static
328 void convertToDatabaseTime(const time_t input_time, MYSQL_TIME& output_time);
329
349 static
350 void convertToDatabaseTime(const time_t cltt, const uint32_t valid_lifetime,
351 MYSQL_TIME& expire);
352
370 static
371 void convertFromDatabaseTime(const MYSQL_TIME& expire,
372 uint32_t valid_lifetime, time_t& cltt);
374
388 void startTransaction();
389
393 bool isTransactionStarted() const;
394
422 template<typename StatementIndex>
423 void selectQuery(const StatementIndex& index,
424 const MySqlBindingCollection& in_bindings,
425 MySqlBindingCollection& out_bindings,
426 ConsumeResultFun process_result) {
428 // Extract native input bindings.
429 std::vector<MYSQL_BIND> in_bind_vec;
430 for (MySqlBindingPtr in_binding : in_bindings) {
431 in_bind_vec.push_back(in_binding->getMySqlBinding());
432 }
433
434 int status = 0;
435 if (!in_bind_vec.empty()) {
436 // Bind parameters to the prepared statement.
437 status = mysql_stmt_bind_param(statements_[index],
438 in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
439 checkError(status, index, "unable to bind parameters for select");
440 }
441
442 // Bind variables that will receive results as well.
443 std::vector<MYSQL_BIND> out_bind_vec;
444 for (MySqlBindingPtr out_binding : out_bindings) {
445 out_bind_vec.push_back(out_binding->getMySqlBinding());
446 }
447 if (!out_bind_vec.empty()) {
448 status = mysql_stmt_bind_result(statements_[index], &out_bind_vec[0]);
449 checkError(status, index, "unable to bind result parameters for select");
450 }
451
452 // Execute query.
453 status = MysqlExecuteStatement(statements_[index]);
454 checkError(status, index, "unable to execute");
455
456 status = mysql_stmt_store_result(statements_[index]);
457 checkError(status, index, "unable to set up for storing all results");
458
459 // Fetch results.
460 MySqlFreeResult fetch_release(statements_[index]);
461 while ((status = mysql_stmt_fetch(statements_[index])) ==
463 try {
464 // For each returned row call user function which should
465 // consume the row and copy the data to a safe place.
466 process_result(out_bindings);
467
468 } catch (const std::exception& ex) {
469 // Rethrow the exception with a bit more data.
470 isc_throw(BadValue, ex.what() << ". Statement is <" <<
471 text_statements_[index] << ">");
472 }
473 }
474
475 // How did the fetch end?
476 // If mysql_stmt_fetch return value is equal to 1 an error occurred.
477 if (status == MLM_MYSQL_FETCH_FAILURE) {
478 // Error - unable to fetch results
479 checkError(status, index, "unable to fetch results");
480
481 } else if (status == MYSQL_DATA_TRUNCATED) {
482 // Data truncated - throw an exception indicating what was at fault
484 << " returned truncated data");
485 }
486 }
487
502 template<typename StatementIndex>
503 void insertQuery(const StatementIndex& index,
504 const MySqlBindingCollection& in_bindings) {
506 std::vector<MYSQL_BIND> in_bind_vec;
507 for (MySqlBindingPtr in_binding : in_bindings) {
508 in_bind_vec.push_back(in_binding->getMySqlBinding());
509 }
510
511 // Bind the parameters to the statement
512 int status = mysql_stmt_bind_param(statements_[index],
513 in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
514 checkError(status, index, "unable to bind parameters");
515
516 // Execute the statement
517 status = MysqlExecuteStatement(statements_[index]);
518
519 if (status != 0) {
520 // Failure: check for the special case of duplicate entry.
521 if (mysql_errno(mysql_) == ER_DUP_ENTRY) {
522 isc_throw(DuplicateEntry, "Database duplicate entry error");
523 }
524 // Failure: check for the special case of WHERE returning NULL.
525 if (mysql_errno(mysql_) == ER_BAD_NULL_ERROR) {
526 isc_throw(NullKeyError, "Database bad NULL error");
527 }
528 checkError(status, index, "unable to execute");
529 }
530 }
531
546 template<typename StatementIndex>
547 uint64_t updateDeleteQuery(const StatementIndex& index,
548 const MySqlBindingCollection& in_bindings) {
550 std::vector<MYSQL_BIND> in_bind_vec;
551 for (MySqlBindingPtr in_binding : in_bindings) {
552 in_bind_vec.push_back(in_binding->getMySqlBinding());
553 }
554
555 // Bind the parameters to the statement
556 int status = mysql_stmt_bind_param(statements_[index],
557 in_bind_vec.empty() ? 0 : &in_bind_vec[0]);
558 checkError(status, index, "unable to bind parameters");
559
560 // Execute the statement
561 status = MysqlExecuteStatement(statements_[index]);
562
563 if (status != 0) {
564 // Failure: check for the special case of duplicate entry.
565 if ((mysql_errno(mysql_) == ER_DUP_ENTRY)
566#ifdef ER_FOREIGN_DUPLICATE_KEY
567 || (mysql_errno(mysql_) == ER_FOREIGN_DUPLICATE_KEY)
568#endif
569#ifdef ER_FOREIGN_DUPLICATE_KEY_WITH_CHILD_INFO
570 || (mysql_errno(mysql_) == ER_FOREIGN_DUPLICATE_KEY_WITH_CHILD_INFO)
571#endif
572#ifdef ER_FOREIGN_DUPLICATE_KEY_WITHOUT_CHILD_INFO
573 || (mysql_errno(mysql_) == ER_FOREIGN_DUPLICATE_KEY_WITHOUT_CHILD_INFO)
574#endif
575 ) {
576 isc_throw(DuplicateEntry, "Database duplicate entry error");
577 }
578 checkError(status, index, "unable to execute");
579 }
580
581 // Let's return how many rows were affected.
582 return (static_cast<uint64_t>(mysql_stmt_affected_rows(statements_[index])));
583 }
584
595 void commit();
596
607 void rollback();
608
637 template<typename StatementIndex>
638 void checkError(const int status, const StatementIndex& index,
639 const char* what) {
640 if (status != 0) {
641 switch(mysql_errno(mysql_)) {
642 // These are the ones we consider fatal. Remember this method is
643 // used to check errors of API calls made subsequent to successfully
644 // connecting. Errors occurring while attempting to connect are
645 // checked in the connection code. An alternative would be to call
646 // mysql_ping() - assuming autoreconnect is off. If that fails
647 // then we know connection is toast.
648 case CR_SERVER_GONE_ERROR:
649 case CR_SERVER_LOST:
650 case CR_OUT_OF_MEMORY:
651 case CR_CONNECTION_ERROR: {
653 .arg(what)
654 .arg(text_statements_[static_cast<int>(index)])
655 .arg(mysql_error(mysql_))
656 .arg(mysql_errno(mysql_));
657
658 // Mark this connection as no longer usable.
659 markUnusable();
660
661 // Start the connection recovery.
663
664 // We still need to throw so caller can error out of the current
665 // processing.
667 "fatal database error or connectivity lost");
668 }
669 default:
670 // Connection is ok, so it must be an SQL error
671 isc_throw(db::DbOperationError, what << " for <"
672 << text_statements_[static_cast<int>(index)]
673 << ">, reason: "
674 << mysql_error(mysql_) << " (error code "
675 << mysql_errno(mysql_) << ")");
676 }
677 }
678 }
679
686 if (callback_) {
688 io_service_ = (*io_service_accessor_)();
689 io_service_accessor_.reset();
690 }
691
692 if (io_service_) {
693 io_service_->post(std::bind(callback_, reconnectCtl()));
694 }
695 }
696 }
697
702 std::vector<MYSQL_STMT*> statements_;
703
708 std::vector<std::string> text_statements_;
709
715
724
727
735};
736
737} // end of isc::db namespace
738} // end of isc namespace
739
740#endif // MYSQL_CONNECTION_H
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Data is truncated.
Definition: db_exceptions.h:35
Common database connection class.
void markUnusable()
Sets the unusable flag to true.
ReconnectCtlPtr reconnectCtl()
The reconnect settings.
void checkUnusable()
Throws an exception if the connection is not usable.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
DbCallback callback_
The callback used to recover the connection.
Exception thrown when a specific connection has been rendered unusable either through loss of connect...
Exception thrown on failure to open database.
Exception thrown on failure to execute a database function.
Database duplicate entry error.
Definition: db_exceptions.h:42
Common MySQL Connector Pool.
isc::asiolink::IOServicePtr io_service_
IOService object, used for all ASIO operations.
MySqlHolder mysql_
MySQL connection handle.
void prepareStatement(uint32_t index, const char *text)
Prepare Single Statement.
std::vector< MYSQL_STMT * > statements_
Prepared statements.
bool isTransactionStarted() const
Checks if there is a transaction in progress.
std::vector< std::string > text_statements_
Raw text of statements.
void insertQuery(const StatementIndex &index, const MySqlBindingCollection &in_bindings)
Executes INSERT prepared statement.
static void convertToDatabaseTime(const time_t input_time, MYSQL_TIME &output_time)
Convert time_t value to database time.
IOServiceAccessorPtr io_service_accessor_
Accessor function which returns the IOService that can be used to recover the connection.
static void convertFromDatabaseTime(const MYSQL_TIME &expire, uint32_t valid_lifetime, time_t &cltt)
Convert Database Time to Lease Times.
void commit()
Commits current transaction.
MySqlConnection(const ParameterMap &parameters, IOServiceAccessorPtr io_accessor=IOServiceAccessorPtr(), DbCallback callback=DbCallback())
Constructor.
void startRecoverDbConnection()
The recover connection.
uint64_t updateDeleteQuery(const StatementIndex &index, const MySqlBindingCollection &in_bindings)
Executes UPDATE or DELETE prepared statement and returns the number of affected rows.
void openDatabase()
Open Database.
void prepareStatements(const TaggedStatement *start_statement, const TaggedStatement *end_statement)
Prepare statements.
static std::pair< uint32_t, uint32_t > getVersion(const ParameterMap &parameters)
Get the schema version.
int transaction_ref_count_
Reference counter for transactions.
void startTransaction()
Starts new transaction.
virtual ~MySqlConnection()
Destructor.
std::function< void(MySqlBindingCollection &)> ConsumeResultFun
Function invoked to process fetched row.
void checkError(const int status, const StatementIndex &index, const char *what)
Check Error and Throw Exception.
void selectQuery(const StatementIndex &index, const MySqlBindingCollection &in_bindings, MySqlBindingCollection &out_bindings, ConsumeResultFun process_result)
Executes SELECT query using prepared statement.
void clearStatements()
Clears prepared statements and text statements.
void rollback()
Rollbacks current transaction.
Fetch and Release MySQL Results.
MySqlFreeResult(MYSQL_STMT *statement)
Constructor.
MySQL Handle Holder.
MySqlHolder()
Constructor.
RAII object representing MySQL transaction.
void commit()
Commits transaction.
MySqlTransaction(MySqlConnection &conn)
Constructor.
Key is NULL but was specified NOT NULL.
Definition: db_exceptions.h:49
We want to reuse the database backend connection and exchange code for other uses,...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::function< bool(ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
boost::shared_ptr< MySqlBinding > MySqlBindingPtr
Shared pointer to the Binding class.
@ MYSQL_FATAL_ERROR
Definition: db_log.h:60
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
const int MLM_MYSQL_FETCH_FAILURE
MySQL fetch failure code.
int MysqlQuery(MYSQL *mysql, const char *stmt)
Execute a literal statement.
std::vector< MySqlBindingPtr > MySqlBindingCollection
Collection of bindings.
const int MLM_MYSQL_FETCH_SUCCESS
check for bool size
int retryOnDeadlock(Fun &fun, Args... args)
Retry on InnoDB deadlock.
int MysqlExecuteStatement(MYSQL_STMT *stmt)
Execute a prepared statement.
Defines the logger used by the top-level component of kea-lfc.
DB_LOG & arg(T first, Args... args)
Pass parameters to replace logger placeholders.
Definition: db_log.h:144
MySQL Selection Statements.