Giter VIP home page Giter VIP logo

Comments (5)

zhuxiujia avatar zhuxiujia commented on June 3, 2024 1

database:sql server 2016.

Call to start transaction:
 let mut txn = CONTEXT.rb.acquire_begin().await?;

Execute transaction error:
  Token error: 'Incorrect syntax near 'begin'.' on server WIN-B9A9 executing  on line 1 (code: 102, state: 1, class: 15)  

Sql server does not support 'begin'.

Added the following method:

pub async fn acquire_begin_transaction(&self) -> Result<RBatisTxExecutor, Error> {
        let pool = self.rb.get_pool()?;
        let mut conn = pool.get().await?;

        conn.exec("BEGIN TRAN", vec![]).await?;

        Ok(RBatisTxExecutor {
            tx_id: new_snowflake_id(),
            conn: Mutex::new(Box::new(conn)),
            rb: self.rb.clone(),
            done: false,
        })
    }


Execute  error:
Token error: 'Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1.' on server WIN-B9A9 executing  on line 0 (code: 266, state: 2, class: 16)

Please give me some advice?

SQL Server 2016 supports starting a transaction using BEGIN TRANSACTION, which is used to explicitly start a transaction in SQL Server. Through BEGIN TRANSACT, you can start a new transaction and then end or roll back the transaction in either COMMIT TRANSACT or Rollback TRANSACT.

I suggest you use an interceptor to modify the transaction statement start to BEGIN TRANSACTION

#[derive(Debug)]
pub struct TxIntercept {}

#[async_trait]
impl Intercept for TxIntercept {
    async fn before(
        &self,
        _task_id: i64,
        _rb: &dyn Executor,
        sql: &mut String,
        _args: &mut Vec<Value>,
        _result: ResultType<&mut Result<ExecResult, Error>, &mut Result<Vec<Value>, Error>>,
    ) -> Result<bool, Error> {
        if sql.contains("begin") {
          *sql = sql.replace("begin","BEGIN TRANSACTION").to_string();
        } else if sql.contains("commit") {
            *sql = sql.replace("commit","COMMIT TRANSACTION").to_string();
        } else if sql.contains("rollback") {
            *sql = sql.replace("rollback","ROLLBACK TRANSACTION").to_string();
        }
        Ok(true)
    }
}
#[tokio::main]
pub async fn main() {
    fast_log::init(fast_log::Config::new().console()).expect("rbatis init fail");
    defer!(|| log::logger().flush());
    let rb = RBatis::new();
    rb.intercepts.push(Arc::new(TxIntercept {}));
}

from rbatis.

xingsongs avatar xingsongs commented on June 3, 2024

database:sql server 2016.

Call to start transaction:
 let mut txn = CONTEXT.rb.acquire_begin().await?;

Execute transaction error:
  Token error: 'Incorrect syntax near 'begin'.' on server WIN-B9A9 executing  on line 1 (code: 102, state: 1, class: 15)  

Sql server does not support 'begin'.

Added the following method:

pub async fn acquire_begin_transaction(&self) -> Result<RBatisTxExecutor, Error> {
        let pool = self.rb.get_pool()?;
        let mut conn = pool.get().await?;

        conn.exec("BEGIN TRAN", vec![]).await?;

        Ok(RBatisTxExecutor {
            tx_id: new_snowflake_id(),
            conn: Mutex::new(Box::new(conn)),
            rb: self.rb.clone(),
            done: false,
        })
    }


Execute  error:
Token error: 'Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1.' on server WIN-B9A9 executing  on line 0 (code: 266, state: 2, class: 16)

Please give me some advice?

SQL Server 2016 supports starting a transaction using BEGIN TRANSACTION, which is used to explicitly start a transaction in SQL Server. Through BEGIN TRANSACT, you can start a new transaction and then end or roll back the transaction in either COMMIT TRANSACT or Rollback TRANSACT.

I suggest you use an interceptor to modify the transaction statement start to BEGIN TRANSACTION

#[derive(Debug)]
pub struct TxIntercept {}

#[async_trait]
impl Intercept for TxIntercept {
    async fn before(
        &self,
        _task_id: i64,
        _rb: &dyn Executor,
        sql: &mut String,
        _args: &mut Vec<Value>,
        _result: ResultType<&mut Result<ExecResult, Error>, &mut Result<Vec<Value>, Error>>,
    ) -> Result<bool, Error> {
        if sql.contains("begin") {
          *sql = sql.replace("begin","BEGIN TRANSACTION").to_string();
        } else if sql.contains("commit") {
            *sql = sql.replace("commit","COMMIT TRANSACTION").to_string();
        } else if sql.contains("rollback") {
            *sql = sql.replace("rollback","ROLLBACK TRANSACTION").to_string();
        }
        Ok(true)
    }
}
#[tokio::main]
pub async fn main() {
    fast_log::init(fast_log::Config::new().console()).expect("rbatis init fail");
    defer!(|| log::logger().flush());
    let rb = RBatis::new();
    rb.intercepts.push(Arc::new(TxIntercept {}));
}

Thank you very much that the interceptor has taken effect.
But it still prompts this error:Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1.

from rbatis.

zhuxiujia avatar zhuxiujia commented on June 3, 2024

Thank you very much that the interceptor has taken effect.
But it still prompts this error:Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1.

did you call commit method or rollback ?

from rbatis.

xingsongs avatar xingsongs commented on June 3, 2024

Thank you very much that the interceptor has taken effect.
But it still prompts this error:Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1.

did you call commit method or rollback ?

image call CONTEXT.rb.try_acquire_begin().await? report error

from rbatis.

xingsongs avatar xingsongs commented on June 3, 2024

#488 Already solved

from rbatis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.