diff --git a/core/storage/src/relational/mod.rs b/core/storage/src/relational/mod.rs index 364301189..cef07bf39 100644 --- a/core/storage/src/relational/mod.rs +++ b/core/storage/src/relational/mod.rs @@ -20,7 +20,7 @@ use common::{ PaginationResponse, Range, Result, }; use common_logger::{tracing, tracing_async}; -use db_xsql::{rbatis::Bytes as RbBytes, XSQLPool}; +use db_xsql::{commit_transaction, rbatis::Bytes as RbBytes, XSQLPool}; use protocol::db::{DBDriver, DBInfo, SimpleBlock, SimpleTransaction, TransactionWrapper}; use ckb_types::core::{BlockNumber, BlockView, HeaderView}; @@ -51,7 +51,7 @@ impl Storage for RelationalStorage { self.insert_transaction_table(ctx.clone(), &block, &mut tx) .await?; - tx.commit().await?; + commit_transaction(tx).await?; Ok(()) } @@ -69,7 +69,7 @@ impl Storage for RelationalStorage { .await?; self.remove_block_table(ctx.clone(), block_number, block_hash, &mut tx) .await?; - tx.commit().await?; + commit_transaction(tx).await?; Ok(()) } @@ -545,7 +545,7 @@ impl Storage for RelationalStorage { let res = self .insert_registered_address_table(addresses, &mut tx) .await?; - tx.commit().await?; + commit_transaction(tx).await?; Ok(res .iter() diff --git a/core/synchronization/src/lib.rs b/core/synchronization/src/lib.rs index 8a89f0eaf..11a8c136b 100644 --- a/core/synchronization/src/lib.rs +++ b/core/synchronization/src/lib.rs @@ -9,7 +9,7 @@ use core_storage::relational::table::{ IO_TYPE_INPUT, IO_TYPE_OUTPUT, }; use core_storage::relational::{generate_id, to_rb_bytes, BATCH_SIZE_THRESHOLD}; -use db_xsql::{rbatis::crud::CRUDMut, XSQLPool}; +use db_xsql::{commit_transaction, rbatis::crud::CRUDMut, XSQLPool}; use ckb_types::core::{BlockNumber, BlockView}; use ckb_types::prelude::*; @@ -335,9 +335,8 @@ async fn sync_indexer_cell(task: Vec, rdb: XSQLPool) -> Result<()> .for_each(|c| c.id = generate_id(c.block_number)); core_storage::save_batch_slice!(tx, indexer_cells, status_list); - tx.commit().await?; + commit_transaction(tx).await?; - let _ = tx.take_conn().unwrap().close().await; free_one_task(); Ok(()) @@ -419,9 +418,7 @@ async fn sync_blocks( canonical_data_table_batch ); - tx.commit().await?; - - let _ = tx.take_conn().unwrap().close().await; + commit_transaction(tx).await?; Ok(()) } diff --git a/db/xsql/src/lib.rs b/db/xsql/src/lib.rs index 01476d4d0..1df7d6d30 100644 --- a/db/xsql/src/lib.rs +++ b/db/xsql/src/lib.rs @@ -2,7 +2,7 @@ pub mod page; pub use rbatis; -use common::Result; +use common::{anyhow::anyhow, Result}; use protocol::db::DBDriver; use log::LevelFilter; @@ -146,6 +146,15 @@ impl XSQLPool { } } +pub async fn commit_transaction(mut tx: RBatisTxExecutor<'_>) -> Result<()> { + if tx.commit().await.is_err() { + tx.rollback().await?; + return Err(anyhow!("Commit transaction failed, transaction rollback!")); + } + + Ok(()) +} + fn build_url( db_type: &str, db_name: &str, diff --git a/src/main.rs b/src/main.rs index 32bfe97e0..4f1fe9227 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #[tokio::main(flavor = "multi_thread")] async fn main() { std::panic::set_hook(Box::new(move |info| { + println!("panic occurred {:?}", info); log::error!("panic occurred {:?}", info); std::process::exit(-1); }));