Hello all,
I have started using soci on a new project and found I needed to call
sqlite3_busy_timeout
on the sqlite3 database handle to avoid the frequent "database is locked"
errors.
Currently the sqlite3 backend for soci does not allow for this, so I have
attached a patch.
The patch is generated from a diff against cvs HEAD. The usage backwards
compatible,
so the old usage still works:
soci::session(soci::sqlite3, "/tmp/foo.db");
With the patch you can specify a timeout, using syntax similar to the mysql
backend:
soci::session(soci::sqlite3, "dbname=/tmp/foo.db timeout=10");
the timeout value is in seconds, which will cause the database handle to
wait up to the
specified time trying to get a database lock when normally it would have
thrown the
"database is locked" error immediately.
Please review the patch and apply if you find it reasonable. If you have
questions or
comments please let me know.
Thanks!
-Cory
? soci-sqlite3-timeout.patch
Index: src/backends/sqlite3/session.cpp
===================================================================
RCS file: /cvsroot/soci/src/backends/sqlite3/session.cpp,v
retrieving revision 1.8
diff -u -7 -p -r1.8 session.cpp
--- src/backends/sqlite3/session.cpp 13 Nov 2007 22:57:56 -0000 1.8
+++ src/backends/sqlite3/session.cpp 4 Aug 2009 15:59:18 -0000
@@ -40,26 +40,55 @@ void hardExec(sqlite_api::sqlite3 *conn,
} // namespace anonymous
sqlite3_session_backend::sqlite3_session_backend(
std::string const & connectString)
{
+ std::string dbname = connectString;
+ int timeout=0;
+ std::stringstream ssconn(connectString);
+ while( !ssconn.eof() && ssconn.str().find('=') >= 0) {
+ std::string key, val;
+ std::getline(ssconn, key, '=');
+ std::getline(ssconn, val, ' ');
+ if( key == "dbname" || key == "db" ) {
+ dbname=val;
+ }
+ else if( key == "timeout" ) {
+ std::istringstream converter(val);
+ converter >> timeout;
+ }
+ }
+
int res;
- res = sqlite3_open(connectString.c_str(), &conn_);
+ res = sqlite3_open(dbname.c_str(), &conn_);
if (res != SQLITE_OK)
{
const char *zErrMsg = sqlite3_errmsg(conn_);
std::ostringstream ss;
ss << "Cannot establish connection to the database. "
<< zErrMsg;
throw soci_error(ss.str());
}
+
+ res = sqlite3_busy_timeout(conn_, timeout * 1000);
+ if (res != SQLITE_OK)
+ {
+ const char *zErrMsg = sqlite3_errmsg(conn_);
+
+ std::ostringstream ss;
+ ss << "Failed to set busy timeout for connection. "
+ << zErrMsg;
+
+ throw soci_error(ss.str());
+ }
+
}
sqlite3_session_backend::~sqlite3_session_backend()
{
clean_up();
}
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
Soci-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/soci-users