CraZySacX / node-jdbc

JDBC Wrapper for node.js
139 stars 106 forks source link

JDBC.close fails when run in module function #13

Closed thmoeller closed 10 years ago

thmoeller commented 10 years ago

Hi,

tried your code and works like a charm standing alone. I put in in a module with callback functionality. This works up to the point, when jdbc.close() is called.

It always fails with:

../jdbc.js:56
        return callback(null);
               ^
TypeError: undefined is not a function
    at JDBCConn.close (.......\jdbc\lib\jdbc.js:56:16))

I tried to put the close somewhere else in the code, but always with the same result.

Any idea??

Thorsten

neurotech commented 10 years ago

Can you post some example code that is generating the error?

thmoeller commented 10 years ago

This is the module code. It executes correct until jdbc.close() is called:

jdbc = require('../../nodejs/node_modules/jdbc/index');
var path = require('path'); // For crossplatform compatibility of path values

var mysqlrequest = function (input, callback) {

            console.log("Start DB request");

            //Create connection string
            var conurl = 'jdbc:mysql://' + input.host + ':' + input.port + '/' + input.database + '?user=' + input.user + '&password=' + input.pass;

            //Create connection configuration
            var config = {
                libpath:  path.resolve('dbdrivers/mysql-connector-java-5.1.30-bin.jar'),
                drivername: 'com.mysql.jdbc.Driver',
                url: conurl
            };

            //Initialize Connection with given config
            jdbc.initialize(config, function(err, res) {
                    if (err) callback(err, null);
            });

            //Create a QueryHandler to return results
            var genericQueryHandler = function(err, results) {

                console.log("Return result");
                if (err) {
                    //console.log(err);
                    callback(err, null);
                } else if (results) {
                    //console.log(results);
                    jdbc.close();
                    callback(null, results);

                }
            };

            //Establish Connection to database and execute defined query
            jdbc.open(function(err, conn) {
                if (conn) {
                    // SELECT statements are called with executeQuery
                    jdbc.executeQuery(input.statement, genericQueryHandler);
                }
                if (err) {
                    callback(err, null);
                }
            }); //End jdbc.open
    jdbc.close();

}; // End mysqlrequest

exports.request = mysqlrequest;

Cheers

Thorsten

thmoeller commented 10 years ago

OK, wrong button, still open

Sorry

CraZySacX commented 10 years ago

jdbc.close requires a callback, similar to the open function.

Try

jdbc.close(function(err) {
  // err is null if the close was successful
});

The docs in README.md are incorrect at the moment,  I'll update those to reflect the close API properly.
neurotech commented 10 years ago

@thmoeller what's with line 1 of your example code? That doesn't seem like the right way to require() the module.

thmoeller commented 10 years ago

Yes, callback in close function did the job.

And yes neurotech, you were right. First line was incorrect, too

Thanks all for your help