通过JDBC将Virtuoso连接到NodeJS

时间:2017-09-29 15:46:20

标签: node.js jdbc virtuoso

我有以下代码通过Node和JDBC连接到Virtuoso 7服务器(通过npm上的jdbc包):

    const checkDatabaseConnection = function(callback)
{
    const JDBC = require('jdbc');
    const jinst = require('jdbc/lib/jinst');

    if (!jinst.isJvmCreated()) {
        jinst.addOption("-Xrs");
        jinst.setupClasspath([
            Pathfinder.absPathInApp("conf/virtuoso-jdbc/virtjdbc4.jar")
        ]);
    }

    const config = {
        // Required
        url : "jdbc:virtuoso://192.168.56.249:1111",
        drivername: 'virtuoso.jdbc4.Driver',
        minpoolsize: 1,
        maxpoolsize: 100,
        maxidle : 5000,
        username : "dba",
        password : "dba",

        properties: {}
    };

    const jdbcdb = new JDBC(config);

    jdbcdb.initialize(function(err) {
        if (err) {
            console.log(err);
        }
    });
};

立即返回:

{ Error: Error running static method
virtuoso.jdbc4.VirtuosoException: Virtuoso Communications Link Failure (timeout) : Connection to the server lost
    at virtuoso.jdbc4.VirtuosoFuture.nextResult(VirtuosoFuture.java:63)
    at virtuoso.jdbc4.VirtuosoConnection.connect(VirtuosoConnection.java:328)
    at virtuoso.jdbc4.VirtuosoConnection.connect(VirtuosoConnection.java:224)
    at virtuoso.jdbc4.VirtuosoConnection.<init>(VirtuosoConnection.java:169)
    at virtuoso.jdbc4.Driver.connect(Driver.java:58)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:208)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
 cause: nodeJava_virtuoso_jdbc4_VirtuosoException {} }

运行Virtuoso JDBC驱动程序4.2,Virtuoso 7。

我在相同的IP和端口上telnet到服务器,服务器正在侦听。可能是什么问题?

1 个答案:

答案 0 :(得分:0)

1)检查引用node-jdbc config属性username是错误的,它必须是我user

2)下一个示例适合我。

var JDBC = require('jdbc');
var jinst = require('jdbc/lib/jinst');
var asyncjs = require('async');

if (!jinst.isJvmCreated()) {
  jinst.addOption("-Xrs");
  jinst.setupClasspath(['./drivers/virtjdbc4.jar']);
}

const config = {
     // Required
     url : "jdbc:virtuoso://192.168.10.101:1111",
     drivername: 'virtuoso.jdbc4.Driver',
     minpoolsize: 1,
     maxpoolsize: 100,
     maxidle : 5000,
     user : "dba",
     password : "dba",

     properties: {}
 };

 const jdbcdb = new JDBC(config);

 jdbcdb.initialize(function(err) {
     if (err) {
         console.log(err);
     }
 });

jdbcdb.reserve(function(err, connObj) {
  if (connObj) {
    console.log("Using connection: " + connObj.uuid);
    var conn = connObj.conn;

    asyncjs.series([
      function(callback) {
        console.log("call create Statement");

        conn.createStatement(function(err, statement) {
          if (err) {
            callback(err);
          } else {
            console.log("execute query");

            statement.executeQuery("SELECT * FROM Demo.demo.Artist",
                                   function(err, resultset) {
              if (err) {
                callback(err)
              } else {
                resultset.toObjArray(function(err, results) {
                  console.log('res='+JSON.stringify(results));
                  if (results.length > 0) {
                    console.log("Name: " + results[0].Name);
                  }
                  callback(null, resultset);
                });
              }
            });
          }
        });
      }
    ], function(err, results) {
      // Release the connection back to the pool.
      if (err) 
        console.log("Error:"+err);

      jdbcdb.release(connObj, function(err) {
        if (err) {
          console.log(err.message);
        }
      });
    });
  }
});

输出日志:

Using connection: f5c60576-1da5-4129-af0a-f25d10160f21
call create Statement
execute query
res=[{"ArtistID":1,"CountryCode":"nl","Name":"Rembrandt Harmenszoon van Rijn"}]
Name: Rembrandt Harmenszoon van Rijn