def get_new_connection(self, kwargs): self.connectionFactory = kwargs.get( 'options' ) and kwargs.get( 'options' ).get( 'CONNECTION_FACTORY' ) or None if self.connectionFactory: con = self.connectionFactory.getConnection() connection = PyConnection( con ) else: host = kwargs.get( 'host' ) or 'localhost' port = kwargs.get( 'port' ) and ( ':%s' % kwargs.get( 'port' )) or '' DriverType = 4 kwargsKeys = kwargs.keys() if kwargsKeys.__contains__( 'DriverType' ): DriverType = kwargs['DriverType'] if DriverType == 4: conn_string = "jdbc:db2://%s%s/%s" % ( host, port, kwargs.get( 'database' ) ) elif DriverType == 2: conn_string = "jdbc:db2:%s" % ( kwargs.get( 'database' ) ) else: raise ImproperlyConfigured( "Wrong Driver type" ) # for Setting default AUTO COMMIT off on connection. autocommit = False if kwargs.get( 'options' ) and kwargs.get( 'options' ).keys().__contains__( 'autocommit' ): autocommit = kwargs.get( 'options' ).get( 'autocommit' ) del kwargs.get( 'options' )['autocommit'] connection = zxJDBC.connect( conn_string, kwargs.get( 'user' ), kwargs.get( 'password' ), 'com.ibm.db2.jcc.DB2Driver', kwargs.get( 'options' ) ) # To prevent dirty reads self.__prevent_dirty_reads( connection ) connection.__connection__.setAutoCommit( autocommit ) return connection
def __init__(self, jConnection): """ @type: java.sql.Connection or com.hp.ucmdb.discovery.probe.util.DiscoveryProbeDbConnection """ assert jConnection, 'Connection should not be None' wrapper = self.__getJdbcConnection(jConnection) connection = PyConnection(wrapper) self._cursor = connection.cursor() self.queryCount = 0
def getDBConnection(userName, password, driverName, connectionURL): """ Return PyConnection to the database (see Jython's zxJDBC description) @param userName: the username to connect to DB with @param password: the password to connect to DB with @param driverName: name of the driver to connect through @return: com.ziclix.python.sql.PyConnection """ jdbcDriver = Class.forName( driverName, 1, Thread.currentThread().getContextClassLoader()).newInstance() props = Properties() props.put('user', userName) props.put('password', password) return PyConnection(jdbcDriver.connect(connectionURL, props))