Python-Flask-Postgresql DB sql Locks - python

I tried looking in the documentation of Flask and SQLAlchemy but I couldn't find the answer...
Do the sessions used with SQLAlchemy automatically lock the DB? I do a read (let's say it takes a long time) and it seems to block out any other reads I am trying to do on my Postgres DB. Is this something I have to configure manually on Postgres using constraints or is there something I can change with Flask / SQLAlchemy?
Thanks

Related

Is SQLAlchemy a database itself?

I am new to python but I have worked with ORM frameworks before.
It is confusing for me that when I create database connection I can specify a database URL to some remote DB.
Or I can create DB in a file without remote DB. How does SQLAlchemy work with a file then?
It doesn`t have an RDBMS to connect to. Only file. So this is the reason for my question:
Is SQLAlchemy an RDBMS itself?
It's not a database, but a library for handling databases, or as they put it:
SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.
The file based dialect is SQLite.

How to set query execution timeout in sqlalchemy sessions

I am using MSSQL as my database. It is taking too long to respond back for some queries and sometimes it is resulting in deadlocks. Hence i want to set custom timeout for queries. I got some custom solution here but it is for psycopg2 and sqlalchemy but i need it for MSSQL and sqlalchemy. Is there any way to accomplish this?
Thanks in advance.

lock rows so others can't modify them using Postgres and SQLAlchemy ORM

I'd like to make a query to a Postgres database then somehow lock the returned rows so that other SQLAlchemy threads/processes cannot modify those rows. In the same session/transaction of the query, I'd like to update the rows I received from the query and then commit the changes. Anyone know what to do?
I tried implementing a query with the with_for_update(nowait=True) function, but this throws an OperationalError. I could catch this exception and simply the query again, but I'd like to offload this to the db if possible.
I'm using:
Postgres 9.4.1
SQLAlchemy 1.0.11 (ORM)
Flask-Restful
FlaskSQLAlchemy
I'm prepared to use straight SQLAlchemy if it's not possible with FlaskSQLAlchemy.

Using two databases with Pyramid SqlAlchemy

I have a SQLite database configured with Pyramid Alchemy scaffold in my Linux machine. I have another remote Mysql database which resides in a Windows machine, with loads of data.Now, I have to connect to the remote Mysql database to pull data and populate them into my Sqlite database with the help of sqlAlchemy.
What I had been doing: I used mysql workbench to query data from the mysql database, export results to a csv file, load the csv file into my pyramid initializedb.py through python's default csv module, and then finally insert the retrieved rows into my Sqlite database.
What I want to do: I want to connect to the remote Mysql database from my initializedb.py itself, fetch results and insert them into my sqlite database.
How do I go on about with this? Any help is appreciated.
You can create a connection into your mysql db within the initializedb.py, extract the data and store them into your local db. Now, you will find the basics about sqlalchemy here:
http://docs.sqlalchemy.org/en/rel_0_9/core/tutorial.html
If you need this configurable, then you can put the url of your database into your config and access it through settings.
Also, you do not have to define the structure of your tables exactly, you can use something like:
users = Table('users', metadata, autoload=True)
On the other hand, if your MySQL db structure is the same as the sqlite db structure, it might be possible to reuse your model, but that would probably be hard to explain here without seeing any of your code.

BlueBream framework and multi connections to databases

I put the first steps in BlueBream framework. In my project I must get data from RDBMS - MySQL, PostgreSQL and MS Server. For now, I made a simple tutorial helloworld :) I know how to write Interfaces and Implementations, etc.
My question is: How to set up a connections to RDBMS and multiple connections? Could you give me a simple "step by step" tutorial ?
How to bake a database? Is in BlueBream something like a command "syncdb" in Django?
Take a look at zope.sqlalchemy; it integrates SQLAlchemy into the Zope transaction manager. SQLAlchemy in turn lets you access many different databases, including MySQL, PostgreSQL and MS Server.
The zope.sqlalchemy package explains how to obtain a SQLAlchemy session. From there on out you use bog-standard SQLAlchemy operations for which there are plenty of tutorials and help here on SO. SQLAlchemy can also take care of setting up the database schema for you, if that is needed.

Categories