Rakesh
Rakesh

Reputation: 4127

Graceful way of cleaning objects in sqlalchemy?

Currently I am developing a class which abstracts the SQLAlchemy. This class will act as helper tool to verify the values from database. This class will be used in regression/load test. Test cases will make hundred-thousands of database query. The layout of my class is as following.

class MyDBClass:
    def __init__(self, dbName)
         self.dbName = dbName
         self.dbEngines[dbName] = create_engine()
         self.dbMetaData[dbName] = MetaData()
         self.dbMetaData[dbName].reflect(bind=self.dbEngines[dbName])
         self.dbSession[dbName] = sessionmaker(bind=self.dbEngines[dbName])

    def QueryFunction(self,dbName, tablename, some arguments):
          session = self.dbSession[dbName]()
          query = session.query(requiredTable)
          result = query.filter().all()
          session.close()
    def updateFunction(self, dbName, talbeName, some arguments):
          session = self.dbSession[dbName]()
          session.query(requiredTable).filter().update()
          session.commit()
          session.close()
    def insertFunction(self, dbName, tableName, some arguments):
          connection = self.dbEngines[dbName].connect()
          requiredTable = self.dbMetaData[dbName].tables[tableName]
          connection.execute(requiredTable.insert(values=columnValuePair))
          connection.close()
    def cleanClose(self):
          # Code which will remove the connection/session/object from memory.
          # do some graceful work to clean close.

I want to write cleanClose() method which should remove the object which might be created by this class. This method should remove all those object from memory and provide a clean close. This may also avoid the memory leak. I am not able to figure out what all object should be removed from the memory. Can some one suggest me what method calls I need to make here?

Edit1:

Is there any way by which I can measure the performance different method and their variant?
I was going through the documentation here and realized that I should not make session in every method rather I should create single instance of session and use throughout. Please provide your feedback on this. And let me know what would be the best way of doing thing here.

Any kind of help will be greatly appreciated here.

Upvotes: 0

Views: 1964

Answers (1)

Kylotan
Kylotan

Reputation: 18449

To remove objects from memory in Python, you just need to stop referencing them. There is not usually any need to explicitly write or call any methods to destroy or clean up the objects. So, an instance of MyDBClass will be automatically cleaned up when it goes out of scope.

If you are talking about closing down an SQLAlchemy session, then you just need to call the close() method on it.

An SQLAlchemy session is designed for multiple transactions. You don't generally need to create and destroy it multiple times. Create one session in the __init__ function and then use that in QueryFunction, updateFunction, etc.

Upvotes: 3

Related Questions