Python psycopg2.extensions.STATUS_IN_TRANSACTION Examples
The following are 9
code examples of psycopg2.extensions.STATUS_IN_TRANSACTION().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
psycopg2.extensions
, or try the search function
.
Example #1
Source File: _json.py From aws-workshop with MIT License | 5 votes |
def _get_json_oids(conn_or_curs, name='json'): # lazy imports from psycopg2.extensions import STATUS_IN_TRANSACTION from psycopg2.extras import _solve_conn_curs conn, curs = _solve_conn_curs(conn_or_curs) # Store the transaction status of the connection to revert it after use conn_status = conn.status # column typarray not available before PG 8.3 typarray = conn.server_version >= 80300 and "typarray" or "NULL" # get the oid for the hstore curs.execute( "SELECT t.oid, %s FROM pg_type t WHERE t.typname = %%s;" % typarray, (name,)) r = curs.fetchone() # revert the status of the connection as before the command if (conn_status != STATUS_IN_TRANSACTION and not conn.autocommit): conn.rollback() if not r: raise conn.ProgrammingError("%s data type not found" % name) return r
Example #2
Source File: extras.py From aws-workshop with MIT License | 5 votes |
def get_oids(self, conn_or_curs): """Return the lists of OID of the hstore and hstore[] types. """ conn, curs = _solve_conn_curs(conn_or_curs) # Store the transaction status of the connection to revert it after use conn_status = conn.status # column typarray not available before PG 8.3 typarray = conn.server_version >= 80300 and "typarray" or "NULL" rv0, rv1 = [], [] # get the oid for the hstore curs.execute("""\ SELECT t.oid, %s FROM pg_type t JOIN pg_namespace ns ON typnamespace = ns.oid WHERE typname = 'hstore'; """ % typarray) for oids in curs: rv0.append(oids[0]) rv1.append(oids[1]) # revert the status of the connection as before the command if (conn_status != _ext.STATUS_IN_TRANSACTION and not conn.autocommit): conn.rollback() return tuple(rv0), tuple(rv1)
Example #3
Source File: postgres.py From barman with GNU General Public License v3.0 | 5 votes |
def close(self): """ Close the connection to PostgreSQL """ if self._conn: # If the connection is still alive, rollback and close it if not self._conn.closed: if self._conn.status == STATUS_IN_TRANSACTION: self._conn.rollback() self._conn.close() # Remove the connection from the live connections list self._conn = None _live_connections.remove(self)
Example #4
Source File: _json.py From syntheticmass with Apache License 2.0 | 5 votes |
def _get_json_oids(conn_or_curs, name='json'): # lazy imports from psycopg2.extensions import STATUS_IN_TRANSACTION from psycopg2.extras import _solve_conn_curs conn, curs = _solve_conn_curs(conn_or_curs) # Store the transaction status of the connection to revert it after use conn_status = conn.status # column typarray not available before PG 8.3 typarray = conn.server_version >= 80300 and "typarray" or "NULL" # get the oid for the hstore curs.execute( "SELECT t.oid, %s FROM pg_type t WHERE t.typname = %%s;" % typarray, (name,)) r = curs.fetchone() # revert the status of the connection as before the command if (conn_status != STATUS_IN_TRANSACTION and not conn.autocommit): conn.rollback() if not r: raise conn.ProgrammingError("%s data type not found" % name) return r
Example #5
Source File: extras.py From syntheticmass with Apache License 2.0 | 5 votes |
def get_oids(self, conn_or_curs): """Return the lists of OID of the hstore and hstore[] types. """ conn, curs = _solve_conn_curs(conn_or_curs) # Store the transaction status of the connection to revert it after use conn_status = conn.status # column typarray not available before PG 8.3 typarray = conn.server_version >= 80300 and "typarray" or "NULL" rv0, rv1 = [], [] # get the oid for the hstore curs.execute("""\ SELECT t.oid, %s FROM pg_type t JOIN pg_namespace ns ON typnamespace = ns.oid WHERE typname = 'hstore'; """ % typarray) for oids in curs: rv0.append(oids[0]) rv1.append(oids[1]) # revert the status of the connection as before the command if (conn_status != _ext.STATUS_IN_TRANSACTION and not conn.autocommit): conn.rollback() return tuple(rv0), tuple(rv1)
Example #6
Source File: test_dialect.py From sqlalchemy with MIT License | 5 votes |
def test_initial_transaction_state(self): from psycopg2.extensions import STATUS_IN_TRANSACTION engine = engines.testing_engine() with engine.connect() as conn: ne_(conn.connection.status, STATUS_IN_TRANSACTION)
Example #7
Source File: extras.py From aws-workshop with MIT License | 4 votes |
def _from_db(self, name, conn_or_curs): """Return a `CompositeCaster` instance for the type *name*. Raise `ProgrammingError` if the type is not found. """ conn, curs = _solve_conn_curs(conn_or_curs) # Store the transaction status of the connection to revert it after use conn_status = conn.status # Use the correct schema if '.' in name: schema, tname = name.split('.', 1) else: tname = name schema = 'public' # column typarray not available before PG 8.3 typarray = conn.server_version >= 80300 and "typarray" or "NULL" # get the type oid and attributes curs.execute("""\ SELECT t.oid, %s, attname, atttypid FROM pg_type t JOIN pg_namespace ns ON typnamespace = ns.oid JOIN pg_attribute a ON attrelid = typrelid WHERE typname = %%s AND nspname = %%s AND attnum > 0 AND NOT attisdropped ORDER BY attnum; """ % typarray, (tname, schema)) recs = curs.fetchall() # revert the status of the connection as before the command if (conn_status != _ext.STATUS_IN_TRANSACTION and not conn.autocommit): conn.rollback() if not recs: raise psycopg2.ProgrammingError( "PostgreSQL type '%s' not found" % name) type_oid = recs[0][0] array_oid = recs[0][1] type_attrs = [(r[2], r[3]) for r in recs] return self(tname, type_oid, type_attrs, array_oid=array_oid, schema=schema)
Example #8
Source File: db.py From pajbot with MIT License | 4 votes |
def create_dbapi_connection_scope(autocommit=False): # This method's contextmanager just checks the connection out, sets autocommit if desired, and # returns the connection to the pool when the contextmanager exits # It does not perform transaction control! create_dbapi_session_scope is more useful since it provides # an automatic COMMIT/ROLLBACK mechanism, and also returns a DBAPI Session instead of Connection. # https://docs.sqlalchemy.org/en/13/core/connections.html#sqlalchemy.engine.Engine.raw_connection pool_connection = DBManager.engine.raw_connection() # Quoting from the SQLAlchemy docs: # > The returned object is a proxied version of the DBAPI connection object used by the underlying driver in use. # To be able to use the connection as a contextmanager for transaction control, # we need the actual underlying connection, the SQLAlchemy proxy will not work for this. raw_connection = pool_connection.connection try: if autocommit: # The parameter "pool_pre_ping=True" from create_engine() above makes SQLAlchemy issue a # "SELECT 1" to test the connection works before returning it from the connection pool. # Even the simple "SELECT 1" has automatically begun a transaction, and the connection # will now be "idle in transaction". # # Because we can only enable autocommit on "idle" connections, we need to ROLLBACK if a transaction # is ongoing first: if raw_connection.status == STATUS_IN_TRANSACTION: raw_connection.rollback() # http://initd.org/psycopg/docs/connection.html#connection.autocommit raw_connection.autocommit = True try: yield raw_connection finally: # We want to reset the connection to a clean state in the end, so we don't return connections # to the pool that are in an ongoing transaction, or whose transaction is aborted # (typical error message: "ERROR: current transaction is aborted, # commands ignored until end of transaction block") if raw_connection.status == STATUS_IN_TRANSACTION: raw_connection.rollback() finally: if autocommit: # Because the connection is returned to the pool, we want to reset the autocommit state on it raw_connection.autocommit = False # Finally release connection back to the pool (notice we use .close() on the pool connection, # not raw_connection which would close the connection for good) pool_connection.close()
Example #9
Source File: extras.py From syntheticmass with Apache License 2.0 | 4 votes |
def _from_db(self, name, conn_or_curs): """Return a `CompositeCaster` instance for the type *name*. Raise `ProgrammingError` if the type is not found. """ conn, curs = _solve_conn_curs(conn_or_curs) # Store the transaction status of the connection to revert it after use conn_status = conn.status # Use the correct schema if '.' in name: schema, tname = name.split('.', 1) else: tname = name schema = 'public' # column typarray not available before PG 8.3 typarray = conn.server_version >= 80300 and "typarray" or "NULL" # get the type oid and attributes curs.execute("""\ SELECT t.oid, %s, attname, atttypid FROM pg_type t JOIN pg_namespace ns ON typnamespace = ns.oid JOIN pg_attribute a ON attrelid = typrelid WHERE typname = %%s AND nspname = %%s AND attnum > 0 AND NOT attisdropped ORDER BY attnum; """ % typarray, (tname, schema)) recs = curs.fetchall() # revert the status of the connection as before the command if (conn_status != _ext.STATUS_IN_TRANSACTION and not conn.autocommit): conn.rollback() if not recs: raise psycopg2.ProgrammingError( "PostgreSQL type '%s' not found" % name) type_oid = recs[0][0] array_oid = recs[0][1] type_attrs = [ (r[2], r[3]) for r in recs ] return self(tname, type_oid, type_attrs, array_oid=array_oid, schema=schema)