Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db transaction issue #18

Open
CrossLee opened this issue Mar 30, 2020 · 4 comments
Open

db transaction issue #18

CrossLee opened this issue Mar 30, 2020 · 4 comments

Comments

@CrossLee
Copy link

sample code:

@With(BeetlSqlTransactional.class)
    public void test() {
    mapper1.insert(obj1);
mapper2.insert(obj1);

// 只要加了类似下面的Response操作,上面的insert就不会提交(很有可能是回滚了)
H.Response response=
response.contentType("text/html;charset=" + ZhifubaoConstants.CHARSET);
            response.writeHtml(form);
            response.close();

}
@javamonkey
Copy link
Collaborator

javamonkey commented Mar 31, 2020

这是为啥呢,beetlsql不会主动提交事务导致的,不过应该是@with(BeetlSqlTransactional.class)没有发挥作用?

//SQLScript.java
 protected void clean(boolean isUpdate, Connection conn, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null)
                rs.close();
            if (ps != null)
                ps.close();
            if (!this.sm.getDs().isTransaction()) {
                try {

                    if (conn != null) {
                        // colse 不一定能保证能自动commit
                        if (isUpdate && !conn.getAutoCommit()) {

                            conn.commit();
                        }

                        conn.close();
                    }
                } catch (SQLException e) {
                    throw new BeetlSQLException(BeetlSQLException.SQL_EXCEPTION, e);
                }

            }
        } catch (SQLException e) {
            // ignore
        }
    }

this.sm.getDs().isTransaction() 用来判断是否是否在事务环境,一般跟web框架集成,都会返回true
这样,等待web框架主动commit

@greenlaw110
Copy link
Contributor

@CrossLee BeetlSqlTransactional is a After interceptor:
image

However because you have already closed the response, therefore After interceptor will no longer be executed:
image

Solution, use @act.db.sql.tx.Transactional to replace @With(BeetlSqlTransactional.class):

say, the following code will make sure TX get committed:

        @Transactional
        public void update2(Integer id, String name, H.Response resp) {
            doUpdate(id, name);
            resp.contentType("text/plaintext");
            resp.writeText("done");
            resp.close();
        }

@CrossLee
Copy link
Author

CrossLee commented Apr 2, 2020

@greenlaw110 是不是@transactional 都可以替代@with(BeetlSqlTransactional.class)?

@greenlaw110
Copy link
Contributor

greenlaw110 commented Apr 2, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants