Example #1
0
    def __init__(self, connect, driver, user, password,isolation=''):
        config_parser = SafeConfigParser()
        config_parser.read(PATH_JDBC_CFG)
        # since it is impossible to call jpype.isThreadAttachedToJVM() before jpype.startJVM()
        # we won't check if JVM is started.
        classpath = config_parser.get("JVM", "classpath")
        javaHome  = config_parser.get("JVM", "java_home")
        if (javaHome[0] != '/'):
            javaHome = "%s/%s" % (etl_util.get_app_root_path(), javaHome)

        args='-Djava.class.path=%s' % classpath
        try:
            os.environ['JAVA_HOME'] = javaHome
            jvm_path                = jpype.getDefaultJVMPath()
        except TypeError:
            logging.error("failed to get default JVM path")
            raise
        if (jpype.isJVMStarted() == False):
            jpype.startJVM(jvm_path, args)
        logging.debug("Connecting with %s to %s with user %s" % (driver, connect, user))
        self.conn = jaydebeapi.connect(driver, connect, user, password)

        #set isolation level
        if isolation!='':

            if 'mysql' in driver:
                sql='SET SESSION TRANSACTION ISOLATION LEVEL %s' % isolation
            elif 'teradata' in driver:
                sql='SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL %s' % isolation

            self._execute(sql)
Example #2
0
import os
from ConfigParser import SafeConfigParser
import jpype
import logging
import thread

import etl_util
import modules.r10_jdbc as jaydebeapi

# Allow to have an external test_jdbc.cfg for local platform development
PATH_JDBC_CFG = os.path.join(os.path.expanduser("~"), '.etl', 'jdbc.cfg')
if not os.path.isfile(PATH_JDBC_CFG):
    PATH_JDBC_CFG = os.path.join(etl_util.get_app_root_path(), 'conf', 'modules', 'jdbc.cfg')

class JDBCFactory(type):
    '''
    This class is factory for JDBC objects

    JDBCObj
        a dictionary of JDBC objects

        JDBCObj Key:
            (connect, driver, user, password, isolation)

        JDBCObj Value:
            a JDBC object

    Usage of JDBC
      Usage1: The isolation remains default.
        JDBC(connect, driver, user, password)
        In this case, JDBCObj Key is (connect, driver, user, password, '')