Unable to create a JDBC connection in python from Jep

1,170 views
Skip to first unread message

Aravind Siripireddy

unread,
Jun 29, 2018, 5:26:41 AM6/29/18
to Jep Project
Hello Team,

I'm trying to create a jdbc connection to monetdb using jaydebeapi as follows.

import jaydebeapi

def __getConnection__(username, password):
  conn = jaydebeapi.connect(jclassname='nl.cwi.monetdb.jdbc.MonetDriver',url='jdbc:monetdb://localhost:50000/altairba',driver_args=[<username>,<password]>,jars=[<driver_path>])
  print(conn)
  return conn
jdbc = __getConnection__(<uname>,<pwd>)

When I run this piece of code in  python environment, it works! 

I could see a connection obj created
<jaydebeapi.Connection object at 0x000000000EA41C50>


But when I try to invoke this function from JAVA environment using 'Jep'(Jep embeds CPython in Java through JNI ) 
i'm seeing the following error

jep.JepException: <class 'RuntimeError'>: Unable to start JVM at native\common\jp_env.cpp:78
at E:\Installation\Anaconda\lib\site-packages\jpype\_core.startJVM(_core.py:50)
at E:\Installation\Anaconda\lib\site-packages\jaydebeapi\__init__._jdbc_connect_jpype(__init__.py:176)
at E:\Installation\Anaconda\lib\site-packages\jaydebeapi\__init__.connect(__init__.py:381)
at E:\Installation\Anaconda\lib\site-packages\caDB\__init__.__getDatasourceConnection__(__init__.py:867)
at E:\Installation\Anaconda\lib\site-packages\caDB\__init__.connect_ca(__init__.py:814)
at <string>.multiplier(<string>:4)
at <string>.<module>(<string>:1)
at jep.Jep.eval(Native Method)
at jep.Jep.eval(Jep.java:609)
at com.altair.ba.advancedscript.BAPythonEngine.execFormula(BAPythonEngine.java:127)
at com.altair.ba.advancedscript.BAPythonEngine.execScript(BAPythonEngine.java:64)
at com.altair.ba.advancedscript.BAAdvancedScriptService$3.run(BAAdvancedScriptService.java:193)
at java.lang.Thread.run(Thread.java:745)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

Please let me know, how to sort this out.

Thanks,
Aravind

Aravind Siripireddy

unread,
Jun 29, 2018, 5:28:29 AM6/29/18
to Jep Project
I'm Using Jep - 3.7.1 and Python 3.6.3

Ben Steffensmeier

unread,
Jun 29, 2018, 10:03:25 AM6/29/18
to Jep Project
I think the exception is occurring because jpype is trying to start a JVM when the JVM is already running. Jep allows you to embed python in Java and jpype allows
you to embed Java in Python so this usage would embed Java inside Python that is embedded inside of Java, I am not sure this use case can be supported. Both JPype and Jep expect to control the Java and Python interaction and I do not think it would be safe to have both of them doing it at once. This particular error would seem to be because JPype is trying to start a JVM instead of attaching to the existing JVM, so I think it would need to be handled differently in the JPype code to get past this exception.

Ben

Nathan Jensen

unread,
Jun 29, 2018, 11:07:35 AM6/29/18
to Jep Project
If you're ok with running from a Java process instead of a Python process, with Jep you could import the Java JDBC classes in Python code and make the JDBC connection.  A long time ago mrj0 wrote a unit test that while not perfect it does illustrate one example of how this could be accomplished.  https://github.com/ninia/jep/blob/v3.7.1/tests/test_jdbc.py 

But as Ben said, you're trying to run JPype, a Python process that embeds the JVM, and Jep, a Java process that embeds CPython, and the two cannot coexist at this time.  They're coming at the language integration from the opposite direction.

--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project+unsubscribe@googlegroups.com.
To post to this group, send email to jep-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jep-project/e9f5854e-6e6a-4d23-9517-19d876ef241b%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Aravind Siripireddy

unread,
Jul 3, 2018, 2:39:29 AM7/3/18
to Jep Project
But we have an environment like this, a Java server(tomcat) which will invoke python script to run some algorithms and save the out comes to DB.
In this process, we are connecting to different databases from python script(using jaydebeapi- a JDBC kind of implementation for python),fetching the data from it and doing some stuff and saving the results back to DB.

Nathan,
As you suggested, if we import the Java JDBC classes in Python code and make the JDBC connection using jep, doesn't it embed Java in Python again as we are supposed to call this python code from Java server. Don't we hit the same problem what we are facing with Jpype?

Thanks,
Aravind
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project...@googlegroups.com.

To post to this group, send email to jep-p...@googlegroups.com.

Ben Steffensmeier

unread,
Jul 3, 2018, 11:51:52 AM7/3/18
to Jep Project
Aravind,

While executing Python in Jep it is completely valid to call back into Java and access Java classes, objects and methods. The code Nathan suggested is part of our unit tests and is proven to work for using JDBC from Python with Jep. The problem with jaydebeapi is that it uses jpype, which is not aware of the Jep environment so it tries to start another instance of the JVM even though there is already one running and that is what causes problems.

Ben

Aravind Siripireddy

unread,
Jul 6, 2018, 8:16:26 AM7/6/18
to Jep Project
I have come across another problem, while tyring to use jep.jdbc as Nathan suggested
We have a python module called 'caDB' in which we want to connect to different databases and do some stuff.
From Java server(using Jep) we access the functions of caDB.

I have used jep.jdbc in caDB module to create a JDBC connection.
But Now, when I import caDB i'm seeing the following error, though im running in JVM

 jep.eval("import caDB")  // error here

Caused by: jep.JepException: <class 'ImportError'>: Jep is not supported in standalone Python, it must be embedded in Java.

Thanks,
Aravind

Nathan Jensen

unread,
Jul 9, 2018, 12:03:11 PM7/9/18
to Jep Project
I'm not sure how you got that.

Can you try the 3.8 release candidate and report back what errors you get from it?  It has better error logging at startup.  https://github.com/ninia/jep/tree/v3.8rc

Also you could alter jep's __init__.py and use traceback or prints to get the underlying error out of the except block.

try:
    from _jep import *
except ImportError:
    # use traceback here and/or do except ImportError as e and print out or log the information provided by e
    raise ImportError("Jep is not supported in standalone Python, it must be embedded in Java.")

--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project+unsubscribe@googlegroups.com.

To post to this group, send email to jep-p...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages