Skip to content

Commit eab2f8f

Browse files
author
Greg Turnquist
committed
Made tweak to DatabaseTemplate to close the DB connection when DT goes out of scope.
1 parent 0104c8f commit eab2f8f

File tree

6 files changed

+20
-11
lines changed

6 files changed

+20
-11
lines changed

springpython.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ project.key=EXTPY
1414

1515
# This property is uncommented for milestone releases.
1616
release.type=milestone
17-
build.stamp=RC1
17+
build.stamp=RC2
1818

1919
# docbook reference documentation
2020
doc.ref.dir=docs/reference

src/springpython/database/core.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,15 @@ def __init__(self, connection_factory = None):
4646
self.connection_factory = connection_factory
4747
self.logger = logging.getLogger("springpython.database.core.DatabaseTemplate")
4848

49-
def __setattr__(self, name, value):
50-
"""When the connection factory is set, initialize a connection to the database."""
51-
self.__dict__[name] = value
52-
if name == "connection_factory" and value:
53-
self.__db = value.getConnection()
49+
def __del__(self):
50+
"When this template goes out of scope, need to close the connection it formed."
51+
if self.connection_factory is not None: self.connection_factory.close()
5452

5553
def execute(self, sql_statement, args = None):
5654
"""Issue a single SQL execute, typically a DDL statement."""
5755
sql_statement = self.connection_factory.convert_sql_binding(sql_statement)
5856

59-
cursor = self.__db.cursor()
57+
cursor = self.connection_factory.getConnection().cursor()
6058
error = None
6159
rows_affected = 0
6260
try:
@@ -107,7 +105,7 @@ def __query_for_list(self, sql_query, args = None):
107105

108106
sql_query = self.connection_factory.convert_sql_binding(sql_query)
109107

110-
cursor = self.__db.cursor()
108+
cursor = self.connection_factory.getConnection().cursor()
111109
error = None
112110
results = None
113111
metadata = None

src/springpython/database/factory.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
"""
16+
import logging
1617
import re
1718
import sys
1819
import types
@@ -33,6 +34,12 @@ def getConnection(self):
3334
self.__db = self.connect()
3435
return self.__db
3536

37+
def close(self):
38+
"Need to offer API call to close the connection to the database."
39+
if self.__db is not None:
40+
self.__db.close()
41+
self.__db = None
42+
3643
def commit(self):
3744
if self.in_transaction():
3845
self.getConnection().commit()

test/springpythontest/checkin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
from springpythontest.securityWebTestCases import *
1414
from springpythontest.util_test_cases import *
1515
from springpythontest.unicodeTestCases import *
16-
from springpythontest.remoting_xmlrpc import *
17-
from springpythontest.remotingTestCases import Pyro4RemotingTestCase
16+
#from springpythontest.remoting_xmlrpc import *
17+
#from springpythontest.remotingTestCases import Pyro4RemotingTestCase

test/springpythontest/databaseCoreTestCases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ def testConnectingToOracle(self):
8282

8383
def testQueryingOracleWithInvalidlyFormattedArguments(self):
8484
sys.modules["cx_Oracle"] = self.mock()
85-
sys.modules["cx_Oracle"].expects(once()).method("connect")
8685

8786
connection_factory = factory.cxoraConnectionFactory(username="foo", password="bar", hostname="localhost", db="mock")
8887
dt = DatabaseTemplate(connection_factory)
@@ -108,6 +107,7 @@ def testQueryingOracleWithValidlyFormattedArguments(self):
108107

109108
conn = self.mock()
110109
conn.expects(once()).method("cursor").will(return_value(cursor))
110+
conn.expects(once()).method("close")
111111

112112
sys.modules["cx_Oracle"] = self.mock()
113113
sys.modules["cx_Oracle"].expects(once()).method("connect").will(return_value(conn))

test/springpythontest/support/testSupportClasses.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,17 @@ def __init__(self):
192192
self.mockCursor = None
193193
def cursor(self):
194194
return self.mockCursor
195+
def close(self):
196+
pass
195197

196198
class StubDBFactory(ConnectionFactory):
197199
def __init__(self):
198200
ConnectionFactory.__init__(self, [types.TupleType])
199201
self.stubConnection = StubConnection()
200202
def connect(self):
201203
return self.stubConnection
204+
def close(self):
205+
pass
202206

203207
class ImpFileProps(object):
204208
def __init__(self, paystat_work_dir, paystat_reload_dir, paystat_archive_dir, oid):

0 commit comments

Comments
 (0)