So, I have a node.js project that I am working on for a class. One of the features our group is trying to build, requires calling a Java class method that connects to a MySQL database, does some computation and returns the result to the node server. Here is what we have so far:
Using the node-java library, we are adding the required jars to classpath:
var java = require('java');
var path = require('path');
java.classpath.push(path.join(__dirname, "../java/mysql-connector-java.5.1.38-bin.jar"));
java.classpath.push(path.join(__dirname, "../java/BayesianCurve.jar"));
java.classpath.push(path.join(__dirname, "../java/commons-math3-3.6.jar"));
Then, the class is instantiated and a method is called:
var test = java.newInstanceSync('BayesianCurve');
test.getPrediction(param, function(err, data){
if(err)
{
console.log(err);
return;
}
else
res.send(JSON.stringify(data));
});
So far, so good. The Java class method then has to create a connection to a MySQL database and this is where the problems start:
Class.forName("com.mysql.jdbc.Driver");
//System.setProperty("jdbc.drivers","com.mysql.jdbc.Driver");
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
System.out.println("Here");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/schema","root","root");
The java code, standalone, is able to connect to the database and execute queries. However, when used through the node-java module, it throws:
ClassNotFoundException: com.mysql.jdbc.Driver
One reason we thought about was that this library is linked dynamically. Somehow, the java environment in node.js is not able to figure this out. However, we are not really sure how to resolve this. Any help in this regard is appreciated.
Related
I've a H2 database file which I need to replicate using SymmetricDS.
I can access the database from web console in embedded mode using this url:
jdbc:h2:file:E:/Folder/database;AUTO_SERVER=TRUE;IFEXISTS=TRUE;
But using the same url in node properties file of symmtericds throws following error:
ERROR [server-000] [AbstractSymmetricEngine] [symmetric-engine-startup-0] Could not get a connection to the database: Cannot create PoolableConnectionFactory (IO Exception: "E:/Folder/database outside D:/symmetric-server-3.7.26/tmp/h2" [90028-182]). Waiting for 10 seconds before trying to connect to the database again.
Though if the following url is used in the node properties file of symmetricds, everything works fine but the database is created in symmetric-server-3.7.26/tmp/h2 directory.
jdbc:h2:file:database;AUTO_SERVER=TRUE;
I've not had any luck in solving this issue in past couple of days.
Any kind of help will be greatly appreciated.
Going through H2 documentation I found out that the this behaviour is due to -baseDir option: http://www.h2database.com/html/advanced.html#remote_access .
But how is this option being set automatically and how to disable it..?
Doing some extensive research on the issue, root cause turns out to be the static block of AbstractCommandLauncher class of SymmetricDS:
static {
String symHome = System.getenv("SYM_HOME");
if (symHome == null) {
symHome = ".";
}
System.setProperty("log4j.sym.home", symHome);
if (isBlank(System.getProperty("h2.baseDir"))) {
System.setProperty("h2.baseDir", symHome + "/tmp/h2");
}
DEFAULT_SERVER_PROPERTIES = System.getProperty(SystemConstants.SYSPROP_SERVER_PROPERTIES_PATH, symHome + "/conf/symmetric-server.properties");
log = LoggerFactory.getLogger(AbstractCommandLauncher.class);
initFromServerProperties();
}
And as it turns out, source code of SymmetricDS will have to be modified to resolve it.
This behaviour can not be disabled altogether but overriding it is possible by setting -Dh2.baseDir while running SymmetricDS. The original answer is here
Ok, I am reposting this question because it really drives me crazy.
I have enterprise.wsdl downloaded from salesforce and generated to some jars.
I build path those jars to my Android project in Eclipse.
Here is my code:
ConnectorConfig config = new ConnectorConfig();
config.setAuthEndpoint(authEndPoint);
config.setUsername(userID);
config.setPassword(password + securityToken);
config.setCompression(true);
con = new EnterpriseConnection(config);
con.setSessionHeader(UserPreference.getSessionID(mContext));
String sql = "SELECT something FROM myNameSpace__myCustomObject__c";
con.query(sql);
but it returns me this error:
[InvalidSObjectFault [ApiQueryFault [ApiFault
exceptionCode='INVALID_TYPE' exceptionMessage='sObject type 'abc__c'
is not supported.'] row='-1' column='-1' ]]
I am pretty sure that my userID has been assigned with profile that has read, edit access to that custom object.
My code also can query standard object.
Anyone can advise me what could be wrong?
From what I know there are three reasons it may give this error.
1. User permission which you said is setup correctly.
2. Do you have the custom object deployed to the org where you are trying to establish the connection?
3. Check the enterprise WSDL if it contains the custom object name which you are trying to query.
Hope it helps.
I'm trying to connect to Titan backed with Cassandra (installed with the Rexster Titan-Server package) via Titan-Node.
I get the error...
java.lang.IllegalArgumentException: Could not find implementation
class: "cassandra"
... when I run the following code....
var Titan = require('titan-node');
var gremlin = new Titan.Gremlin({ loglevel: 'OFF' });
var TitanFactory = gremlin.java.import('com.thinkaurelius.titan.core.TitanFactory');
var graph = TitanFactory.openSync('titan.config');
Config:
storage.directory = "/tmp/titan"
storage.backend = "cassandra"
storage.hostname = "127.0.0.1"
storage.port = 9160
Any ideas?
I seem to have the required .jar within my Titan installation folder and also in the target/dependency folder within the Titan-Node package.
Why can't Java find the file? Is there a missing classpath entry? If so would it need to be set for the database or for node? I figure it would be node because that's the app trying to load the class.
EDIT
Dan's suggestion gave me....
'java.lang.IllegalArgumentException: Could not find implementation
class:
"com.thinkaurelius.titan.diskstorage.cassandra.thrift.CassandraThriftStoreManager"'
...and...
'java.lang.IllegalArgumentException: Could not find implementation
class:
"com.thinkaurelius.titan.diskstorage.cassandra.astyanax.AstyanaxStoreManager"'
...respectively.
In fact whatever value you set storage.backend to in the config, that's the name of the class it can't find an implementation for that it reports in the exception message.
If you look in the code here...
https://github.com/thinkaurelius/titan/blob/master/titan-core/src/main/java/com/thinkaurelius/titan/diskstorage/Backend.java
...the instantiate method that is throwing the exception uses the method parameter clazzname in the message which is passed in by the call in getImplementationClass. The latter ought to be looking up the class to load via the key set in storage.backend but oddly it doesn't seem to find anything so it uses the value as is. Even so, it still can't find the class even if you set it directly. So there's a second mystery here.
I suppose I will have to go through Rexster until this is fixed.
At this time titan-node supports Titan 0.4.1 java jar files.
You can upgrade it by replacing new Titan jar files.
Then you can use the code bellow to connect to Titan
var Titan = require('titan-node');
var gremlin = new Titan.Gremlin({ loglevel: 'OFF' });
var BaseConfiguration = gremlin.java.import('org.apache.commons.configuration.BaseConfiguration');
var _confObj={'backend':'cassandra','hostname':'127.0.0.1'};
var TitanFactory = gremlin.java.import('com.thinkaurelius.titan.core.TitanFactory');
conf = new BaseConfiguration();
conf.setPropertySync("storage.backend",_confObj.backend);
conf.setPropertySync("storage.hostname",_confObj.hostname);
var graph = TitanFactory.openSync(conf);
var g = gremlin.wrap(graph);
g.addVertex(null, function (err, saturn) {
console.log('added');
g.commit(function() {
console.log('commited');
});
});
I have a problem in a system that I am working as back-end support for. I need to write a test that calls one of the classes handeling the communications with our database so I can log out what it actually returns.
System setup
Our system is developed in Java and deployed on a weblogic server. It consists of many parts that I will not go into detail on here. But the interesting part is that we have a class acting as an adapter for our database. We call it "CMAdapter" and it is an implementations of IBM Content Manager specific code to handle interaction with our database. In this class we have a methid called fetchAct() that take one object with search parameters as an argument and it returns the result of the search. In this case it returns one act. The code we have is running on a weblogic server, that has an IBM Information Integrator for Content installed so that it can communicate with IBM Content Manager that is installed and running on a different server. The system is deployed on the server using a .ejb and a few .jar files.
The problem
I have recieved a case stating that for some acts the users are not recieving the full act as expected but only parts of it. The system itself displays no errors and the documents are present in the database. So what I am trying to do is write a simple test program that calls this "CMAdapter" with a predetermined set of searchcriteria so that I may log out the return of the search.
My question
How can I make a freestading class with a main() method and run it on the server? I need to make a call to the CMAdapter.fetchAct() method in a way so it runs on the server like any normal query?
My test class
public class TestHamtaAkt
{
public static void main(String[] args) throws BasException
{
Log LOG = Log.getLog(TestHamtaAkt.class);
// Get the CMAdapter
CMAdapter cmadapter = new CMAdapter();
// Create empty instance of our query object
SokVO sokvo = new SokVO();
// Put a value to search for in our query object
AttributVO aktAttribut = new AttributVO();
aktAttribut.setNamn(DLAKonstanter.AKT_KORT_R_KOD);
aktAttribut.setVarde("090084831574");
sokvo.aktAttributLista().add(aktAttribut);
// do the search an recieve the answer
AktVO aktvo = cmadapter.hamtaAkt(sokvo);
// log out the result
LOG.debug("main", "Akten som hämtades: " + aktvo.toString(), null);
}
}
Thanks to all for reading my question. It appears I have found the answer to my own question. It was hiding with a collegue of mine. The answer to my problem was, to be able to access the server deployed code, I need to get the JNDI context from my webserver and from that do a jndi lookup for the class I need.
I still have some problems making the connection but that is problably just my configurations that are off. I now know how I get a simple java class to make a call to a deployed class on a server.
here is the code I am currently using to get the context from the WLS server:
private static InitialContext getWLSContext(String url) throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, WLS_CONTEXT_FACTORY);
//env.put(Context.PROVIDER_URL, "t3://" + host + ":" + port);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
This is my code for geting the class I need.
public static EJBObject getRemote(String url, String jndiname, Class homeClass, AppserverTyp typ) throws Exception
{
Object obj = getWLSContext(url).lookup(jndiname);
EJBHome home = (EJBHome) javax.rmi.PortableRemoteObject.narrow(obj, homeClass);
Class homeBase = home.getClass();
Method m = homeBase.getMethod("create", (Class[])null);
EJBObject remote = (EJBObject) m.invoke(home, (Object[])null);
return remote;
}
I hope this helps someone with a similar problem to move forward. Like I said I have still to actually get this code working for me but my this is the answer to my initial question on how to make a call to a deployed method from an external class.
I am attempting to combine two disparate data sources into one MBO. The sybase documentation states that you have to develop a custom result set filter in java. Ok. No big deal. I am somewhat familiar with JDBC Rowsets so I think I can probably handle this. So I go grab JConnect 7 and am attempting to use the JDBC driver in my result set filter to get a result set that I can use to filter the data set returned from the web service I am connecting to. I have tested my connection with the simple test below:
try
{
DriverManager.registerDriver((Driver)Class.forName("com.sybase.jdbc4.jdbc.SybDriver").newInstance());
Connection conn = DriverManager.getConnection("jdbc:sybase:Tds:localhost:5500", "dba", "sql");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select * from traveler where traveler_id = 1");
while (rs.next())
{
System.out.println(rs.getRow());
}
} catch (Exception se)
{
se.printStackTrace();
}
Of which I get a rowcount of one back, which is what I am expecting. The problem is when I implement this same code in the ResultSet filter class, and attempt to preview it in the MBO, I get the following error in the eclipse debug console (distilled for clarity):
22:14:20 [ERROR] [ExecuteSection]: Execution error
java.lang.reflect.InvocationTargetException...
[...]
Caused by: java.lang.UnsatisfiedLinkError: no dbjodbc11 in java.library.path
This looks like a classpath issue, but I have the Jconnect jar in my build path, and that is verified by the fact that my little test runs successfully. Is this an eclipse issue.. is eclipse using a different classpath when executing code for the Mobile workspace project?
Can't remember the exact path right now, but there is a third-party lib folder under the <SybaseInstallation>/UnwiredWorkspace folder. Try adding your jar there.