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

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