Skip to content

Commit

Permalink
add query timeout to execute and executemany methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Fernandez committed Jan 3, 2019
1 parent e99a05d commit b04ab61
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions jaydebeapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def TimestampFromTicks(ticks):
return apply(Timestamp, time.localtime(ticks)[:6])

# DB-API 2.0 Module Interface connect constructor
def connect(jclassname, url, driver_args=None, jars=None, libs=None):
def connect(jclassname, url, driver_args=None, jars=None, libs=None, timeout=None):
"""Open a connection to a database using a JDBC driver and return
a Connection instance.
Expand Down Expand Up @@ -378,8 +378,7 @@ def connect(jclassname, url, driver_args=None, jars=None, libs=None):
libs = [ libs ]
else:
libs = []
jconn = _jdbc_connect(jclassname, url, driver_args, jars, libs)
return Connection(jconn, _converters)
return Connection(jconn, _converters, timeout)

# DB-API 2.0 Connection Object
class Connection(object):
Expand All @@ -395,10 +394,11 @@ class Connection(object):
DataError = DataError
NotSupportedError = NotSupportedError

def __init__(self, jconn, converters):
def __init__(self, jconn, converters, timeout=None):
self.jconn = jconn
self._closed = False
self._converters = converters
self._timeout = timeout

def close(self):
if self._closed:
Expand All @@ -419,7 +419,7 @@ def rollback(self):
_handle_sql_exception()

def cursor(self):
return Cursor(self, self._converters)
return Cursor(self, self._converters, self._timeout)

# DB-API 2.0 Cursor Object
class Cursor(object):
Expand All @@ -430,9 +430,10 @@ class Cursor(object):
_rs = None
_description = None

def __init__(self, connection, converters):
def __init__(self, connection, converters, timeout=None):
self._connection = connection
self._converters = converters
self._timeout = timeout

@property
def description(self):
Expand Down Expand Up @@ -486,9 +487,12 @@ def _close_last(self):

def _set_stmt_parms(self, prep_stmt, parameters):
for i in range(len(parameters)):
# print (i, parameters[i], type(parameters[i]))
prep_stmt.setObject(i + 1, parameters[i])

# https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#setQueryTimeout(int)
if self._timeout is not None:
prep_stmt.setQueryTimeout(int(self._timeout)) # unit: seconds

def execute(self, operation, parameters=None):
if self._connection._closed:
raise Error()
Expand Down

0 comments on commit b04ab61

Please sign in to comment.