Search in sources :

Example 11 with DbSession

use of io.nuls.db.transactional.annotation.DbSession in project nuls by nuls-io.

the class TransactionalInterceptorImpl method intercept.

@Override
public Object intercept(Annotation annotation, Object obj, Method method, Object[] args, BeanMethodInterceptorChain interceptorChain) throws Throwable {
    String lastId = SessionManager.getId();
    String id = lastId;
    if (id == null) {
        id = StringUtils.getNewUUID();
    }
    Object result;
    boolean isSessionBeginning = false;
    boolean isCommit = false;
    DbSession ann = (DbSession) annotation;
    if (ann.transactional() == PROPAGATION.REQUIRED && !SessionManager.getTxState(id)) {
        isCommit = true;
    } else if (ann.transactional() == PROPAGATION.INDEPENDENT) {
        id = StringUtils.getNewUUID();
        isCommit = true;
    }
    SqlSession session = SessionManager.getSession(id);
    if (session == null) {
        isSessionBeginning = true;
        session = SessionManager.openSession(false);
        SessionManager.setConnection(id, session);
        SessionManager.setId(id);
    } else {
        isSessionBeginning = false;
    }
    try {
        if (isCommit) {
            SessionManager.startTransaction(id);
        }
        result = interceptorChain.execute(annotation, obj, method, args);
        if (isCommit) {
            session.commit();
            SessionManager.endTransaction(id);
        }
    } catch (Exception e) {
        session.rollback();
        SessionManager.endTransaction(id);
        throw e;
    } finally {
        if (isSessionBeginning) {
            SessionManager.setConnection(id, null);
            SessionManager.setId(lastId);
            session.close();
        }
    }
    return result;
}
Also used : DbSession(io.nuls.db.transactional.annotation.DbSession) SqlSession(org.apache.ibatis.session.SqlSession)

Example 12 with DbSession

use of io.nuls.db.transactional.annotation.DbSession in project nuls by nuls-io.

the class AccountTxDaoImpl method rollbackAlias.

@Override
@DbSession
public void rollbackAlias(AliasPo aliasPo) {
    try {
        AliasPo po = aliasDao.get(aliasPo.getAlias());
        if (po != null && po.getAddress().equals(aliasPo.getAddress())) {
            aliasDao.delete(aliasPo.getAlias());
            AccountPo accountPo = new AccountPo();
            po.setAddress(aliasPo.getAddress());
            po.setAlias("");
            accountDao.updateAlias(accountPo);
        }
    } catch (Exception e) {
        throw new NulsRuntimeException(ErrorCode.DB_ROLLBACK_ERROR);
    }
}
Also used : AccountPo(io.nuls.db.entity.AccountPo) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) AliasPo(io.nuls.db.entity.AliasPo) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) DbSession(io.nuls.db.transactional.annotation.DbSession)

Example 13 with DbSession

use of io.nuls.db.transactional.annotation.DbSession in project nuls by nuls-io.

the class AccountTxDaoImpl method saveAlias.

@DbSession
@Override
public Result saveAlias(AliasPo alias) {
    try {
        aliasDao.save(alias);
        AccountPo po = new AccountPo();
        po.setAddress(alias.getAddress());
        po.setAlias(alias.getAlias());
        accountDao.updateAlias(po);
    } catch (Exception e) {
        throw new NulsRuntimeException(ErrorCode.DB_SAVE_ERROR);
    }
    return new Result(true, "OK");
}
Also used : AccountPo(io.nuls.db.entity.AccountPo) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) Result(io.nuls.core.chain.entity.Result) DbSession(io.nuls.db.transactional.annotation.DbSession)

Example 14 with DbSession

use of io.nuls.db.transactional.annotation.DbSession in project nuls by nuls-io.

the class AccountTxDaoImpl method importAccount.

@DbSession
@Override
public void importAccount(List<AccountPo> accountPoList) {
    for (AccountPo account : accountPoList) {
        accountDao.save(account);
        for (int i = 0; i < account.getMyTxs().size(); i++) {
            TransactionLocalPo tx = account.getMyTxs().get(i);
            txDao.save(tx);
        }
    }
}
Also used : AccountPo(io.nuls.db.entity.AccountPo) TransactionLocalPo(io.nuls.db.entity.TransactionLocalPo) DbSession(io.nuls.db.transactional.annotation.DbSession)

Example 15 with DbSession

use of io.nuls.db.transactional.annotation.DbSession in project nuls by nuls-io.

the class NodeDaoImpl method saveChange.

@Override
@DbSession
public void saveChange(NodePo po) {
    try {
        Searchable searchable = new Searchable();
        searchable.addCondition("id", SearchOperator.eq, po.getId());
        if (getMapper().selectCount(searchable) > 0) {
            getMapper().updateByPrimaryKey(po);
        } else {
            getMapper().insert(po);
        }
    } catch (Exception e) {
        Log.error(e);
    }
}
Also used : Searchable(io.nuls.db.dao.impl.mybatis.util.Searchable) DbSession(io.nuls.db.transactional.annotation.DbSession)

Aggregations

DbSession (io.nuls.db.transactional.annotation.DbSession)15 NulsException (io.nuls.core.exception.NulsException)7 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)6 AccountPo (io.nuls.db.entity.AccountPo)6 Account (io.nuls.account.entity.Account)4 Result (io.nuls.core.chain.entity.Result)3 AliasPo (io.nuls.db.entity.AliasPo)3 TransactionLocalPo (io.nuls.db.entity.TransactionLocalPo)3 ValidateResult (io.nuls.core.validate.ValidateResult)2 TransactionPo (io.nuls.db.entity.TransactionPo)2 UtxoInputPo (io.nuls.db.entity.UtxoInputPo)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Block (io.nuls.core.chain.entity.Block)1 Transaction (io.nuls.core.chain.entity.Transaction)1 Searchable (io.nuls.db.dao.impl.mybatis.util.Searchable)1 TxAccountRelationPo (io.nuls.db.entity.TxAccountRelationPo)1 UtxoOutputPo (io.nuls.db.entity.UtxoOutputPo)1 UtxoOutput (io.nuls.ledger.entity.UtxoOutput)1 AbstractCoinTransaction (io.nuls.ledger.entity.tx.AbstractCoinTransaction)1