在使用sping 数据库使用oracle 时,想要更新某一条记录时,其他的线程不可操作此条记录,可以使用select * from tableName where id=? for update; 这样就可以把这条符合条件的记录上锁,行级锁。什么时候解锁呢,这个需要 手动提交事务后 就解锁了。spring 提供了手动提交事务的操作。
import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallback; import org.springframework.transaction.support.TransactionTemplate;
@Autowired TransactionTemplate transactionTemplate; transactionTemplate.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { try { FbiInfluxTransaction lock = influxTransactionService.selectFbiInfluxTransactionForUpdate(updateTransaction.getSettleSequenceNo(), "0"); if (lock != null) { FbiInfluxTransaction fbiInfluxTransaction = new FbiInfluxTransaction(); } } catch (Exception e) { LogUtil.error(LOGGER,e,"ErrorCode:{0},Error:{1},SettleSequenceNo:{2}" ,ErrorCode.FNFIBIZ012.getCode(), ErrorCode.FNFIBIZ012.getMsg() , bankReturnInforBO.getSettleSequenceNo()); status.setRollbackOnly(); throw new RuntimeException(e); } return null; } });