sqlite: Use per-thread connections
这个提交包含在:
@@ -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;
|
||||
}
|
||||
|
在新工单中引用
屏蔽一个用户