transaction-manager.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var Utils = require('./utils')
  2. var TransactionManager = module.exports = function(sequelize) {
  3. this.sequelize = sequelize
  4. this.connectorManagers = {}
  5. try {
  6. this.ConnectorManager = require("./dialects/" + sequelize.getDialect() + "/connector-manager")
  7. } catch(err) {
  8. throw new Error("The dialect " + sequelize.getDialect() + " is not supported.")
  9. }
  10. }
  11. TransactionManager.prototype.getConnectorManager = function(uuid) {
  12. uuid = uuid || 'default'
  13. if (!this.connectorManagers.hasOwnProperty(uuid)) {
  14. var config = Utils._.extend({ uuid: uuid }, this.sequelize.config)
  15. if (uuid !== 'default') {
  16. config.pool = Utils._.extend(
  17. {},
  18. Utils._.clone(config.pool || {}),
  19. {
  20. minConnections: 1,
  21. maxConnections: 1,
  22. useReplicaton: false
  23. }
  24. )
  25. config.keepDefaultTimezone = true
  26. }
  27. this.connectorManagers[uuid] = new this.ConnectorManager(this.sequelize, config)
  28. }
  29. return this.connectorManagers[uuid]
  30. }
  31. TransactionManager.prototype.releaseConnectionManager = function(uuid) {
  32. this.connectorManagers[uuid].cleanup();
  33. delete this.connectorManagers[uuid]
  34. }
  35. TransactionManager.prototype.query = function(sql, callee, options) {
  36. options = options || {}
  37. options.uuid = 'default'
  38. if (options.transaction) {
  39. options.uuid = options.transaction.id
  40. }
  41. return this.getConnectorManager(options.uuid).query(sql, callee, options)
  42. }