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;
}
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);
}
}
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");
}
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);
}
}
}
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);
}
}
Aggregations