I am working on a simple service using Node.js. It receives the downloaded files, saves them to disk, and writes some metadata to the Oracle table. I use the package db-oraclealong with the connection pool, following this article: http://nodejsdb.org/2011/05/connection-pooling-node-db-with-generic-pool/
However, I noticed that the data I insert is only sent to the Oracle database after the connection pool closes the idle connection by calling its method disconnect().
Is there a way to reset the data before sending the “OK” signal to my client? The way it works now, a failure on my web service or on Oracle itself, can lead to data loss, and the client of my service will not know about it. I really tested this by killing my application process after some downloads and the data was really lost.
Here is a simplified version of the code:
var express = require('express');
var app = module.exports = express.createServer();
app.post('/upload', handleUpload);
app.listen(4001, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
function handleUpload(req, res) {
res.contentType('application/xml');
var buf = '';
req.on('data', function(chunk) { buf += chunk; });
req.on('end', function() {
saveUpload(req, res, buf);
});
}
function saveUpload(req, res, buf) {
if (buf.length == 0)
return sendError(res, 'No data supplied', 422);
var payload = new Buffer(buf, 'base64');
files.save(payload, function(err, savedFile) {
if (err)
return sendError(res, 'Error while saving', 500);
var obj = { ip: req.connection.remoteAddress, location: savedFile.path,
created_at: new Date(), updated_at: new Date() };
var fields = ['IP', 'LOCATION', 'CREATED_AT', 'UPDATED_AT'];
var values = fields.map(function(v) { return obj[v.toLowerCase()] });
pool.acquire(function(err, conn) {
if (err)
return sendError(res, err, 500);
var q = conn.query().insert('FILES', fields, values);
q.execute(function(err, result) {
pool.release(conn);
if (err)
return sendError(res, err, 500);
if (result.affected < 1)
return sendError(res, 'Error saving the record', 500);
res.end('<ok />');
});
});
});
}
function sendError(res, err, code) {
console.log(err);
res.send('<error>' + err + '</error>', code || 500);
}
As a workaround, I tried to implement a fake connection pool and free all purchased connections, but now my application is dying with a message: pure virtual method calledAbort trap: 6
Here's the fake connection pool:
var fakePool = {
acquire: function(callback) {
new oracle.Database(config.database).connect(function(err, server) {
callback(err, this);
});
},
release: function(conn) {
conn.disconnect();
}
};
Just to be clear, I don't care about the fake connection pool, it was just a dirty workaround. I want to clear the data before Oracle before sending "OK" to my client.
Btw Github: https://github.com/mariano/node-db-oracle/issues/38