2

I'm using flask-appbuilder to build an application. To submit a form to execute ddl, I add a jar file to jpype when startJVM(), but java always crashes. Below is my code:

import jaydebeapi
from flask import flash

def execute_ddl(dic):
    def set_up_conn(dic):
        conn = jaydebeapi.connect(jclassname=dic['jclass'],
                              url=dic['url'],
                              driver_args=[dic['driver_name'], 
                              dic['driver_params']],
                              jars=dic['jars'])
        curs = conn.cursor()
        curs.execute(dic['ddl'])
        output = curs.fetchall()
        print(output)
        curs.close()
        conn.close()
    try:
        import jpype
        if not jpype.isJVMStarted():
            jvmPath = jpype.getDefaultJVMPath()
            flash(str(jvmPath), 'info')
            jpype.startJVM(jvmPath, "-ea", "-Djava.class.path=/lib/uber-hive-jdbc-1.2.1.jar")

        if not jpype.isThreadAttachedToJVM():
            jpype.attachThreadToJVM()
            jpype.java.lang.Thread.currentThread().\
         setContextClassLoader(jpype.java.lang.ClassLoader.getSystemClassLoader())
        set_up_conn(dic)
        # post process form
        flash('{} is submitted!'.format(dic['ddl']), 'info')
    except Exception as e:
        flash(str(e), 'info')

This is the crashing error and I tried "ulimit -c unlimited" but no luck.

# A fatal error has been detected by the Java Runtime Environment:
#  
#  SIGSEGV (0xb) at pc=0x00007fce15f978b9, pid=12, tid=0x00007fce0f7fe700
#
# JRE version: OpenJDK Runtime Environment (8.0_181-b13) (build 1.8.0_181-8u181-b13-2~deb9u1-b13)
# Java VM: OpenJDK 64-Bit Server VM (25.181-b13 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  [_jpype.cpython-37m-x86_64-linux-gnu.so+0x3f8b9]  
JPJavaEnv::NewLocalRef(_jobject*)+0x9
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# //hs_err_pid12.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
dlwlrma
  • 808
  • 2
  • 10
  • 21

1 Answers1

0

I used the multi_threading which is the reason.

Jaydebeapi uses jpype to start JVM. You need to use jpype.attachThreadToJVM() in the thread body to make the JVM usable from that thread. You can use jpype.isThreadAttachedToJVM() to check it is attached.

ItsPete
  • 2,363
  • 3
  • 27
  • 35
  • you can refer to jpype document ,the threading part.https://jpype.readthedocs.io/en/latest/userguide.html#overview – Berton_sun Mar 28 '19 at 04:01