How can I create a DB Transaction with Joget BeanShell Script

Confluence User - 22 Oct, 2016

Dear Expert,

I would like to make a db transaction with the following code.

Connection con = null;
DataSource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");
con = ds.getConnection();
try{
	if (!con.isClosed()){
		con.setAutoCommit(false);
		// Some code to select a Record for checking!
		// sql = "select * from tables for update";
		...
		if (checking == true) {
			result = true;
		} else {
			// Some code update another db record here!
			...
        };
		con.commit();
} catch (Exception e) {
	LogUtil.error("Application",e, "Error updateing data");
} finally {
	if (con !=null) {
		con.close();
	};
}

 

However, I get the error of  "java.sql.SQLException: Auto-commit can not be set while enrolled in a transaction"

Everyone can point me how to solve this problem.

 

Many Thanks!

Best Regards

Tony Chu

beanshell;record;locking;jdbc

1


23 Oct, 2016
confluenceUser
1
confluenceUser

Ok I found out  the answer and here is the Code by using:

con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

and not

con.setAutoCommit(false);

 

Connection con = null;
DataSource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");
con = ds.getConnection();
try{
    if (!con.isClosed()){
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        // Some code to select a Record for checking!
        // sql = "select * from tables for update";
        ...
        if (checking == true) {
            result = true;
        } else {
            // Some code update another db record here!
            ...
        };
        con.close();
} catch (Exception e) {
    LogUtil.error("Application",e, "Error updateing data");
} finally {
    if (con !=null) {
        con.close();
    };
}
RELATED QUESTIONS

Your answer


To answer a question you'll need an account.

Print