node Sequelize使用
查sql server
module.exports.cityOneSequelize = new Sequelize("db_name", "user", "pass", { host: "127.0.0.1", port: 1433 dialect: "mssql", dialectOptions: { requestTimeout: 3600000 }, options: { encrypt: false }, // logging: false, pool: { max: 5 } });
默认查询15s超时,如果有慢查询的话,需要按上面所示设置requestTimeout。
数据库是sql server时,要用到tedious组件,否则会报错,如下
所以,需要我们在package.json中引入mssql驱动的同时引入tedious组件。
实践表明,当sql server服务端版本比较低时(生产中是sql server 2005和sql server 2008),mssql的版本高点无所谓,但是tedious的版本不能太高,5.0.0及以下都是可以的,6.0.0版本就会报SequelizeConnectionError: Failed to connect to xxx - Cannot call write after a stream was destroyed。
查mysql
重点关注连接池配置及超时配置。
module.exports.tcSequelize = new Sequelize("db_name", "user", "pass", { dialect: "mysql", host: "127.0.0.1", port: 3306, timezone: "+08:00", logging: false, operatorsAliases: false, dialectOptions: { requestTimeout: 3000 }, pool: { min: 1, max: 5, idle: 900000, acquire: 5000, evict: 600000 } // * @param {Integer} [options.pool.max=5] Maximum number of connection in pool // * @param {Integer} [options.pool.min=0] Minimum number of connection in pool // * @param {Integer} [options.pool.idle=10000] The maximum time, in milliseconds, that a connection can be idle before being released. Use with combination of evict for proper working, for more details read https://github.com/coopernurse/node-pool/issues/178#issuecomment-327110870 // * @param {Integer} [options.pool.acquire=10000] The maximum time, in milliseconds, that pool will try to get connection before throwing error // * @param {Integer} [options.pool.evict=10000] The time interval, in milliseconds, for evicting stale connections. Set it to 0 to disable this feature. // * @param {Boolean} [options.pool.handleDisconnects=true] Controls if pool should handle connection disconnect automatically without throwing errors // * @param {Function} [options.pool.validate] A function that validates a connection. Called with client. The default function checks that client is an object, and that its state is not disconnected });