baztian / jaydebeapi

JayDeBeApi module allows you to connect from Python code to databases using Java JDBC. It provides a Python DB-API v2.0 to that database.
GNU Lesser General Public License v3.0
366 stars 148 forks source link

unable to open connections multiple times using jaydebeapi in a flask webservice app #84

Open arunnatva opened 6 years ago

arunnatva commented 6 years ago

Traceback (most recent call last): File "/home/aknatva/mdef/src/model_exec_stats_service.py", line 62, in get self.db_conn = PhoenixDam.get_connection("/home/aknatva/mdef/conf/db_conn.conf") File "/home/aknatva/mdef/src/phoenix_dam.py", line 11, in get_connection db_conn = jaydebeapi.connect(config_object["driver"],config_object["conn_string"],config_object["class_path"],) File "/opt/anaconda3/envs/mdef/lib/python3.5/site-packages/jaydebeapi/init.py", line 68, in connect connection = gateway.jvm.DriverManager.getConnection(*driver_args) File "/opt/anaconda3/envs/mdef/lib/python3.5/site-packages/py4j/java_gateway.py", line 1257, in call answer, self.gateway_client, self.target_id, self.name) File "/opt/anaconda3/envs/mdef/lib/python3.5/site-packages/py4j/protocol.py", line 332, in get_return_value format(target_id, ".", name, value)) py4j.protocol.Py4JError: An error occurred while calling z:java.sql.DriverManager.getConnection. Trace: py4j.Py4JException: Method getConnection([class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String]) does not exist at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318) at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:339) at py4j.Gateway.invoke(Gateway.java:276) at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132) at py4j.commands.CallCommand.execute(CallCommand.java:79) at py4j.GatewayConnection.run(GatewayConnection.java:238) at java.lang.Thread.run(Thread.java:748)

I am building a Rest API using flask where i have to access apache phoenix tables for every rest api call. I could not figure out how to open one single connection and use it for all resquests, so I am opening and closing the connection for every request.

Can you please help me out with this

weihanhua commented 5 years ago

Have you solved it? I have the same requirement and problem

john-drews commented 4 years ago

i did not get the exact same error but something close to it. i followed the below stack overflow to fix it

https://stackoverflow.com/questions/49105504/jaydebeapi-triggers-java-sql-sqlexception-no-suitable-driver-found

    @contextmanager
    def get_cursor(self):
        if jpype.isJVMStarted() and not jpype.isThreadAttachedToJVM():
            jpype.attachThreadToJVM()
            jpype.java.lang.Thread.currentThread().setContextClassLoader(
                jpype.java.lang.ClassLoader.getSystemClassLoader())
        conn = jaydebeapi.connect(self.driver,
                                  self.url.format(host=self.creds.host),
                                  [self.creds.user, self.creds.pw])
        cur = conn.cursor()
        try:
            yield cur
        finally:
            cur.close()
            conn.close()