ionrock / cherrypy-sqlalchemy Goto Github PK
View Code? Open in Web Editor NEWUse SQLAlchemy with CherryPy
License: BSD 3-Clause "New" or "Revised" License
Use SQLAlchemy with CherryPy
License: BSD 3-Clause "New" or "Revised" License
For projects where database schema is maintained using alembic there should be an option to disable database tables with metadata,create_all() at startup.
https://pypi.python.org/pypi/CherryPy-SQLAlchemy
Points to https://github.com/ionrock/cp_sqlalchemy and the repository seems to have been renamed.
So my MySQL server reboots once in a while and when it does so I get the above error message, after that sqlalchemy can no longer connect and repeatedly says
"InvalidRequestError: Can't reconnect until invalid transaction is rolled back"
Is there a way to catch this issue, roll back the transaction and reconnect with cherrypy-sqlalchemy?
0.5.1 has a connection close bug which I fixed and you merged but the pypi release is out of date. We found that 0.5.1 was leaving 250+ connections to the DB open, with 0.5.2 these connections go away.
I am trying to use this in my pytests but I am getting error
AttributeError: type object 'Base' has no attribute 'metadata'
My code is
class Base:
def get_session(self):
cherrypy.tools.db = SQLAlchemyTool()
db_uri = 'postgresql://{0}:{1}@{2}:{3}/{4}'.format(
settings['database']['user'],
settings['database']['password'],
settings['database']['host'],
settings['database']['port'],
settings['database']['dbname']
)
sqlalchemy_plugin = SQLAlchemyPlugin(
cherrypy.engine, Base, db_uri,
echo=True
)
sqlalchemy_plugin.subscribe()
sqlalchemy_plugin.bind_db()
engine = create_engine(db_uri)
Session = sessionmaker(bind=engine)
return Session()
and when I try to use this
class TestAuditModel(Base):
@property
def get_db(self):
return cherrypy.request.db
def test_insert(self):
db = self.get_session() # On this line I get issues
test = Test(starttime=datetime.now(),
endtime=datetime.now(),
count=1)
Now I had written this file after looking at the module files
class SQLAlchemyPlugin(plugins.SimplePlugin):
def __init__(self, bus, orm_base, db_uri, **kwargs):
"""
The plugin is registered to the CherryPy engine and therefore
is part of the bus (the engine *is* a bus) registery.
We use this plugin to create the SA engine. At the same time,
when the plugin starts we create the tables into the database
using the mapped class of the global metadata.
Finally we create a new 'bind' channel that the SA tool
will use to map a session to the SA engine at request time.
"""
plugins.SimplePlugin.__init__(self, bus)
self.db_uri = db_uri
self.orm_base = orm_base
self.create_kwargs = kwargs
self.bus.subscribe('db.start_session', self.start_session)
self.bus.subscribe('db.bind_db', self.bind_db)
self.sa_engine = None
def start(self):
self.sa_engine = create_engine(self.db_uri)
def bind_db(self):
if not self.sa_engine:
self.start()
self.orm_base.metadata.bind = self.sa_engine # (here I get the error
def stop(self):
if self.sa_engine:
self.sa_engine.dispose()
self.sa_engine = None
def start_session(self, session):
session.configure(bind=self.sa_engine)
def execute(self):
return create_engine(self.db_uri).connect()
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.