sqlite: Use per-thread connections

This commit is contained in:
Albert S. 2023-07-29 10:00:22 +02:00
父節點 fe533a5076
當前提交 c0049fc7b6
共有 5 個檔案被更改,包括 45 行新增30 行删除

查看文件

@ -13,7 +13,7 @@
#include "permissionsdao.h" #include "permissionsdao.h"
class Database class Database
{ {
private: protected:
std::string connnectionstring; std::string connnectionstring;
public: public:

查看文件

@ -54,7 +54,7 @@ void SessionDaoSqlite::fillSession(int userid, Session &sess)
{ {
if(userid > -1) if(userid > -1)
{ {
UserDaoSqlite userDao{this->db}; UserDaoSqlite userDao{*this->db};
auto u = userDao.find(userid); auto u = userDao.find(userid);
if(u) if(u)
{ {

查看文件

@ -18,23 +18,43 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include <atomic>
#include "sqlite.h" #include "sqlite.h"
#include "../logger.h"
#include "pagedaosqlite.h" #include "pagedaosqlite.h"
#include "revisiondaosqlite.h" #include "revisiondaosqlite.h"
#include "sessiondaosqlite.h" #include "sessiondaosqlite.h"
#include "sqlite_modern_cpp.h"
#include "userdaosqlite.h" #include "userdaosqlite.h"
#include "categorydaosqlite.h" #include "categorydaosqlite.h"
#include "exceptions.h"
#include "permissionsdaosqlite.h" #include "permissionsdaosqlite.h"
thread_local sqlite::database *Sqlite::db = nullptr;
std::atomic<int> instances = 0;
Sqlite::Sqlite(std::string path) : Database(path) Sqlite::Sqlite(std::string path) : Database(path)
{ {
this->db = std::make_shared<sqlite::database>(path); instances++;
if(instances.load() > 1)
*db << "PRAGMA journal_mode=WAL;"; {
std::cerr << "temporal (yeah, right) HACK... only one instance allowed" << std::endl;
abort();
}
} }
std::mutex dbmutex;
sqlite::database &Sqlite::database() const
{
if(Sqlite::db == nullptr)
{
sqlite::sqlite_config config;
config.flags = config.flags | sqlite::OpenFlags::FULLMUTEX;
std::lock_guard<std::mutex> dbguard(dbmutex);
Sqlite::db = new sqlite::database(this->connnectionstring, config);
*Sqlite::db << "PRAGMA journal_mode=WAL;";
*Sqlite::db << "PRAGMA busy_timeout=10000;";
}
return *Sqlite::db;
}
std::unique_ptr<RevisionDao> Sqlite::createRevisionDao() const std::unique_ptr<RevisionDao> Sqlite::createRevisionDao() const
{ {
return create<RevisionDaoSqlite>(); return create<RevisionDaoSqlite>();
@ -67,27 +87,20 @@ std::unique_ptr<PermissionsDao> Sqlite::createPermissionsDao() const
void Sqlite::beginTransaction() void Sqlite::beginTransaction()
{ {
if(!inTransaction)
{
*db << "begin;"; *db << "begin;";
inTransaction = true;
}
} }
void Sqlite::rollbackTransaction() void Sqlite::rollbackTransaction()
{ {
if(inTransaction)
{
*db << "rollback;"; *db << "rollback;";
inTransaction = false;
}
} }
void Sqlite::commitTransaction() void Sqlite::commitTransaction()
{ {
if(inTransaction)
{
*db << "commit;"; *db << "commit;";
inTransaction = false; }
}
Sqlite::~Sqlite()
{
delete this->db;
} }

查看文件

@ -8,14 +8,15 @@
class Sqlite : public Database class Sqlite : public Database
{ {
private: private:
bool inTransaction = false; static thread_local sqlite::database *db;
std::shared_ptr<sqlite::database> db;
template <class T> std::unique_ptr<T> create() const template <class T> std::unique_ptr<T> create() const
{ {
return std::make_unique<T>(db); return std::make_unique<T>(database());
} }
sqlite::database &database() const;
public: public:
Sqlite(std::string path); Sqlite(std::string path);
std::unique_ptr<PageDao> createPageDao() const; std::unique_ptr<PageDao> createPageDao() const;
@ -27,6 +28,7 @@ class Sqlite : public Database
void beginTransaction(); void beginTransaction();
void commitTransaction(); void commitTransaction();
void rollbackTransaction(); void rollbackTransaction();
virtual ~Sqlite();
}; };
#endif // SQLITE_H #endif // SQLITE_H

查看文件

@ -12,20 +12,20 @@
class SqliteDao class SqliteDao
{ {
protected: protected:
std::shared_ptr<sqlite::database> db; sqlite::database *db = nullptr;
public: public:
SqliteDao() SqliteDao()
{ {
} }
SqliteDao(std::shared_ptr<sqlite::database> db) SqliteDao(sqlite::database &db)
{ {
this->db = db; this->db = &db;
} }
void setDb(std::shared_ptr<sqlite::database> db) void setDb(sqlite::database &db)
{ {
this->db = db; this->db = &db;
} }
inline void throwFrom(const sqlite::sqlite_exception &e) const inline void throwFrom(const sqlite::sqlite_exception &e) const