MCPcopy
hub / github.com/MagicStack/asyncpg / start

Method start

asyncpg/transaction.py:96–151  ·  view source on GitHub ↗

Enter the transaction or savepoint block.

(self)

Source from the content-addressed store, hash-verified

94
95 @connresource.guarded
96 async def start(self):
97 """Enter the transaction or savepoint block."""
98 self.__check_state_base('start')
99 if self._state is TransactionState.STARTED:
100 raise apg_errors.InterfaceError(
101 'cannot start; the transaction is already started')
102
103 con = self._connection
104
105 if con._top_xact is None:
106 if con._protocol.is_in_transaction():
107 raise apg_errors.InterfaceError(
108 'cannot use Connection.transaction() in '
109 'a manually started transaction')
110 con._top_xact = self
111 else:
112 # Nested transaction block
113 if self._isolation:
114 top_xact_isolation = con._top_xact._isolation
115 if top_xact_isolation is None:
116 top_xact_isolation = ISOLATION_LEVELS_BY_VALUE[
117 await self._connection.fetchval(
118 'SHOW transaction_isolation;')]
119 if self._isolation != top_xact_isolation:
120 raise apg_errors.InterfaceError(
121 'nested transaction has a different isolation level: '
122 'current {!r} != outer {!r}'.format(
123 self._isolation, top_xact_isolation))
124 self._nested = True
125
126 if self._nested:
127 self._id = con._get_unique_id('savepoint')
128 query = 'SAVEPOINT {};'.format(self._id)
129 else:
130 query = 'BEGIN'
131 if self._isolation == 'read_committed':
132 query += ' ISOLATION LEVEL READ COMMITTED'
133 elif self._isolation == 'read_uncommitted':
134 query += ' ISOLATION LEVEL READ UNCOMMITTED'
135 elif self._isolation == 'repeatable_read':
136 query += ' ISOLATION LEVEL REPEATABLE READ'
137 elif self._isolation == 'serializable':
138 query += ' ISOLATION LEVEL SERIALIZABLE'
139 if self._readonly:
140 query += ' READ ONLY'
141 if self._deferrable:
142 query += ' DEFERRABLE'
143 query += ';'
144
145 try:
146 await self._connection.execute(query)
147 except BaseException:
148 self._state = TransactionState.FAILED
149 raise
150 else:
151 self._state = TransactionState.STARTED
152
153 def __check_state_base(self, opname):

Callers 1

__aenter__Method · 0.95

Calls 5

__check_state_baseMethod · 0.95
is_in_transactionMethod · 0.80
_get_unique_idMethod · 0.80
fetchvalMethod · 0.45
executeMethod · 0.45

Tested by

no test coverage detected