简体   繁体   中英

C++ OTL doesn't see external database changes

I have a C++ program that is using OTLv4 to connecto to a database. Everything is working fine. I can both insert data into the database and read data out of the database.

However, if I change data in the database from another program, then this isn't reflected in my C++ program. If I for example remove an entry with MySQL workbench, the C++ program will still see the entry. The data I see is the data as it appeared when the program first logged in to the database.

If I log off and log on each time I do a query then I will get the current value, but that does not seem very efficient. Similarly if I run a query from the C++ program that will modifiy the database then the program will start seeing the current values up until that point.

To me this feels like some sort of over-aggressive caching, but I don't know how that works in OTL, haven't seen any mention of caches other than possibly the stream pooling which I know nothing about.

I'm not doing anything fancy. OTL is compiled with these parameters:

#define OTL_ODBC // Compile OTL 4.0/ODBC
#define OTL_UNICODE // Compile OTL with Unicode 
#define OTL_UNICODE_EXCEPTION_AND_RLOGON
#define OTL_UNICODE_STRING_TYPE std::wstring
// The following #define is required with MyODBC 3.51.11 and higher
#define OTL_ODBC_SELECT_STM_EXECUTE_BEFORE_DESCRIBE

The code looks something like this:

otl_connect::otl_initialize(1); // Multithreading
otl_connect database;
database.rlogon(...);

// Make queries with otl_stream and direct_exec
otl_stream stream(50, "select * from ...", database);
database.direct_exec("insert ... into ...", otl_exception::disabled);

database.logoff();

Is there something I have missed, some configuration I need to do? Turn off some sort of cache? Maybe i really do need to login and logoff each time?

I found out what is wrong:

Q. OTL: When I insert a new row into a table in MySQL, I can't SELECT it, what's going on?

If you're using a prepared SELECT statement in an otl_stream, and keep executing / reusing the statement to get new rows, you need to commit (call otl_connect::commit()) after the fetch sequence is exhausted each time. The commit call will let your MySQL Server know that your current read only transaction is finished, and the server can start a new transaction, which will make newly inserted rows to be visible to your SELECT statement. In other words, you need to commit your SELECT statements in order to able to see new rows.

From http://otl.sourceforge.net/otl3_faq.htm

So the problem was that whenever I make a SELECT statement I have to call otl_connect::commit(); or MySQL won't understand that the statement is finished.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM