A Note About Connections
So far, all our inserter examples have specified a connection to the execute() method. While not strictly necessary, this is probably a good idea. Consider the following example:
RWDBInserter insert = someTable.inserter();
insert << x1 << y1;
insert.execute();
insert << x2 << y2;
insert.execute();
insert << x3 << y3;
insert.execute();
// etc.
In this example, each call to
execute() causes another database connection to be opened. When
execute() is called without a connection, a connection is automatically requested from the connection pool inside the
RWDBDatabase. For some C++ compilers, the temporary
RWDBResult instances produced by the
execute() calls are destroyed only when the current code block is exited, so their associated connections are busy and unavailable until the end of the block. The
RWDBInserter may also keep a reference to the last connection on which it was executed, marking that connection busy as well. In short, each call to
execute() causes a new connection to be created because none of the previously created connections are available.
A better idea is to use one connection explicitly, or increase the size of the connection pool by using RWDBDatabase::defaultConnections() method.
RWDBInserter insert = someTable.inserter();
insert << x1 << y1;
insert.execute(myConnection);
insert << x2 << y2;
insert.execute(myConnection);
insert << x3 << y3;
insert.execute(myConnection);
// etc.
In this code, the same connection is reused for all the insertions. You avoid opening unneeded connections, and reduce the risk of an error if there aren’t enough connections available. You may also improve performance, since you reduce the overhead associated with opening new connections.
NOTE: Explicit connections may improve performance. Increasing the size of connection pool may also improve performance.
RWDBUpdater and
RWDBDeleter handle connections the same way as
RWDBInserter and
RWDBBulkInserter. See
Chapter 8, Bulk Reading and Writing, for more information on
RWDBBulkInserter.
NOTE: By using explicit connections, you avoid opening unneeded connections and reduce the risk of an error if there aren't enough connections available.