sqlite: Use per-thread connections

This commit is contained in:
Albert S. 2023-07-29 10:00:22 +02:00
parent fe533a5076
commit c0049fc7b6
5 ha cambiato i file con 45 aggiunte e 30 eliminazioni

Vedi File

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

Vedi File

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

Vedi File

@ -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
SOFTWARE.
*/
#include <atomic>
#include "sqlite.h"
#include "../logger.h"
#include "pagedaosqlite.h"
#include "revisiondaosqlite.h"
#include "sessiondaosqlite.h"
#include "sqlite_modern_cpp.h"
#include "userdaosqlite.h"
#include "categorydaosqlite.h"
#include "exceptions.h"
#include "permissionsdaosqlite.h"
thread_local sqlite::database *Sqlite::db = nullptr;
std::atomic<int> instances = 0;
Sqlite::Sqlite(std::string path) : Database(path)
{
this->db = std::make_shared<sqlite::database>(path);
*db << "PRAGMA journal_mode=WAL;";
instances++;
if(instances.load() > 1)
{
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
{
return create<RevisionDaoSqlite>();
@ -67,27 +87,20 @@ std::unique_ptr<PermissionsDao> Sqlite::createPermissionsDao() const
void Sqlite::beginTransaction()
{
if(!inTransaction)
{
*db << "begin;";
inTransaction = true;
}
*db << "begin;";
}
void Sqlite::rollbackTransaction()
{
if(inTransaction)
{
*db << "rollback;";
inTransaction = false;
}
*db << "rollback;";
}
void Sqlite::commitTransaction()
{
if(inTransaction)
{
*db << "commit;";
inTransaction = false;
}
*db << "commit;";
}
Sqlite::~Sqlite()
{
delete this->db;
}

Vedi File

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

Vedi File

@ -12,20 +12,20 @@
class SqliteDao
{
protected:
std::shared_ptr<sqlite::database> db;
sqlite::database *db = nullptr;
public:
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