Search in sources :

Example 1 with Transaction

use of org.nutz.trans.Transaction in project nutz by nutzam.

the class NutDaoRunner method run.

public void run(final DataSource dataSource, final ConnCallback callback) {
    if (callback instanceof DaoInterceptorChain) {
        // 看看是不是应该强制使用事务
        DaoStatement[] sts = ((DaoInterceptorChain) callback).getDaoStatements();
        boolean useTrans = false;
        boolean isAllSelect = true;
        for (DaoStatement st : sts) {
            if (!st.isSelect() && !st.isForceExecQuery()) {
                isAllSelect = false;
                break;
            }
        }
        switch(meta.getType()) {
            case PSQL:
                // PSQL必须带事务,不然Clob和Blob操作必死
                useTrans = true;
                break;
            case SQLITE:
                // SQLITE仅支持2种事务级别
                Transaction t = Trans.get();
                if (t == null) {
                    if (isAllSelect)
                        useTrans = false;
                    else {
                        ((DaoInterceptorChain) callback).setAutoTransLevel(Connection.TRANSACTION_READ_UNCOMMITTED);
                        useTrans = true;
                    }
                } else if (t.getLevel() != Connection.TRANSACTION_SERIALIZABLE && t.getLevel() != Connection.TRANSACTION_READ_UNCOMMITTED) {
                    t.setLevel(Connection.TRANSACTION_READ_UNCOMMITTED);
                    useTrans = true;
                }
                break;
            default:
                useTrans = !(Trans.isTransactionNone() && (sts.length == 1 || isAllSelect));
                break;
        }
        // 看来需要开启事务了
        if (useTrans) {
            Trans.exec(((DaoInterceptorChain) callback).getAutoTransLevel(), new Atom() {

                public void run() {
                    _run(dataSource, callback);
                }
            });
            return;
        }
    }
    // 不需要额外加事务,直接通过
    _run(dataSource, callback);
}
Also used : DaoInterceptorChain(org.nutz.dao.DaoInterceptorChain) Transaction(org.nutz.trans.Transaction) DaoStatement(org.nutz.dao.sql.DaoStatement) Atom(org.nutz.trans.Atom)

Aggregations

DaoInterceptorChain (org.nutz.dao.DaoInterceptorChain)1 DaoStatement (org.nutz.dao.sql.DaoStatement)1 Atom (org.nutz.trans.Atom)1 Transaction (org.nutz.trans.Transaction)1