pandas SQLAlchemy中的PendingRollbackError

y1aodyip  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(331)

我使用sqlalchemy和pandas的to_sql()将csv文件写入MYSQL表。但是在这个过程中间,我不小心退出了程序,现在我有一个无效的事务,我不能回滚。我收到一个PendingRollbackError,提示“在回滚无效事务之前无法重新连接。请在继续之前完全回滚()。我试着使用try和except,并在except块中执行回滚,但没有成功。
下面是回溯:

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\generic.py:2878, in NDFrame.to_sql(self, name, con, schema, if_exists, index, index_label, chunksize, dtype, method)
   2713 """
   2714 Write records stored in a DataFrame to a SQL database.
   2715 
   (...)
   2874 [(1,), (None,), (2,)]
   2875 """  # noqa:E501
   2876 from pandas.io import sql
-> 2878 return sql.to_sql(
   2879     self,
   2880     name,
   2881     con,
   2882     schema=schema,
   2883     if_exists=if_exists,
   2884     index=index,
   2885     index_label=index_label,
   2886     chunksize=chunksize,
   2887     dtype=dtype,
   2888     method=method,
   2889 )

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\sql.py:768, in to_sql(frame, name, con, schema, if_exists, index, index_label, chunksize, dtype, method, engine, **engine_kwargs)
    763 elif not isinstance(frame, DataFrame):
    764     raise NotImplementedError(
    765         "'frame' argument should be either a Series or a DataFrame"
    766     )
--> 768 with pandasSQL_builder(con, schema=schema, need_transaction=True) as pandas_sql:
    769     return pandas_sql.to_sql(
    770         frame,
    771         name,
   (...)
    780         **engine_kwargs,
    781     )

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\sql.py:1548, in SQLDatabase.__exit__(self, *args)
   1546 def __exit__(self, *args) -> None:
   1547     if not self.returns_generator:
-> 1548         self.exit_stack.close()

File ~\AppData\Local\Programs\Python\Python311\Lib\contextlib.py:597, in ExitStack.close(self)
    595 def close(self):
    596     """Immediately unwind the context stack."""
--> 597     self.__exit__(None, None, None)

File ~\AppData\Local\Programs\Python\Python311\Lib\contextlib.py:589, in ExitStack.__exit__(self, *exc_details)
    585 try:
    586     # bare "raise exc_details[1]" replaces our carefully
    587     # set-up context
    588     fixed_ctx = exc_details[1].__context__
--> 589     raise exc_details[1]
    590 except BaseException:
    591     exc_details[1].__context__ = fixed_ctx

File ~\AppData\Local\Programs\Python\Python311\Lib\contextlib.py:574, in ExitStack.__exit__(self, *exc_details)
    572 assert is_sync
    573 try:
--> 574     if cb(*exc_details):
    575         suppressed_exc = True
    576         pending_raise = False

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\engine\util.py:146, in TransactionalContext.__exit__(self, type_, value, traceback)
    144     self.commit()
    145 except:
--> 146     with util.safe_reraise():
    147         if self._rollback_can_be_called():
    148             self.rollback()

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\util\langhelpers.py:147, in safe_reraise.__exit__(self, type_, value, traceback)
    145     assert exc_value is not None
    146     self._exc_info = None  # remove potential circular references
--> 147     raise exc_value.with_traceback(exc_tb)
    148 else:
    149     self._exc_info = None  # remove potential circular references

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\engine\util.py:144, in TransactionalContext.__exit__(self, type_, value, traceback)
    142 if type_ is None and self._transaction_is_active():
    143     try:
--> 144         self.commit()
    145     except:
    146         with util.safe_reraise():

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\engine\base.py:2615, in Transaction.commit(self)
   2599 """Commit this :class:`.Transaction`.
   2600 
   2601 The implementation of this may vary based on the type of transaction in
   (...)
   2612 
   2613 """
   2614 try:
-> 2615     self._do_commit()
   2616 finally:
   2617     assert not self.is_active

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\engine\base.py:2720, in RootTransaction._do_commit(self)
   2717 assert self.connection._transaction is self
   2719 try:
-> 2720     self._connection_commit_impl()
   2721 finally:
   2722     # whether or not commit succeeds, cancel any
   2723     # nested transactions, make this transaction "inactive"
   2724     # and remove it as a reset agent
   2725     if self.connection._nested_transaction:

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\engine\base.py:2691, in RootTransaction._connection_commit_impl(self)
   2690 def _connection_commit_impl(self) -> None:
-> 2691     self.connection._commit_impl()

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\engine\base.py:1134, in Connection._commit_impl(self)
   1132     self.engine.dialect.do_commit(self.connection)
   1133 except BaseException as e:
-> 1134     self._handle_dbapi_exception(e, None, None, None, None)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\engine\base.py:2342, in Connection._handle_dbapi_exception(self, e, statement, parameters, cursor, context, is_sub_exec)
   2340     else:
   2341         assert exc_info[1] is not None
-> 2342         raise exc_info[1].with_traceback(exc_info[2])
   2343 finally:
   2344     del self._reentrant_error

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\engine\base.py:1132, in Connection._commit_impl(self)
   1130         self._log_info("COMMIT")
   1131 try:
-> 1132     self.engine.dialect.do_commit(self.connection)
   1133 except BaseException as e:
   1134     self._handle_dbapi_exception(e, None, None, None, None)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\engine\base.py:573, in Connection.connection(self)
    571 if self._dbapi_connection is None:
    572     try:
--> 573         return self._revalidate_connection()
    574     except (exc.PendingRollbackError, exc.ResourceClosedError):
    575         raise

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\engine\base.py:665, in Connection._revalidate_connection(self)
    663 if self.__can_reconnect and self.invalidated:
    664     if self._transaction is not None:
--> 665         self._invalid_transaction()
    666     self._dbapi_connection = self.engine.raw_connection()
    667     return self._dbapi_connection

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sqlalchemy\engine\base.py:655, in Connection._invalid_transaction(self)
    654 def _invalid_transaction(self) -> NoReturn:
--> 655     raise exc.PendingRollbackError(
    656         "Can't reconnect until invalid %stransaction is rolled "
    657         "back.  Please rollback() fully before proceeding"
    658         % ("savepoint " if self._nested_transaction is not None else ""),
    659         code="8s2b",
    660     )

字符串

dwbf0jvd

dwbf0jvd1#

更新:问题与to_sql()中的chunk_size参数有关。我正在处理的表有很多列并且很大,所以发送的数据包的大小超过了mysql数据库的设定阈值。

相关问题