不要怂,就是干,撸起袖子干!

Commit af790ca8 by Mick Hansen

refactor(connections): sqlite needs to be singleton for regular queries but need…

…s a differerent connection per transaction
1 parent 8d161a59
......@@ -7,6 +7,8 @@ var AbstractConnectionManager = require('../abstract/connection-manager')
ConnectionManager = function(dialect, sequelize) {
AbstractConnectionManager.call(this, dialect, sequelize);
this.sequelize = sequelize;
this.sequelize.config.port = this.sequelize.config.port || 3306;
try {
this.lib = require(sequelize.config.dialectModulePath || 'mariasql');
} catch (err) {
......
......@@ -8,7 +8,7 @@ ConnectionManager = function(dialect, sequelize) {
this.sequelize = sequelize;
this.config = sequelize.config;
this.dialect = dialect;
this.connection = null;
this.connections = {};
try {
this.lib = require(sequelize.config.dialectModulePath || 'sqlite3').verbose();
......@@ -21,16 +21,18 @@ Utils._.extend(ConnectionManager.prototype, AbstractConnectionManager.prototype)
ConnectionManager.prototype.getConnection = function(options) {
var self = this;
if (self.connection) return Promise.resolve(self.connection);
options = options || {};
options.uuid = options.uuid || 'default';
if (self.connections[options.uuid]) return Promise.resolve(self.connections[options.uuid]);
return new Promise(function (resolve, reject) {
self.connection = new self.lib.Database(self.sequelize.options.storage || ':memory:', function(err) {
self.connections[options.uuid] = new self.lib.Database(self.sequelize.options.storage || ':memory:', function(err) {
if (err) {
if (err.code === 'SQLITE_CANTOPEN') return reject('Failed to find SQL server. Please double check your settings.');
return reject(err);
}
resolve(self.connection);
resolve(self.connections[options.uuid]);
});
}).tap(function (connection) {
if (self.sequelize.options.foreignKeys !== false) {
......@@ -42,7 +44,9 @@ ConnectionManager.prototype.getConnection = function(options) {
};
ConnectionManager.prototype.releaseConnection = function(connection) {
// no-op
if (connection.uuid) {
connection.close();
}
};
module.exports = ConnectionManager;
\ No newline at end of file
......@@ -89,7 +89,9 @@ Transaction.prototype.rollback = function() {
Transaction.prototype.prepareEnvironment = function() {
var self = this;
return this.sequelize.connectionManager.getConnection().then(function (connection) {
return this.sequelize.connectionManager.getConnection({
uuid: self.id
}).then(function (connection) {
self.connection = connection;
self.connection.uuid = self.id;
}).then(function () {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!